$(document).ready(function(){
	asset.global();	
	if($("body.pc_Home")[0]){
		asset.homepage();		
	}
	if($("#timelineBox .timeline")[0]){
		asset.timelineSetup();		
	}
	
});

var asset = {};
asset.hptempv = 0;
asset.hpstat = 1;
asset.navobj = null; 
asset.global = function(){
	
	$("#nav a").bind("mouseenter",function(){
		if(!this.className.match("active")){
			if(this.className.match("purple")){
				$(this).animate({"borderColor":"#d3186f"},200); }
			if(this.className.match("green")){
				$(this).animate({"borderColor":"#81d127"},200); }
			if(this.className.match("blue")){
				$(this).animate({"borderColor":"#0075bd"},200); }
			if(this.className.match("orange")){
				$(this).animate({"borderColor":"#ff9a04"},200); }
		}
	});
	$("#nav a").bind("mouseleave",function(){
		if(!this.className.match("active")){
			$(this).animate({"borderColor":"#53465c"},200);
		}
	});
	$(".backToTop").click(function(){
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 400 ); 	
	});
	$(".cbutton, .button").bind("mouseenter",function(){
		$(this).animate({"opacity":".7"},150);
	});
	$(".cbutton, .button").bind("mouseleave",function(){
		$(this).animate({"opacity":"1"},150);
	});
	
	asset.specialList();
	asset.Tstories();
	form.setUpContact();
	asset.Lists();
}

