/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options=$.extend({},options);options.expires=-1;}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}expires='; expires='+date.toUTCString();}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}return cookieValue;}};

/* SSP */
var homeTimeout = null;
var msgTimeout = null;
var quoteTimeout = null;
var quoteTimeout2 = null;
var msgNum = 1;
var csHoverState = null;
var numberOfBusinessAreas = 0;
var solutionsBoxWidth = 0;
var urlToOpen = "";
var lastQuote = 0;
$(document).ready(function(){
    numberOfBusinessAreas = $(".business-area").length;
    solutionsBoxWidth = Math.floor(928/numberOfBusinessAreas);
    $(".cs-selected").hover(countryHover,countryHoverOut);
    $(".business-area").hover(highlightThis,resumeCycle);
    if($("#solutions-selected").length>0){homeTimeout = setTimeout(cycleSections,10000);$(".business-area:eq(0)").addClass("selected");}
    if($(".customer-list li").length>1){
	  setInterval(cyclePartners,7000);
    }

    if($(".industry-issues li").length>1){
	  setInterval(industryCycle,10000);
    }
	var dlg = $("#docform").dialog({ autoOpen: false, modal: true, width:360 });
	dlg.parent().appendTo($("form:first"));
    $(".modallink").click(modallink);
/*
   if($(".msg1").length>0){
	cycleMessages(1);
	}
*/
if( $(".quote").length>0 )
{
	cycleQuotes();
}
   $("#docform .FormButton").click(function(){
	$.cookie('enteredInfo', 'true');
	window.open(urlToOpen,"_blank");
   });
});

function countryHover(){
    
    if(csHoverState!=true)
    {
        $("#cs-pop").fadeIn();
        csHoverState = true;
    }
}
function countryHoverOut(){
    csHoverState = false;
    setTimeout(delayFadeOut,500);
}

function delayFadeOut(){
    if(csHoverState==true || csHoverState==true)
    {
        setTimeout(delayFadeOut,500);
    }
    else{
        $("#cs-pop").fadeOut();
        csHoverState=false;
    }
}

function cycleSections(){
    clearTimeout(homeTimeout);
    var currentPos = returnLeftNum($("#solutions-selected"));
    var newPos = currentPos + solutionsBoxWidth;
    if(newPos>925){
        newPos = 1;
    }   
    //$("#solutions-fade").fadeIn();
    $("#solutions-selected").animate({'left':newPos+"px"},1000);
    //$("#solutions-fade").fadeOut();
    $("#business-areas .selected").removeClass("selected");
    var child = Math.floor((newPos-1)/solutionsBoxWidth);
    $(".business-area:eq("+child+")").addClass("selected");
    homeTimeout = setTimeout(cycleSections,10000);
}

function cycleQuotes(){
	clearTimeout(quoteTimeout2);
	clearTimeout(quoteTimeout);
	$(".quote").html(randomQuote());
	$(".quote").fadeIn();
	quoteTimeout2 = setTimeout(function(){
		$(".quote").fadeOut();
		quoteTimeout = setTimeout(cycleQuotes,2000);
	},8000);
	
}

function returnLeftNum(elem){
    var currentPos = elem.css("left");
    currentPos = parseInt(currentPos.split("px")[0],10);
    return currentPos;
}
function highlightThis(){
    clearTimeout(homeTimeout);
    var newPos = $(this).position().left;
    if(returnLeftNum($("#solutions-selected"))!=newPos)
    {
        //$("#solutions-fade").fadeIn();
        $("#solutions-selected").animate({'left':newPos+"px"},500);
        //$("#solutions-fade").fadeOut();
        $("#business-areas .selected").removeClass("selected");
        var child = Math.floor((newPos-1)/solutionsBoxWidth);
        $(".business-area:eq("+child+")").addClass("selected");
    }
}
function resumeCycle(){
    clearTimeout(homeTimeout);
    homeTimeout = setTimeout(cycleSections,10000);
}

function cyclePartners(){
    $(".customerSelected").removeClass("customerSelected");
    $(".customerNext").removeClass("customerNext");
    $(".customer-list li:first").addClass("customerSelected");
    $(".customer-list li:eq(1)").addClass("customerNext");
    $(".customer-list .customerSelected").fadeOut("slow").appendTo(".customer-list").show();
    $(".customer-list .customerNext").show()
}

function cycleMessages(){
	clearTimeout(msgTimeout);
	$(".msg1,.msg2,.msg3").fadeOut();
	switch(msgNum){
		case 1: {
				
				$(".msg3").fadeOut();
				$(".msg1").fadeIn();
				msgNum=2;
				msgTimeout = setTimeout(cycleMessages,10000);
				break;
			}
		case 2: {
				$(".msg1").fadeOut();
				$(".msg2").fadeIn();
				msgNum=3;
				msgTimeout = setTimeout(cycleMessages,10000);
				break;
			}
		case 3: {
				$(".msg2").fadeOut();
				$(".msg3").fadeIn();
				msgNum=1;
				msgTimeout = setTimeout(cycleMessages,10000);
				break;
			}
	}
	
	
}

function industryCycle(){
	$(".industry-issues .active").removeClass("active");
    	$(".industry-issues .next").removeClass("next");
	$(".industry-issues li:first").addClass("active");
    	$(".industry-issues li:eq(1)").addClass("next");
	$(".industry-issues .active").fadeOut("slow", function(){
		$(".industry-issues .next").show();
		$(this).appendTo(".industry-issues ul");
	});
    	
}

function submitTheForm(name,coname,tel,country,size){
	$("#flashform :input")[0].value = name;
	$("#flashform :input")[1].value = coname;
	$("#flashform :input")[2].value = tel;
	$("#flashform :input")[3].value = country;
	$("#flashform :input")[4].selectedIndex = size;
	$("#flashform .FormButton")[0].click();
}

function modallink(){
	if($.cookie('enteredInfo')=="true")
	{
		this.target = "_blank";
		return true;
	}
	else{
		urlToOpen = this.href;
		$("#docform").dialog('open');
		return false;
	}
}

function fmodallink(url){
	if($.cookie('enteredInfo')=="true")
	{
		window.open(url,"_blank");
	}
	else{
		urlToOpen = url;
		$("#docform").dialog('open');
	}
}

function randomQuote(){
	var quoteArr = new Array();
	quoteArr[0] = ["We chose SSP because the breadth of functionality could support our business.  SSP implemented the system very quickly and met its commitments.","Valiant Insurance"];
	quoteArr[1] = ["SSP has been a great partner of ours with a knowledgeable and professional support staff and their system has been one of the reasons for our growth.","Best Practices Insurance Services"];
	quoteArr[2] = ["SSP listened to the needs of Island Heritage, were flexible and delivered an integrated solution. SSP is a key partner and will play an integral role in our future development plans.","Island Heritage"];
	quoteArr[3] = ["We needed a complete administration system that would enable us to quickly react to changing market conditions, and make better informed decisions to closely track our business performance.","PLICO"];
	var rand = Math.round(Math.random()*3);
	if(lastQuote==rand)
	{
		rand = (rand+1)%4;
	}
	lastQuote = rand;
	var quoteString = "<p class='quotetext'>"+quoteArr[rand][0]+"</p><p class='quotee'>"+quoteArr[rand][1]+"</p>";
	return quoteString;
}
