
/***********************************/
/*	requires jquery	*/

//load xml with gallery data at the begining and set the global variable
$(window).ready(function(){
	cdsGallery.loadxml();
	$(".loadBtn").click(function(e){
		e.preventDefault();
		var projectstring = cdsGallery.loadproject($(this).attr('data-project-id'));
		var boxcss = cdsGallery.positioncontent();
		$(projectstring)
			.css({"display":"none"})
			.appendTo('body');

		$('#gallery_filmstrip').css({
			'left': boxcss.box_left,
			'width' : boxcss.box_width
		});

		$("#gallery_prev").css({"display":"none"});

		$("#gallery_master").fadeIn(function(){
			cdsGallery.removeblinds();
			cdsGallery.move_next();
			cdsGallery.move_prev();
		});

		$('body').data('oldwidth', $(window).width());

		$(window).bind('resize', function(oldsize){
			var old_size = $('body').data('oldwidth');
			var new_size = $(window).width();
			cdsGallery.reposition(old_size, new_size);
			$('body').data('oldwidth', new_size);
		});

		cdsGallery.destroy();
		//save initial left value for later :)
		$("body").data('initial_left', boxcss.box_left);

	});
});


var cdsGallery = {
	loadxml : function(){
		$.ajax({
			'url': 'gallery.xml',
			'type': 'post',
			'cache': true,
			'dataType': 'xml',
			success: function(xml){
				$('body').data('gallery', xml);
			}
		});
	},
	loadproject : function(project_id){
		project_id = project_id.toLowerCase();
		var gallery = $("body").data('gallery');
		var test_pattern = '[id='+project_id+']';
		var project = $(gallery).find(test_pattern);
		var images = $(gallery).find('[id='+project_id+'] image');
		if($(images).length !== 0){
			var imgstring = '';
			var box_count = 0;
			var url = '';
			//if has child nodes loop through them and return image panel
			$(images).each(function(){
				var imgtitle = '';
				if($(this).attr('title') != ''){
					imgtitle = '<div class="title"><h4>'+imgtitle+'</h4></div>';
				}
				//create html string for single image
				var single = [
					'<div class="gallery_img_wrap">',
						'<div class="gal_preload"></div>',
						'<img src="images/'+$(this).attr('src')+'" alt="'+$(this).attr('alt')+'" />',
						imgtitle,
					'</div>'
				].join('');
				box_count++;
				imgstring += single;
			});

			projectUrl = $(project).attr('url') != "undefined" ? $(project).attr('url') : "";

			if(typeof($(project).attr("url")) !== "undefined") {
				url= '<a class="hasUrl"><a class="livesite" title="See this site live!" target="_blank" href="'+ projectUrl +'"></a>';
			}
			var descstring = '';
			if(($(gallery).find(test_pattern +' client').length != 0) || ($(gallery).find(test_pattern +' client').text() != '' )){
				descstring += 'Client: <strong>'+$(project).find('client').text()+'</strong>';
			}
			if(($(gallery).find(test_pattern +' projekat').length != 0) || ($(gallery).find(test_pattern +' projekat').text() != '' )){
				descstring += 'Project: <strong>'+$(project).find('projekat').text()+'</strong>';
			}
			if(($(gallery).find(test_pattern +' project_lead').length != 0) || ($(gallery).find(test_pattern +' project_lead').text() != '')){
				descstring += 'Project Leader: <strong>'+$(project).find('project_lead').text()+'</strong>';
			}
			if(($(gallery).find(test_pattern +' art_director').length != 0) || ($(gallery).find(test_pattern +' art_director').text() != '')){
				descstring += 'Art Director: <strong>'+$(project).find('art_director').text()+'</strong>';
			}
			if(($(gallery).find(test_pattern +' developer').length != 0) || ($(gallery).find(test_pattern +' developer').text() != '')){
				descstring += 'Developer: <strong>'+$(project).find('developer').text()+'</strong>';
			}
			if(($(gallery).find(test_pattern +' designer').length != 0) || ($(gallery).find(test_pattern +' designer').text() != '')){
				descstring += 'Design: <strong>'+$(project).find('designer').text()+'</strong>';
			}
			if(($(gallery).find(test_pattern +' photographer').length != 0) || ($(gallery).find(test_pattern +' photographer').text() != '')){
				descstring += 'Photography: <strong>'+$(project).find('photographer').text()+'</strong>';
			}
			if(($(gallery).find(test_pattern +' drawer').length != 0) || ($(gallery).find(test_pattern +' drawer').text() != '')){
				descstring += 'Drawings: <strong>'+$(project).find('drawer').text()+'</strong>';
			}

			//create html string for full project
			var projectstring = [
				'<div id="gallery_master">',
					'<div id="gallery_blur"></div>',
					'<div id="gallery_meta">',
						'<div id="gal_meta_wrapper">',
							'<div id="gallery_close"></div>',
							'<h2 id="project_info">'+$(project).attr('pr-id')+'</h2>',
							'<p id="project_description">'+descstring+'<p>',
							'<div class="gallery_social_media">',
								url,
								'<a rel="nofollow" id="twitter_link" target="_blank" href="http://twitter.com/home?status='+encodeURIComponent("Check this: ")+'http://www.nemanjaivanovic.com/" title="nemanjaivanovic.com"></a>',
								'<a rel="nofollow" id="facebook_link" target="_blank" 	href="http://www.facebook.com/sharer.php?u=http://www.nemanjaivanovic.com/&amp; title="Share this on Facebook"></a>',
							'</div>',
						'</div>',
					'</div>',
					'<div id="gallery_filmstrip">'+imgstring+'',
					'</div>',
					'<a href="javascript:void(0);" class="gallery_controls" id="gallery_next"></a>',
					'<a href="javascript:void(0);" class="gallery_controls" id="gallery_prev"></a>',
				'</div>'
			].join('');

			$("body").data('box_count', box_count);
			var min_left = (box_count-2) * 600 * (-1);
			$("body").data('min_left', min_left);
			return projectstring;
		}else{
			//if no images, do nothing
			return false;
		}
	},
	removeblinds : function(){
		$(".gallery_img_wrap img").each(function(){
			$(this).load(function(){
				$(this).siblings('.gal_preload').fadeOut();
			});
		});
	},
	positioncontent : function(){
		var whalf = $(window).width() / 2;
		var slide = 600;
		var left = Math.round(whalf - slide/2);
		var width = $('body').data('box_count') * 600;
		var newcss = {
			'box_left': left,
			'box_width': width
		};
		return newcss;
	},
	reposition : function(old_size, new_size){
		var delta =  (old_size - new_size) / 2;
		var cur_left = parseInt($("#gallery_filmstrip").css('left'), 10);
		var left = cur_left - delta;
		$("#gallery_filmstrip").css({'left':left});
	},
	destroy : function(){
		$("#gallery_blur, #gallery_close").click(function(){
			$("#gallery_master").fadeOut(function(){$(this).remove();});
		});
	},
	move_next : function(){
		$('#gallery_next').click(function(e){
			curr_left = parseInt($("#gallery_filmstrip").css('left'), 10);
			curr_left -= 600;
			$("#gallery_filmstrip").not(':animated').animate({'left': curr_left}, 200);
			if(curr_left < $('body').data('min_left')){
				$(this).fadeOut();
			}
			if($("#gallery_prev").css("display") == 'none'){
				$("#gallery_prev").fadeIn();
			}
		});
	},
	move_prev : function(){
		$('#gallery_prev').click(function(e){
			curr_left = parseInt($("#gallery_filmstrip").css('left'), 10);
			curr_left += 600;
			$("#gallery_filmstrip").not(':animated').animate({'left': curr_left}, 200);
			if(curr_left > ($('body').data('initial_left') - 600)){
				$(this).fadeOut();
			}
			if($("#gallery_next").css("display") == 'none'){
				$("#gallery_next").fadeIn();
			}
		});
	}
};