asset.Lists = function() {
	var rppCname ="";
	if($(".tabbedList")[0]){
		rppCname = $(".tabbedList")[0].className.replace("tabbedList","").replace(" ","").substring(4);	
	}
	//Upper Tab controls
	$(".tabbedList #aTabs a").click(function(){
		var olist = "#"+$(".tabbedList #aTabs a.active")[0].href.split("#")[1];
		$(".tabbedList #aTabs a").removeClass("active");
		$(this).addClass("active");
		$(".tabbedList #aTabs")[0].className = "c"+this.href.split("#")[1];
		
		if(!$(".tabbedList #aTabs")[0].className.match("switchingtabs")){
			//Fancy Tab Switch
		$(".tabbedList #aTabs").addClass("switchingtabs");
		DoSwitch1(olist,"#"+this.href.split("#")[1],($(olist+" .item").length-1),0,500);
		}
		
		return false;
	});
	
	//Pagination for Tabs:
	var tslideI = 0;
	if($("#newspage .tabbedList #aTabs a").length>6){
		var total = $(".tabbedList #aTabs a").length;
		//var groups = Math.floor(total/5);
		$(".tabbedList #aTabs a:eq(4)").after("<a href='#' class='next next-only'></a>");
	}
	$(".tabbedList #aTabs a.next").unbind();
	$(".tabbedList #aTabs a.next").click(function(){ 
		doTabSlide(this,1); 
		return false; 
	});	
	
	function TabSlide(obj,dir){
		var newIndex = tslideI +5;
		if(dir == -1){
			newIndex = tslideI -5;
		}
		var n = obj;
		if(newIndex >($(".tabbedList #aTabs a").length-5)){
			newIndex =$(".tabbedList #aTabs a").length-5;
			if($(".tabbedList #aTabs a.prev")[0]){
				newIndex -=	1;
			}
			if($(".tabbedList #aTabs a.next")[0]){
				newIndex -=	1;
			}
		}
		if(newIndex < 0){
			newIndex =0;
		}
		tslideI = newIndex;
		$(".tabbedList #aTabs .next, .tabbedList #aTabs .prev").remove();
		$(".tabbedList #aTabs .slider").animate({"left":"-"+(160*newIndex)+"px"},300,function(){
			
			if(newIndex > 0 & newIndex < ($(".tabbedList #aTabs a").length-5)){
				$(".tabbedList #aTabs a:eq("+(tslideI+4)+")").after("<a href='#' class='next'></a>");
				$(".tabbedList #aTabs a:eq("+tslideI+")").before("<a href='#' class='prev'></a>");
			}else if(newIndex > 0){
				$(".tabbedList #aTabs a:eq("+tslideI+")").before("<a href='#' class='prev prev-only'></a>");
			}else{
				$(".tabbedList #aTabs a:eq("+(tslideI+4)+")").after("<a href='#' class='next next-only'></a>");
			}
			
			$(".tabbedList #aTabs a.next, .tabbedList #aTabs a.prev").unbind();
			$(".tabbedList #aTabs a.next").click(function(){doTabSlide(this,1);return false;});
			$(".tabbedList #aTabs a.prev").click(function(){doTabSlide(this,-1);return false;});
		});	
	}
	 
	function doTabSlide(obj,dir){
		 TabSlide(obj,dir);	
	}
	
	$(".tabbedList").addClass("tL-Js");
	$(".subList").addClass("hide");
	$(".subList:eq(0)").removeClass("hide");
	
	//Lower List controls
	$(".tabbedList .itemlink, .tabbedList .outlink").bind("mouseenter",function(){
		$(this).children(".item_inner").addClass("inhover");
		});
	$(".tabbedList .itemlink, .tabbedList .outlink").bind("mouseleave",function(){
		$(this).children(".item_inner").removeClass("inhover");
		});
	
	$(".tabbedList .itemlink .item_inner").click(function(){ // what if I repaced itemlink with item_inner? can you reassign "this"? doesn't matter.
		//CLOSE if OPEN
		if($(this).parent()[0].className.match("expanded")){
			var a = this;
			$(this).parent().children(".details").slideUp(200,function(){
				$(a).parent().removeClass("expanded");
				$(a).parent().children(".item_inner").children(".moreBtn").children("span").children(".c").addClass("hide");
				$(a).parent().children(".item_inner").children(".moreBtn").children("span").children(".r").removeClass("hide");
			});
		//OPEN when CLOSED	
		}else{ 
			$(this).parent().children(".details").css({"display":"none"});
			$(this).parent().addClass("expanded");
			$(this).parent().children(".item_inner").children(".moreBtn").children("span").children(".r").addClass("hide");
			$(this).parent().children(".item_inner").children(".moreBtn").children("span").children(".c").removeClass("hide");
			$(this).parent().children(".details").slideDown(200);
		}
		return false;
	});
	
	function DoSwitch1(olist,list,index,type,dur){
		var speed = 60;
		if(type==0){
			/*speed = dur / ($(olist+" .item").length+4);
			$(olist+" .item:eq("+index+")").animate({"opacity":"0"},(speed*4),function(){
				$(this).css({"display":"none"});
			});
			$.doTimeout(speed,function() { 
				index -= 1;
				DoSwitch2(olist,list,index,type,dur);
			});*/
			//DO NOT APPLY FIRST FADEOUT.
			$(olist+" .item").css({"display":"none"});
			index = -1;
			DoSwitch2(olist,list,index,type,dur);
		}else if(type==1){ 
			speed = dur / ($(list+" .item").length+4);
			$(list+" .item:eq("+index+")").css({"display":"block","opacity":"0"});
			$(list+" .item:eq("+index+")").animate({"opacity":"1"},(speed*4),function(){});
			$.doTimeout(speed,function() { 
			  index += 1;	
			  DoSwitch2(olist,list,index,type,dur);
			});
		}
	}
	
	function DoSwitch2(olist,list,index,type,dur){
		if(type==0){
			if(index ==-1){
				type = 1;	
				$(list).removeClass("hide");
				$(olist).addClass("hide");
				$(list+" .item").css({"display":"none"});
				$(olist+" .item").css({"display":"none"});
				index = 0;
				DoSwitch1(olist,list,index,type,dur);
			}else{
				DoSwitch1(olist,list,index,type,dur);
			}
		}else if(type==1){
			if(index == $(list+" .item").length){
				type=2;//Stops here;
				$(".tabbedList #aTabs").removeClass("switchingtabs");
				if($("#newspage")[0]){
					Pagination(list);
				}
			}else{
				DoSwitch1(olist,list,index,type,dur);
			}
			
			if(index == ($(list+" .item").length-1)){
				$(".tabbedList #aTabs").removeClass("switchingtabs");	
			}
		}
	}
	if($("#newspage")[0]){
		Pagination("#"+$(".tabbedList .subList:eq(0)")[0].id);
	}
	//Item List Pagination
	function Pagination(list){
		var pgThisList = list;
	 $(".tabsNavigation").remove();
	  if($(list+" .item").length > rppCname){
		$(".tabbedList").after('<div class="tabsNavigation"><a href="#" class="next">Older</a><a href="#" class="prev">Newer</a><span></span></div>');
		var egc = $(list+" .item")[($(list+" .item").length -1)].className;
		var endGroup = egc.substring(egc.indexOf("grp_"));
		if(endGroup.match(" ")){
			endGroup = endGroup.substring(0,endGroup.indexOf(" "));
		}
		endGroup = endGroup.substring(4);

		for(var i=0;i<(parseInt(endGroup,10)+1);i++){
			if(i==0){
				$(".tabsNavigation span").append("<a href='#"+(i+1)+"' class='b'>"+(i+1)+"</a>");
			}else{
				$(".tabsNavigation span").append("<a href='#"+(i+1)+"'>"+(i+1)+"</a>");
			}
		}
		var ePagI = 0;
		pagSw(ePagI);
		$(".tabsNavigation .prev").click(function(){  
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 250}, 300 ); 
			ePagI = ePagI-1;
			if(ePagI == -1){
				ePagI = 0;	
			}
			pagSw(ePagI);
			return false;
		});
		$(".tabsNavigation .next").click(function(){  
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 250}, 300 );
			ePagI = ePagI+1;
			if(ePagI > endGroup){
				ePagI = endGroup;
			}else{
				pagSw(ePagI);	
			}
			return false;
		});
		$(".tabsNavigation span a").click(function(){  
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 250}, 300 );
			ePagI = (this.href.split("#")[1])-1;
			pagSw(ePagI);
			return false;
		});
	  }
	  
	  function pagSw(ni){
			$(pgThisList+" .item").css({"display":"none"});
			$(pgThisList+" .grp_"+ni).css({"display":"block"});
			$(".tabsNavigation span a").removeClass("b");
			if(ni > endGroup){
				ni--;	
			}/**/
			//alert(endGroup);
			$(".tabsNavigation span a[href~='#"+(ni+1)+"']").addClass("b");
	  }
	}
	
	
}

