$(function () {
	
	var activeUrl = "";
	var activeBoard = 0;
	var activeProject = "";
	var activeTemplate = "";
	var popupShow = false;
	var currentPorto = 1;
	
	var url1 = ['','Betta-browser', 'Rgbclass-browser', 'Concept-browser'];
	var url2 = ['','Chinatrust-browser', '01photography-browser', 'Ecorner-browser'];
	var url3 = ['','Sandei-browser', 'Cygnet-browser', 'Lenmarc-browser'];
	var url4 = ['','Sharp-browser', 'Vimala-browser', 'Black-browser'];
	var url5 = ['','Rexona-browser', 'Aptare-browser', 'Changer-browser'];
	var url6 = ['','Nikon-browser', 'Pizzahut-browser', 'Fanta-browser'];
	var url7 = ['','Cisco1-browser', 'Cisco2-browser', 'Pastelmojo-browser'];
	var url8 = ['','Mcafee-ipadver', 'Epocrates-browser', 'Lumbung-browser'];
	var url9 = ['','Goodybops-iphone', 'Broadcom-iphone', 'Emc-iphone'];
	

	function swapImage(id, type){
		if(!popupShow){
			var currentBoard = '#basicBoard' + id;
			
			if(activeBoard == 0){
				activeBoard = id;
			}else{
				$('#basicBoard' + activeBoard).flightboard('change',{lettersImage: 'public/images/port'+  activeBoard +'-bw.jpg'}, '', 'current', false);
				activeBoard = id;
			}
			
			if(type == 'on'){
				$(currentBoard).flightboard('change',{lettersImage: 'public/images/port'+  id +'.jpg'}, '', 'current', false);
				activeUrl = eval("url"+ id +"[$(currentBoard).flightboard('current')]");
			}else{
				$('#basicBoard' + id).flightboard('change',{lettersImage: 'public/images/port'+  id +'-bw.jpg'}, '', 'current', false);
			}
		}
	}
	
	function clicked(url){
		if(!popupShow){
			var id = url.split("-");
			
			activeProject = id[0];
			activeTemplate = id[1];
			
			for (i=1;i<=9;i++)
			{
				$('#basicBoard' + i).flightboard("stop");
			}
			
			
		 	$("." + id[1]).fadeIn("fast", function(){
		 		popupShow = true;
		 		currentPorto = 1;
		 		$("#" + id[0]).fadeIn("fast");
		 		startInterval();
		 	}); 	
		 } 		
	}
	
	function startInterval(){
		myInterval = setInterval(startSlideShow, 5000);
	}
	
	function startSlideShow() {
		if(currentPorto == 3){
			currentPorto = 1;
			$("#" + activeProject + " > .porto").animate({opacity: 0.25,
			    top: '+=936px'
			  }, 1000, function() {
			    $("#" + activeProject + " > .porto").animate({opacity: 1});
			  });
			
		}else{
			currentPorto++;
	    	$("#" + activeProject + " > .porto").animate({"top": "-=468px"}, "slow");
	    }
	    
	}
	
	
	
	function closePopup(){
		clearInterval(myInterval);
		
		for (i=1;i<=9;i++)
		{
			$('#basicBoard' + i).flightboard("start");
		}
		
		$("#" + activeProject).fadeOut("fast");
		$("." + activeTemplate).fadeOut("fast", function(){
			popupShow = false;
		});
	}
	
	$(".iphone").click(function() {
		closePopup();
	});
	
	$(".ipadver").click(function() {
		closePopup();
	});
	
	$(".ipadhor").click(function() {
		closePopup();
	});
	
	$(".browser").click(function() {
		closePopup();
	});
	
	
	$("#works").mouseleave(function(){
	      	$('#basicBoard' + activeBoard).flightboard('change',{lettersImage: 'public/images/port'+  activeBoard +'-bw.jpg'}, '', 'current', false);
	      	activeBoard = 0;
	});
	
	for (i=1;i<=9;i++)
	{
		$('#basicBoard' + i).flightboard({lettersImage: 'public/images/port' + i +'-bw.jpg'});
	}
	
	// ------------------------------ 1
	$("#basicBoard1").mouseenter(function(){
	      	swapImage(1, 'on');
	});
	$("#basicBoard1").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 2
	$("#basicBoard2").mouseenter(function(){
	      	swapImage(2, 'on');
	});
	$("#basicBoard2").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 3
	$("#basicBoard3").mouseenter(function(){
	      	swapImage(3, 'on');
	});
	$("#basicBoard3").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 4
	$("#basicBoard4").mouseenter(function(){
	      	swapImage(4, 'on');
	});
	$("#basicBoard4").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 5
	$("#basicBoard5").mouseenter(function(){
	      	swapImage(5, 'on');
	});
	$("#basicBoard5").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 6
	$("#basicBoard6").mouseenter(function(){
	      	swapImage(6, 'on');
	});
	$("#basicBoard6").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 7
	$("#basicBoard7").mouseenter(function(){
	      	swapImage(7, 'on');
	});
	$("#basicBoard7").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 8
	$("#basicBoard8").mouseenter(function(){
	      	swapImage(8, 'on');
	});
	$("#basicBoard8").click(function(){clicked(activeUrl);});
	
	// ------------------------------ 9
	$("#basicBoard9").mouseenter(function(){
	      	swapImage(9, 'on');
	});
	$("#basicBoard9").click(function(){clicked(activeUrl);});
		
	
	// static content
	
});

