$(function() {

if( $(window).width() < 1025) {   

$("#prev-button, #next-button").css({'display': 'none'});

 };

 $("#menu li a").click(function() {

		$this = $(this);
		var goingto = $this.attr("href");
		var wherearewe = $("#menu .active").find("a:first").attr("href");
		if(wherearewe==undefined) { wherearewe="#slide1"; }

		goingtoIndex = $(goingto).index();
		whereareweIndex = $(wherearewe).index();

		move = 1000;
		move2 = 300;

		if(goingtoIndex>whereareweIndex) { 
			// Moving forward
			var multiplier = goingtoIndex-whereareweIndex;
			move = move*multiplier;
			$("#circles1 div").animate({ 'left' : '-='+move },{
				duration: 1000
			});
			$("#circles2 div").animate({ 'left' : '+='+move2 },{
				duration: 1200
			});
		} else if(goingtoIndex<whereareweIndex) { 
			// Going backwards
			var multiplier = whereareweIndex-goingtoIndex;
			move = move*multiplier;
			$("#circles1 div").animate({ 'left' : '+='+move },{
				duration: 1000
			});
			$("#circles2 div").animate({ 'left' : '-='+move2 },{
				duration: 1200
			});
		} else if(goingtoIndex==whereareweIndex) {
			// Same tab
		} else {	
			// Multiple click on the same menu item
			$("#circles1 div").animate({ 'left' : '+='+move },{
				duration: 1000
			});
			$("#circles2 div").animate({ 'left' : '-='+move2 },{
				duration: 1200
			});
		}
	});

	$('body').append('<div id="white-out"></div>');
	
	$('#white-out').animate({
		'opacity':'0'}, 1600, function(){
			$('#white-out').remove()
		}
	);

$.localScroll.defaults.axis = 'x';

	offset = difference();
	$("#slider").css("margin-left","-"+offset+"px");

	function difference() {
		var slide = $(".slide").eq(0).width();
		var screenWidth = $(window).width();
		var newScrollWidth = ((slide-screenWidth)/2);
		return newScrollWidth;
	}

function windowSizeEvent() {
offset = difference();
$("#slider").css("margin-left","-"+offset+"px");
if( $(window).width() < 1025) { 
$("#prev-button, #next-button").css({'display': 'none'});
} else {
$("#prev-button, #next-button").css({'display': 'block'});
}
}

$(window).resize(windowSizeEvent);
	
	$.localScroll({
		target: '#container', 
		queue:true,
		duration:1200,
		offset: {left:+offset},
onBefore: windowSizeEvent,
onAfter: windowSizeEvent
	});

	index = 0;

	$menu_a = $("#menu a");
	$menu_li = $("#menu li");	
	$next = $("#next-button");
	$prev = $("#prev-button");

	function clearNextPrev() 
	{
		$next.removeClass("disabled");
		$prev.removeClass("disabled");	
	}

	$menu_a.click(function() 
	{
	    $menu_li.removeClass("active"); // Remove active ID
		$(this).parent().addClass("active"); // Apply active ID
		index = $menu_a.index(this);		
		clearNextPrev();
		if(index==($menu_li.size()-1)) {
			$next.addClass("disabled");
		}
		if(index==0) {
			$prev.addClass("disabled");
		}
	});

	$next.click(function() 
	{
		clearNextPrev();
		$menu_a.eq(index+1).click();
		if(index==($menu_li.size()-1)) {
			$(this).addClass("disabled");
		}
	});

	$prev.click(function() 
	{
		clearNextPrev();

		if(index!=0) {
			$menu_a.eq(index-1).click();			
		}
		if(index==0) {
			$(this).addClass("disabled");
		}
	});


/*$ul.find("li").each(function() {
$(this).find("img").load(function() {
console.log($(this).parent().width());
width = width+$(this).parent().width();
});
});*/

var $ul = $("#design-print-cycle ul");

var img = $ul.find("img").size();
var count = 0

$ul.find("img").load(function() {
count++;
if(count==img) {
//designPrintGallery();
}
});

$(window).load(
function() {
designPrintGallery();
}
);

function designPrintGallery() {

            $('#design-print-cycle');
			var $nav = $("#design-print-nav");

			var width = 0;

$ul.find("li").each(function() {
width += $(this).width();
});
				
				$ul.css({ 'width': width+"px"}); // Loop through li's to get UL width			

				nav = $ul.find("li").size();

				for (var i = 0; i < nav; i++) {
					$nav.append("<a href='#'>"+i+"</a>");
				};

				$nav.find("a:first").addClass("active");

				$nav.find("a").bind('click',function() {
					var index = $(this).index();
					var left = $ul.find("li").eq(index).position().left;
					$nav.find("a").removeClass("active");
					$(this).addClass("active");
					$ul.animate({ 'left' : '-'+left+'px' },1000);
					return false;
				});

};

$ul2 = $("#property-boards-cycle ul");

var img2 = $ul2.find("img").size();
var count2 = 0

$ul2.find("img").load(function() {
count2++;
if(count2==img2) {
//propertyBoardsGallery();
}
});

$(window).load(
function() {
propertyBoardsGallery();
}
);


function propertyBoardsGallery() {	
			
			$('#property-boards-cycle');
			var $nav2 = $("#property-boards-nav");
				var height = 0;

				$ul2.find("li").each(function() {
					height += $(this).height();
				});

				$ul2.css({ 'height': height+"px"}); // Loop through li's to get UL width			

				nav2 = $ul2.find("li").size();

				for (var i = 0; i < nav2; i++) {
					$nav2.append("<a href='#'>"+i+"</a>");
				};

				$nav2.find("a:first").addClass("active");

				$nav2.find("a").bind('click',function() {
					var index = $(this).index();
					var top = $ul2.find("li").eq(index).position().top;
					$nav2.find("a").removeClass("active");
					$(this).addClass("active");
					$ul2.animate({ 'top' : '-'+top+'px' },1000);
					return false;
				});

};
			

	$('#going-on-cycle').after('<div id="going-on-nav">').cycle({
		fx: 'scrollUp',
		speed: 700,
		timeout: 10000,
		pager: '#going-on-nav',
		cleartype: 0,
		cleartypeNoBg:true
	});


	$('#video-iframe1').ready(function() {
		$('#video1').css({ 'display' : 'none', 'left' : '50%'  });
	});
	
	$('#video-iframe2').ready(function() {
		$('#video2').css({ 'display' : 'none', 'left' : '50%'  });
	});
	
	$('#video-iframe3').ready(function() {
		$('#video3').css({ 'display' : 'none', 'left' : '50%'  });
	});

	$("#how-we-do-it, #design-print-link, #franchise-link, #web-to-print-link5").click(function() {
		$("#black-out, #video1").show();
	});

	$("#property-boards-link, #property-boards-link5").click(function() {
		$("#black-out, #video2").show();
	});

	$("#property-promotion-link5").click(function() {
		$("#black-out, #video3").show();
	});

	$(".close-button").click(function() {
		$('#black-out, #video1, #video2, #video3').hide();
	});

});