asset.Tstories = function() {
	/*$(".tStories").each(function(){
		var a = this;
		$(this).parent().append("<div class='controls Tcontrols'><a href='#' class='prev'>Prev</a>&nbsp;<span>1 of "+$(this).children("li").length+" testimonials</span>&nbsp;<a href='#' class='next'>NEXT</a></div>");
		this.title="0";
		$(this).children("li").addClass("hide");
		$(this).children("li:eq(0)").removeClass("hide");
		$(this).parent().children(".controls .next").click(function(){
			
		});
		$(this).parent().children(".controls .prev").click(function(){
			
		});
		
	});*/
	$(".tStories div").click(function(){
		if($(this)[0].className.match("hidequote")){
			$(this).children("blockquote").slideDown(300);
			$(this).removeClass("hidequote");
		}else{
			$(this).children("blockquote").slideUp(300);
			$(this).addClass("hidequote");
		}
	});
		
	
}

asset.timelineSetup = function(){
	var index = 0;
	var bLngval = 158;
	var total = $(".timeNav a.num").length;
	var across = 5;
	
	$(".timeNav a.num").click(function(){
		if(!this.className.match("disable")){
		  $(".timeNav a.num").removeClass("active");
		  $(this).addClass("active");
		  timelineSlide(this.href.split("#")[1]);
		  timelineReplace("tdp_"+this.href.split("#")[1]);	
		}return false;	
	});
	$(".timeline .next").click(function(){
		if(!this.className.match("disable")){
			timelineSlide(index+across);//parseInt(this.href.split("#")[1],10)
		}return false;	
	});
	$(".timeline .prev").click(function(){
		if(!this.className.match("disable")){
			timelineSlide(index-across);
		}return false;	
	});
	
	function timelineSlide(dex) {
		$(".timeline .arrow, .timeNav a").addClass("disable");
		if(index>total-across){
			index= total-across;	
		}else if(index<0){
			index = 0;	
		}
		index = dex;	
		
		//center list of 5 basedon on Index
		dex = dex-(Math.floor(across/2));		
		if(dex>total-across){
			dex= total-across;	
		}else if(dex <0){
			dex = 0;	
		}
	  	//make timeline move based on the index &then fade in/out content for new stuff
	  	$(".timeNav .slider").animate({"left":"-"+(dex*bLngval)+"px"},800,"easeOutCirc",function(){
			$(".timeline .arrow, .timeNav a").removeClass("disable");	
		});
  	}
  	function timelineReplace(id) {
		$(".tditem").animate({"opacity":"0"},400,function(){
			$(".tditem").css({"display":"none"});
			$("#"+id).css({"display":"block"});
			$("#"+id).animate({"opacity":"1"},200);
		});
  	}
}



