/*
 *	Alert JQuery Functions
 *	Author: Alan Agius
 *
 *	Last Updated: 21st October 09
 *	Copyright 2009 Alert Communications
 *	www.alert.com.mt
 */
 
//Declare Variables (Used for Banners)
var intDuration = 700,
	intCurrentIndex = -1,
	intWorkCurrentIndex = -1,
	intTotalWorks = 0,
	timeout = null,
	blnTransitionReady = true,
	strSpanClass = "image-holder",
	strLoader = "<div class=\"loading-bar\"></div>",
	strSpanObj = null,
	strBillboardClass = ".mpBillboardContainer";

//Change banner function
function changeBanner(intCurrentItem, strCategory){
	var strSpanId = strCategory + "-image-" + intCurrentItem;
	
	if (strCategory === "works"){
		$(".mpBillboardPagination ul > li > a").removeClass("active");
		$(".mpBillboardPagination ul > li:eq("+ intCurrentItem +") > a").addClass("active");
	}
	
	//Check if last banner is the same as the last one
	if(strSpanId != $(strBillboardClass + "> span:first").attr("id")){
		blnTransitionReady = false;
		//Get Image Paths and Alt Tag from XML file
		$.ajax({type: "GET", url: "xml/alertMPBanners.xml", dataType: "xml", success: function(xml){
			var strImagePath = $(xml).find(strCategory +" > item:eq("+ intCurrentItem +") > image").text(),
				strImageAlt = $(xml).find(strCategory +" > item:eq("+ intCurrentItem +") > alt").text(),
				strImageLink = $(xml).find(strCategory +" > item:eq("+ intCurrentItem +") > link").text();
			
			strSpanObj = "<span class=\""+ strSpanClass +"\" id=\""+ strSpanId +"\">";
			strSpanObj += "<a href=\""+ strImageLink +"\"></a>";
			strSpanObj += "</span>";
			
			//Append new banner
			$(strBillboardClass).append(strSpanObj);				

			var objImage = new Image();
			$(objImage).attr("src", strImagePath).attr("alt", strImageAlt)
			
			//200 millisecond to see if image is cached, Show preloader is not cached
			var objLoaderTimeout = setTimeout("$(strBillboardClass).append(strLoader);", 200);

			$(objImage).load(function(){
			clearTimeout(objLoaderTimeout);
			$(".loading-bar").remove(); //remove loader
			//Animate banner
				$("#" + strSpanId + " a").append($(objImage)).fadeIn(intDuration, function(){
					//Remove holder class and remove previous image holder
					$("#" + strSpanId).removeClass("image-holder");
					$(strBillboardClass + "> span:first").remove();
					blnTransitionReady = true;
				});
			});
		}});
	}else{
		return false;
	}
}

function intervalAnimator(){
	if(intTotalWorks == intWorkCurrentIndex){
		intWorkCurrentIndex = 0;
	}else{
		intWorkCurrentIndex++;
	}
	changeBanner(intWorkCurrentIndex, "works");
}

$(document).ready(function(){
	intTotalWorks =  $(".mpBillboardPagination ul > li").length-1;
	
	//Load Deafult Banner
	intervalAnimator(); 
	
	//Top Icons onClick function
	$(".headerIcons > ul > li").hover(function(){
	clearInterval(objIntervaLAnimator);
		if(blnTransitionReady){
			intCurrentIndex = $(this).children("a").attr("rel") - 1,
			changeBanner(intCurrentIndex, "icons");
		}
	});

	//Work Numbers onClick function
	$(".mpBillboardPagination > ul > li").click(function(){
	clearInterval(objIntervaLAnimator);
		if(blnTransitionReady){
			intWorkCurrentIndex = $(this).children("a").attr("rel") - 1,
			changeBanner(intWorkCurrentIndex, "works");
		};
	});
	
	//Work Arrow Previous onClick function
	$("#btn-previous-work").click(function(){	
	clearInterval(objIntervaLAnimator);
		if((intWorkCurrentIndex <= intTotalWorks) &&(intWorkCurrentIndex >= 1)&&(blnTransitionReady)){
			intWorkCurrentIndex --;
			changeBanner(intWorkCurrentIndex, "works");
		};
	});

	//Work Arrow Next onClick function
	$("#btn-next-work").click(function(){
	clearInterval(objIntervaLAnimator);
		if((intWorkCurrentIndex < intTotalWorks)&&(blnTransitionReady)){;
			intWorkCurrentIndex ++;
			changeBanner(intWorkCurrentIndex, "works");
		};
	});
	
	//Auto animator
	var objIntervaLAnimator = setInterval("intervalAnimator()", 6000);
});
//Featured Project Slider
var objSliderItems = null,
	intItemWidth = null,
	intTotalItems = null,
	intCurIndex = 0,
	blnTransReady = true;
	
