/*function initForms(){
	$.validationEngineLanguage.newLang();
	$('form.validateMe').validationEngine({validationEventTriggers:"blur change",promptPosition:"centerRight",alpha:1,topOffset:5,leftOffset:0});	
}*/
function initCommentForm(){
	$('#authorLevelFallback').hide();
	$('#authorLevel').show().children('a').click(function(e){
		$(this).css("background-position", "0 -23px").siblings().css("background-position", "0 0");
		targetValue = $(this).attr("id");		
		$('#authorLevelFallback').children("input[name='level']").each(function(i){
			if($(this).val() == targetValue){
				$(this).attr("checked", "checked"); 
			}		
		});
     	e.preventDefault();
    });
	$("#comment_form input[type='submit']").click(function(e){
		alert(  $("input[name='level']:checked").val()  ); // temp 
     	e.preventDefault();
    });	
}
function initCarousel(){
	// variables
	var source = $('#carousel');
	var fadeSpeed = 500;
	var cycleSpeed = 6000;	
	//hide source html
	source.hide();
	//set up working vars
	var currentCycle = 0;	
	var action = false;
	var initCycle	
	var data  = new Array();	
	source.children('li').each(function(i){
		itemArray = new Array();
		itemArray[0] = $(this).find('a').attr("href");
		itemArray[1] = $(this).find('img');		
		data[i] = itemArray;
	});
	
	//build holding divs
	$('<div id="cycleWindow"></div><img src="../static/images/site/carousel_prev.png" id="carousel_prev" /><img src="../static/images/site/carousel_next.png" id="carousel_next" /></a><div id="paginationButtons"></div>').insertBefore(source);
	//set width of buttons holding div
	$('#paginationButtons').css('width',data.length*15)
	//add buttons
	for(var i=0; i<data.length; i++){
		$('#paginationButtons').append('<a href="#">'+i+'</a>');	

	}
	//load/clone an image from the source
	function loadImage(){
		$('#cycleWindow').append(
			data[currentCycle][1].clone().hide().bind('click',function(){
				window.location.href = data[currentCycle][0];																	   
			}).fadeIn(fadeSpeed,function(){
				action = false;
				$(this).siblings().remove()					
			})
		);
		//set active class on the buttons
		$('#paginationButtons').children("a:contains('"+currentCycle+"')").addClass('active').siblings().removeClass();
		//init cycle
		clearTimeout(initCycle);
		initCycle = setTimeout(function(){cycle(1);},cycleSpeed);
	}
	//cycle through the images
	function cycle(dir){
		if(!action){
			action = true;
			currentCycle += dir;			
			if(currentCycle >= data.length) currentCycle = 0;
			if(currentCycle < 0) currentCycle = data.length-1;	
			loadImage();
		}
	}
	//add event to prev button
	$('#carousel_prev').click(function(e){
		if(!action){						
			cycle(-1);
			e.preventDefault();
		}
	})
	//add event to next button
	$('#carousel_next').click(function(e){
		if(!action){						
			cycle(1);
			e.preventDefault();
		}
	})
	//add event to the buttons
	$('#paginationButtons').children('a').click(function(e){
		if(!action){
			action = true;
			currentCycle = $(this).html()
			loadImage();
			e.preventDefault();
		}
	})	
	//load initial image
	loadImage();
}
function initAgentRollovers(){
	items = $('#agentList').children('li');
	items.children('a').css('display','none');
	$('body').append('<div id="agentTooltip"></div>');
	tooltip = $('#agentTooltip');
	tooltip.hide();
	toolWidth = tooltip.width();
	tooltip.hover(function(){		
		$(this).stop();
	},function(){
	 	tooltip.stop(false,true).fadeOut(500);
	}); 
	items.hover(function(){
		tooltip.empty();
		newPos = $(this).offset();
		//newTop = newPos.top+1;
		//newLeft = newPos.left+142;
		newTop = newPos.top+162;
		newLeft = newPos.left+1;
		if((newLeft+toolWidth) > $(window).width()){
			newLeft = newPos.left-toolWidth;
		}
		tooltip.append($(this).children().clone().css('display','block'));
		tooltip.find('img').remove();
		tooltip.find('h4').html('Agent Details');
		tooltip.stop(false,true).css({'top':newTop,'left':newLeft}).fadeIn(300);
	},function(){
	 	tooltip.stop(false,true).fadeOut(500);
	}); 	
}
var started = false;
startup = function(){
	if(!started){			
		started = true;
		//initForms(); - forms checking cancelled
		if($("#comment_form").length) initCommentForm();		
		if($('#carousel').length) initCarousel();
		if($('#agentList').length) initAgentRollovers();
	};	
};
//$(document).ready(function(){startup();});

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


var webChatPopUp = null;
function startWebChat(urlPrefix, chatEvent) {	
	if (webChatPopUp && !webChatPopUp.closed)
	{		
		webChatPopUp.focus();
	} else {
		var url = urlPrefix + "html/webchat/chat.html?&ce=" + chatEvent + "";
		webChatPopUp = window.open(url,"_blank", "menubar=0,location=0,scrollbars=auto,resizable=1,status=0,width=400,height=490");
	}
}