asset.homepage = function(){
	$(".whatsnew").append('<a class="arrow arup imgLink" href="#0">up</a><a class="arrow ardown imgLink" href="#1">down</a>');
	for(var i=0;i<$(".whatsnew .moduleItem").length;i++){
		if(i==0){
			$(".whatsnew .moduleItem:eq("+i+")").addClass("active");
		}
		$(".whatsnew .moduleItem")[i].id = "mi"+i;
	}	
	$(".whatsnew .moduleItem").addClass("hide");
	$(".whatsnew .active").removeClass("hide");
	$(".whatsnew .arrow").click(function(){
		asset.hpstat = 0;
		//if(!this.className.match("disable")){
			var val = parseInt(this.href.split("#")[1],10);
			if(val==0){
				val = -1;
			}
			var currValue = parseInt($(".whatsnew .active")[0].id.split("mi")[1],10);
			asset.hptempv = loop(currValue+val,$(".whatsnew .moduleItem").length-1);
			asset.h_whatsNew();

		//}
	});
	var t = self.setInterval("asset.runhwn()",6000)
	//setTimeout(function() { if (!IsMouseOver(oi)) $(oi).fadeOut(); }, 100);
}

asset.specialList = function(){
	$(".specialList li a").bind("mouseenter",function(){
		var obj = this;
		$(obj).children(".text").css({"color":"#fff"});
		$(this).children(".bgColor").animate({"height":"110px"},200);
	});	
	$(".specialList li a").bind("mouseleave",function(){
		var obj = this;
		$(obj).children(".text").css({"color":"#43344e"});
		$(this).children(".bgColor").animate({"height":"6px"},200);
	});	
}

asset.runhwn = function(){
	if(asset.hpstat==1){
		asset.hptempv = loop(parseInt($(".whatsnew .active")[0].id.split("mi")[1],10)+1,$(".whatsnew .moduleItem").length-1);
		asset.h_whatsNew();
	}
}

asset.h_whatsNew = function(){
	$(".whatsnew .arrow").removeClass("disable");
	$(".whatsnew .active").animate({"opacity":"0"},200,function(){
		$(".whatsnew .active").removeClass("active");
		$(".whatsnew .moduleItem").addClass("hide");		
		$(".whatsnew #mi"+asset.hptempv).addClass("active").css({"opacity":"0"}).removeClass("hide");
		$(".whatsnew #mi"+asset.hptempv).animate({"opacity":"1"},200,function(){});
		/*if(asset.hptempv == ($(".whatsnew .moduleItem").length-1)){
			$(".whatsnew .ardown").addClass("disable");
		}else if(asset.hptempv == 0){
			$(".whatsnew .arup").addClass("disable");
		}*/
	});	
}

/* NO NAMESPACES */
function getQueryVal(query,param)
{	
	var par = param+"=";
	var st = query.indexOf(par);
	var sl = par.length;
	
	var en = query.indexOf("&",st);
	var fv ="";
	if(query.indexOf("&",st) >-1){
		fv = query.substring(st+sl,en);
	}else{ fv = query.substring(st+sl); }

	return fv;
}

function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/");
}

function getWinSize(){
	var win = [800,800]; // [width,height]
	if (document.body && document.body.offsetWidth) {
	 win = [document.body.offsetWidth,document.body.offsetHeight];
	}
	if (document.compatMode=='CSS1Compat' &&
		document.documentElement &&
		document.documentElement.offsetWidth ) {
	 win = [document.documentElement.offsetWidth,document.documentElement.offsetHeight];
	}
	if (window.innerWidth && window.innerHeight) {
	 win = [window.innerWidth,window.innerHeight];
	}
	return win;
}

function loop(val,end){
	if(val<0){
		return end;	
	}else if(val>end){
		return 0;	
	}else{
		return val;	
	}
}