$(document).ready(function(){
	objSliderItems = $("#featuredProjectsSider > dl dd");
	intItemWidth = objSliderItems.width();
	intTotalItems = objSliderItems.length;
	
	//Next arrow click
	$("#featuredProjectsNext").click(function(){
		if(blnTransReady){
			intCurIndex ++;
			slideProject();
		}
	});
	
	//Previous arrow click
	$("#featuredProjectsPrevious").click(function(){
		if(blnTransReady){
			intCurIndex --;
			slideProject();		
		}
	});
});

// Slide slider
function slideProject(){
	blnTransReady = false;
	if (intCurIndex < 0){
		intCurIndex = 0;
	}else if (intCurIndex >= intTotalItems){
		blnTransReady = true;
		intCurIndex = intTotalItems - 1;
		return false;
	}
	
	intPosition = - (intItemWidth * intCurIndex);
	$("#featuredProjectsSider > dl").animate({left:intPosition}, 650, function(){blnTransReady = true});
}

//Modale Box
(function($){
//Generate Modale Box
$.fn.modaleBox = function(options){
	//Disable window scroll and Enable resize center function
	$(window).bind('scroll', function(){
		$().modaleBox.Center();
	}).bind('resize', function(){
		$().modaleBox.Center();
	})
	 
	
	//Create container if it's not hidden
	if(!!!$("#"+options.dialogueID + "-holder").length){
		strBodyHTML = "<div id=\""+ options.containerID +"\" style=\"display:none;opacity: 0.0\"></div>";
		strBodyHTML += "<div id=\""+ options.dialogueID +"-holder\" style=\"position:absolute; z-index:999; display:none\">";
		strBodyHTML += "<div id=\""+ options.closeButtonID +"\" style=\"display:none;\"><!-- close button --></div>";
		strBodyHTML += "<div id=\""+ options.dialogueID +"\" style=\"display:none;\"></div>";
		strBodyHTML += "<div id=\""+ options.dialogueID +"-border\" style=\"display:none; background-color:" + options.borderColor + "\"><!-- border --></div>";
		strBodyHTML += "</div>";
		$("body").append(strBodyHTML);
		strBodyHTML = null;
		
		//Close Button
		$("#" + options.closeButtonID, "#"+ options.dialogueID +"-holder").click(function(){
			$().modaleBox.Close();
		});
		
		//Get content
		if(options.contentHTMLID === undefined){
			//Load remote page
			$.ajax({
				url: options.url,
				cache: options.cache,
				success: function(strHTML){
					$("#" + options.dialogueID).html(strHTML);
				}
			});
		}else{
			//Load HTML from hidden element
			$("#" + options.dialogueID).html($("#" + options.contentHTMLID).html());
		}
	}
	
	//Create borders
	$().modaleBox.CreateBorder();	
	
	//Show Modale
	$().modaleBox.Show();
	
}

//Create Dialogue Box Border
$.fn.modaleBox.CreateBorder = function(){
	$("#"+ options.dialogueID +"-holder").css("top","-99999px").css("left","-99999px").show(function(){
		var objDialogue = $("#" + options.dialogueID),
			intBorderDivHeight = 0,
			intBorderDivWidth = 0;
			
			intBorderDivHeight = objDialogue.outerHeight(true) + (options.borderWidth * 2);
			intBorderDivWidth = objDialogue.outerWidth(true) + (options.borderWidth * 2);
			
			$("#"+ options.dialogueID +"-border").fadeTo(0, options.borderOpacity).css("top", 0).css("left", 0).width(intBorderDivWidth).height(intBorderDivHeight);
			
			objDialogue.css("top", options.borderWidth).css("left", options.borderWidth);
	})
}

//Center Dialogue Box
$.fn.modaleBox.Center = function(){
	var objDialogue = $("#" + options.dialogueID),
		intTop = 0,
		intLeft = 0,
		intWindowHeight = $(window).height(),
		intWindowWidth = $(window).width(),
		intScrollY = 0,
		intScrollX = 0,
		strRegExpChrome = new RegExp("chrome");
		
		//Check if browser is Chrome
		if(strRegExpChrome.test(navigator.userAgent.toLowerCase())){
			intScrollY = document.body.scrollTop;
			intScrollX = document.body.scrollLeft;        
		}else{
			intScrollY = document.body.parentNode.scrollTop;
			intScrollX = document.body.parentNode.scrollLeft;
		}

	
	//Set new positions
	intTop = ((intWindowHeight - objDialogue.outerHeight(true)) / 2) + intScrollY;
	intLeft = ((intWindowWidth - objDialogue.outerWidth(true)) / 2) + intScrollX;
	
	$("#" + options.dialogueID + "-holder").css("top", intTop).css("left", intLeft);
	$("#" + options.containerID).css("top", intScrollY).css("left", intScrollX).width(intWindowWidth).height(intWindowHeight);
}

//Show Modale Box
$.fn.modaleBox.Show = function(){
	// Show dialogue box, container, close button and border
	$("#"+ options.dialogueID +"-holder").show(function(){
		$().modaleBox.Center();
		$("#" + options.dialogueID).fadeIn(options.dialogueEasing);	
		$("#"+options.closeButtonID).fadeIn(options.dialogueEasing);	
		$("#"+ options.dialogueID +"-border").fadeIn(options.dialogueEasing);
		$("#"+ options.containerID).show().fadeTo(options.containerEasing, options.containerOpacity);
	})
}

//Close Modale Box
$.fn.modaleBox.Close = function(){
	$("#" + options.dialogueID).fadeOut(options.dialogueEasing);	
	$("#"+ options.dialogueID +"-border").fadeOut(options.dialogueEasing);
	$("#"+options.closeButtonID).fadeOut(options.dialogueEasing);

	$("#" + options.containerID).fadeOut(options.containerEasing, function(){
		if(!!options.hide){
			//Hide Container and Dialogue	
			$(this).fadeTo(0,0);
			$("#"+ options.dialogueID +"-holder").css("top","-99999px").css("left","-99999px").hide()
		}else{
			//Remove Container and Dialogue
			$(this).remove();
			$("#"+ options.dialogueID +"-holder").remove();
		}
	});	
	
	//Enable window scroll and Disable resize center function
	$(window).unbind('scroll').unbind('resize');
}
})(jQuery);

$(document).ready(function() {
	$(".navExtranet", ".headerNavigation").click(function(){
		$().modaleBox(
			options = {
				contentHTMLID: "modale-login-html",
				hide:true,
				containerID: "modale-container-box",
				dialogueID: "modale-dialogue-box",
				closeButtonID: "modale-btn-close",
				opacity: 0.50,
				borderColor: "#000",
		  		borderWidth: 12,
		  		borderOpacity: 0.5,
				containerOpacity:0.2,
		  		containerEasing:350,
		  		dialogueEasing: 500	
			}
		)
	})
});

