$(document).ready(function(){
	var NR_PROJECTS = 0;
	var PROJECTS_CLICK = 0;
	var PROJECTS_POS = 0;
	
	/* Company profile page */
	var current2 = $("#company-profile");
	
	/* Services */
	var current = $("#design-development");
	
	/* Auto scroller */
	AutoScroller = {
		AUTO_SCROLLER: false,
		start: function() {
			if (!this.AUTO_SCROLLER) {
				this.AUTO_SCROLLER = true;
				$(document).everyTime(4000, function(){
					if (PROJECTS_CLICK < (NR_PROJECTS - 1)) {
						PROJECTS_CLICK++;
						PROJECTS_POS = (PROJECTS_POS == 0) ? -360 : (PROJECTS_POS - 360);
						$("#projects_wrapper div.scroller").animate({
							top: PROJECTS_POS + "px"
						});
						$(".project_preview").hide();
					}
					else {
						PROJECTS_CLICK = 0;
						PROJECTS_POS = 0;
						$("#projects_wrapper div.scroller").animate({
							top: "0px"
						});
					}
				});
			}	
		},
		
		stop: function(){
			this.AUTO_SCROLLER = false;
			$(document).stopTime();
		}
	}
	
	/* Page Loader */
	PageLoader = {
		MENU_INDEX: 0,
		GOTO: null,
		
		setIndex: function(index) {
			this.MENU_INDEX = index;
		},
		getIndex: function() {
			return this.MENU_INDEX;
		},
		setGoto: function(_goto){
			this.GOTO = _goto;
		},
		getGoto: function() {
			return this.GOTO;
		},
		getPage: function() {
			var A = $("#footer-navigator a")[this.MENU_INDEX];
			var HREF = $(A).attr("href");
			switch(this.MENU_INDEX) {
				case 3:
					if ( $("#clients_content").children().size() == 0) {
						$.get(HREF, function(data){
							$("#clients_content").append(data);
						});
					}
					break;
					
				case 2:
					if (NR_PROJECTS == 0) {
						$.get(HREF, function(data){
							$("#projects_content div.scroller").append(data);
							NR_PROJECTS = $("#projects_wrapper div.scroller").children().size();
							$("#projects_content div.scroller").css({
								height: (NR_PROJECTS * 370) + "px"
							});
							PageLoader.gotoProject();
							
							AutoScroller.start();
						});
					} else {
						if (PageLoader.getGoto() == null) 
							AutoScroller.start();
						else 
							PageLoader.gotoProject();
					}
			}
		},
		
		gotoProject: function() {
			if (PageLoader.getGoto() != null) {
				var index = 0;
				
				AutoScroller.stop();
				
				$("#projects_wrapper div.scroller").children().each(function(){
					if ( $(this).attr("id") == PageLoader.getGoto()) {
						$("#menu-hover").animate({ top: ((2 * 45) + 20) + "px" }, 500);
						$("#content-wrapper").animate({ top: "-" + (2 * 418) + "px" }, 500);
						PROJECTS_POS = 0;
						PROJECTS_CLICK = index + 1;
						PROJECTS_POS -= (360 * index);
						$("#projects_wrapper div.scroller").animate({
							top: PROJECTS_POS + "px"
						});
						PageLoader.setGoto(null);
					}
					
					index++;
				});
				
			}
		}
	}
	
	/* Footer Positioning */
	$("#wrapper").css({
		height: ( ($(window).height() < 750) ? "750px" : $(window).height()  + "px" )
	});
	
	$(window).resize(function(){
		$("#wrapper").css({
			height: ( ($(window).height() < 750) ? "750px" : $(window).height() + "px" )
		});	
	});
	
	/* Main Menu */
	$("#menu li").click(
		function() {
			PageLoader.setIndex( $("#menu li").index(this) );
			PageLoader.getPage();
			
			if ( $("#content-wrapper").css("left") != "0px" ) {
				$("#project-list").remove();
				$("#content-wrapper").animate({ left: "0px" });
			}
			
			/* Company profile reset */
			$(current2).hide(function(){ $("#company-profile").show(); current2 = $("#company-profile") });
			
			/* Executive Profile reset */
			$("#coo").hide(function(){ $("#ceo").show(); });
			
			/* Services reset */
			$(current).hide(function(){ $("#design-development").show(); current = $("#design-development") });
			
			/* Bubble hack */
			if (PageLoader.getIndex() == 4)
				$("#bubble").oneTime(1000, function(){
					if (!$.browser.msie) {
						$("#bubble").css({
							top: "185px",
							opacity: "0.0",
							display: "block"
						}).animate({
							top: "155px",
							opacity: "1.0"
						}, function(){
							$(this).oneTime(5000, function(){
								$("#bubble").fadeOut("slow");
							})
						});
					} else {
						$("#bubble").css({ display: "block", top: "155px" }).oneTime(5000, function(){
							$(this).css({ display: "none" });
						})
					}
				});
				
			/* Map display reset */
			if ( $("#map_canvas").css("visibility") == "visible" ) {
				$("#map_canvas").css({ visibility: "hidden" });
				$("#map_canvas_close").css({ display: "none" });
			}
		
	 		$("#menu-hover").animate({ top: ((PageLoader.getIndex() * 45) + 20) + "px" }, 500);
			$("#content-wrapper").animate({ top: "-" + (PageLoader.getIndex() * 418) + "px" }, 500);
		}
	);
	
	$("#footer-navigator a").click(
		function() {
			PageLoader.setIndex( $("#footer-navigator a").index(this) );
			PageLoader.getPage();
			
			if ( $("#content-wrapper").css("left") != "0px" ) {
				$("#project-list").remove();
				$("#content-wrapper").animate({ left: "0px" });
			}
			
			/* Company profile reset */
			$(current2).hide(function(){ $("#company-profile").show(); current2 = $("#company-profile") });
			
			/* Executive Profile reset */
			$("#coo").hide(function(){ $("#ceo").show(); });
			
			/* Services reset */
			$(current).hide(function(){ $("#design-development").show(); current = $("#design-development") });
			
			/* Bubble hack */
			if (PageLoader.getIndex() == 4)
				$("#bubble").oneTime(1000, function(){
					if (!$.browser.msie) {
						$("#bubble").css({
							top: "185px",
							opacity: "0.0",
							display: "block"
						}).animate({
							top: "155px",
							opacity: "1.0"
						}, function(){
							$(this).oneTime(5000, function(){
								$("#bubble").fadeOut("slow");
							})
						});
					} else {
						$("#bubble").css({ display: "block", top: "155px" }).oneTime(5000, function(){
							$(this).css({ display: "none" });
						})
					}
				});
			
			/* Map display reset */
			if ( $("#map_canvas").css("visibility") == "visible" ) {
				$("#map_canvas").css({ visibility: "hidden" });
				$("#map_canvas_close").css({ display: "none" });
			}
		
	 		$("#menu-hover").animate({ top: ((PageLoader.getIndex() * 45) + 20) + "px" }, 500);
			$("#content-wrapper").animate({ top: "-" + (PageLoader.getIndex() * 418) + "px" }, 500);
			
			return false;	
		}
	);
	
	$("#up").click(function(){
		AutoScroller.stop();
		if (PROJECTS_CLICK > 0) {
			PROJECTS_CLICK--;
			PROJECTS_POS = (PROJECTS_POS == 0) ? 0 : (PROJECTS_POS + 360);
			$("#projects_wrapper div.scroller").animate({
				top: PROJECTS_POS + "px"
			});
			$(".project_preview").hide();
		}
	});
	
	$("#down").click(function(){
		AutoScroller.stop();
		if (PROJECTS_CLICK < (NR_PROJECTS - 1)) {
			PROJECTS_CLICK++;
			PROJECTS_POS = (PROJECTS_POS == 0) ?  -360 : (PROJECTS_POS - 360);
			$("#projects_wrapper div.scroller").animate({
				top: PROJECTS_POS + "px"
			});
			$(".project_preview").hide();
		}
	});
	
	
	/* Qucik Contact */
	$("#quick-contact").hover(
		function() {
			$(this).removeClass("quick_contact_normal").addClass("quick_contact_hover");
		},
		function() {
			$(this).removeClass("quick_contact_hover").addClass("quick_contact_normal");
		}
	).click(function(){
		$.get("quickcontact", function(data){
			$.popup(data, "Quick Contact");
		});
	});
	
	$("#contact-us").click(function(){
		$.get("quickcontact", function(data){
			$.popup(data, "Quick Contact");
		});
	});
	
	/* Locate on Map */
	$("#locate-on-map").click(function(){
		$("#map_canvas").css({ top: "-656px", visibility: "visible" }).animate({ top: "17px" }, function(){
			$("#map_canvas_close").fadeIn();
		});
		return false;
	});
	
	$("#locate-on-map2").click(function(){
		$("#map_canvas2").css({ top: "-656px", visibility: "visible" }).animate({ top: "17px" }, function(){
			$("#map_canvas_close2").fadeIn();
		});
		return false;
	});
	
	$("#map_canvas_close").hover(function(){
		$(this).css({ textDecoration: "underline", cursor: "pointer" });
	}, function(){
		$(this).css({ textDecoration: "none" });
	}).click(function(){
		//console.log("click");
		$(this).fadeOut(100,function(){
			$("#map_canvas").animate({ top: "-656px" }, function(){
				$(this).css({ visibility: "hidden" });
			});
		});
	});
	
	$("#map_canvas_close2").hover(function(){
		$(this).css({ textDecoration: "underline", cursor: "pointer" });
	}, function(){
		$(this).css({ textDecoration: "none" });
	}).click(function(){
		//console.log("click");
		$(this).fadeOut(100,function(){
			$("#map_canvas2").animate({ top: "-656px" }, function(){
				$(this).css({ visibility: "hidden" });
			});
		});
	});
	
	/* List Projects */
	$("#list-projects").click(function(){
		$.get("projects/small", function(data){
			$("#content-wrapper").animate({ left: "-700px" }, function(){
				$("#content-container").append(data);
			});
		});
		return false;
	});
	
	/* Company */
	$(".executive-profile-page").click(function(){
		var element = current2;
		current2 = $("#executive-profile");
		$(element).fadeOut(function(){
			$(current2).fadeIn();
		});
		return false;
	});
	
	$(".company-profile-page").click(function(){
		var element = current2;
		current2 = $("#company-profile");
		$(element).fadeOut(function(){
			$(current2).fadeIn();
		});
		return false;
	});
	
	$(".geographical-data-page").click(function(){
		var element = current2;
		current2 = $("#geographical-data");
		$(element).fadeOut(function(){
			$(current2).fadeIn();
		});
		return false;
	});
	
	/* Services */
	$("a.design-development-page").click(function(){
		element = current;
		current = $("#design-development");
		$(element).fadeOut(function(){
			$("#design-development").fadeIn();
		});
		
		return false;
	});
	
	$("a.core-competencies-page").click(function(){
		element = current;
		current = $("#core-competencies");
		$(element).fadeOut(function(){
			$("#core-competencies").fadeIn();
		});
		
		return false;
	});
	
	$("a.key-benefits-page").click(function(){
		element = current;
		current = $("#key-benefits");
		$(element).fadeOut(function(){
			$("#key-benefits").fadeIn();
		});
		
		return false;
	});
	
	/* Slogan */
	$("#slogans").cycle({
		fx: 'fade',
		speed: 500,
		timeout: 4000
	});
	
	/* CEO, COO */
	$("#ceo").click(function(){
		$(this).slideToggle(function(){ $("#coo").slideToggle(); });
		return false;
	});
	
	$("#coo").click(function(){
		$(this).slideToggle(function(){ $("#ceo").slideToggle(); });
		return false;
	});

});