$(document).ready(function() {
	
	$(".hidden").slideUp();
	
	$("#openads").bind("click", function() {
		if ($("#theads").css("display") == "none") {
			$("#theads").slideDown();	
		} else {
			$("#theads").slideUp();
		}
		return false;
	});
	
	$(".widget .head a").bind("click", function() {
		$(".sliderContainer").css("width","258px").css("height","85px");
		if ($(this).parent().parent().children(".body").css("display") == "none") {
			$(this).parent().parent().children(".body").slideDown(300, function() {
				updateWidgetCookie();
			});	
		} else {
			$(this).parent().parent().children(".body").slideUp(300, function() {
				updateWidgetCookie();
			});	
		}
		return false;
	});
	
	$("#newsSlider").easySlider({
		controlsBefore:	'<p id="controls">',
		controlsAfter:	'</p>'
	});
	$("#caseSlider").easySlider({
		controlsBefore:	'<p id="controls2">',
		controlsAfter:	'</p>',
		prevId: 'prevBtn2',
		nextId: 'nextBtn2'
	});
	
});

function updateWidgetCookie() {
	
	var data = ''
	
	$(".widget .head a").each(function () {
		var id = $(this).parent().parent().attr("id");
		var state = $(this).parent().parent().children(".body").css("display");
		data += id + "=" + state +";";
	}); 	
	

	Set_Cookie('lwsaWidgetState',data,30);
}

// COOKIE FUNCTIONS
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function gotoStep(step, required) {
	var err = 0;

	if (required) {
		
		var req = required.split(',');
		if (req.length > 0) {
			for (i=0;i<req.length;i++) {
				if ($("input[name="+req[i]+"]:checked").val() == undefined) {
					err++;
				}
			}	
		}
			
	}
	
	if (err > 0) {
		$("#quizerr").show();
	} else {
		$("#quizerr").hide();
		$(".quiz-step").hide();
		$("#step"+step).show();	
	}
	
	
}

function showSkillResults() {
	
	// Questions 1 - 6
	var individual = 0;
	var team = 0;
	
	for (i=1; i<7; i++) {
		var val = $("input[name=q"+i+"]:checked").val();
		if (val && val != 0) {
			var v = val.substring(0,1);
			var t = val.substring(1);
			
			if (t == 'i') {
				individual = parseInt(individual) + parseInt(v);
			} else {
				team = parseInt(team) + parseInt(v);
			}
		}
	}
	
	var indpro = Math.round((individual / 12) * 100);
	var teampro = Math.round((team / 12) * 100);
	$("#indscore").html(indpro+"%");
	$("#teamscore").html(teampro+"%");
	$("#individual-bar span").css("width","0px").animate({"width": ((485 / 100) * indpro)+"px"}, {duration: "slow"});
	$("#team-bar span").css("width","0px").animate({"width": ((485 / 100) * teampro)+"px"}, {duration: "slow"});
	
	// Questions 7 - 10
	var recog = $("input[name=q7]:checked").val();
	var life = $("input[name=q8]:checked").val();
	var friend = $("input[name=q9]:checked").val();
	var money = $("input[name=q10]:checked").val();
	var recogpro = Math.round((recog / 4) * 100);
	var lifepro = Math.round((life / 4) * 100);
	var friendpro = Math.round((friend / 4) * 100);
	var moneypro = Math.round((money / 4) * 100);
	$("#recogscore").html(recogpro+"%");
	$("#lifescore").html(lifepro+"%");
	$("#friendscore").html(friendpro+"%");
	$("#moneyscore").html(moneypro+"%");
	$("#recognition-bar span").css("width","0px").animate({"width": ((485 / 100) * recogpro)+"px"}, {duration: "slow"});
	$("#lifestyle-bar span").css("width","0px").animate({"width": ((485 / 100) * lifepro)+"px"}, {duration: "slow"});
	$("#friendship-bar span").css("width","0px").animate({"width": ((485 / 100) * friendpro)+"px"}, {duration: "slow"});
	$("#money-bar span").css("width","0px").animate({"width": ((485 / 100) * moneypro)+"px"}, {duration: "slow"});

	// Questions 11 - 25
	var seller = 0;
	var innovator = 0;
	var organiser = 0;
	
	for (i=11; i<16; i++) {
		var val = $("input[name=q"+i+"]:checked").val();
		if (val && val != 0) {
			seller = parseInt(seller) + parseInt(val);
		}
	}
	for (i=16; i<21; i++) {
		var val = $("input[name=q"+i+"]:checked").val();
		if (val && val != 0) {
			innovator = parseInt(innovator) + parseInt(val);
		}
	}
	for (i=21; i<26; i++) {
		var val = $("input[name=q"+i+"]:checked").val();
		if (val && val != 0) {
			organiser = parseInt(organiser) + parseInt(val);
		}
	}
	var seller = Math.round((seller / 20) * 100);
	var innovator = Math.round((innovator / 20) * 100);
	var organiser = Math.round((organiser / 20) * 100);
	$("#sellerscore").html(seller+"%");
	$("#innovatorscore").html(innovator+"%");
	$("#organiserscore").html(organiser+"%");
	$("#seller-bar span").css("width","0px").animate({"width": ((485 / 100) * seller)+"px"}, {duration: "slow"});
	$("#innovator-bar span").css("width","0px").animate({"width": ((485 / 100) * innovator)+"px"}, {duration: "slow"});
	$("#organiser-bar span").css("width","0px").animate({"width": ((485 / 100) * organiser)+"px"}, {duration: "slow"});
}

function showChoiceResults() {
	
	var total = 0;
	
	for (i=1; i<35; i++) {
		total = parseInt(total) + parseInt($("input[name=q"+i+"]:checked").val());					
	}
	
	total = Math.round(((total/34)*100));
	
	$("#score").html(total+"%");
	$("#score-bar span").css("width","0px").animate({"width": ((485 / 100) * total)+"px"}, {duration: "slow"});
	
	if (total < 40) {
		$("#lt40").show();
	}
	if (total > 40 && total < 60) {
		$("#gt40lt60").show();
	}
	if (total > 60) {
		$("#gt60").show();
	}
}

function animPanel(panel) {
    
    $(".jqpanel").hide();
    $("#"+panel+" p.buttons a, #"+panel+" p.buttons a span").addClass("nobg");
    $("#"+panel+" .info").children().hide();
    $("#"+panel+" p.buttons").css("bottom","202px");
    
    $("#"+panel+" img").hide().css("left","-162px").show().animate({"left": "0"}, "slow", "linear", function() {
         $("#"+panel+" p.buttons a, #"+panel+" p.buttons a span").removeClass("nobg");
         if ($.browser.msie) {
            $("#"+panel+" .info h3, #"+panel+" .info ul").show();
            
            $("#"+panel+" p.buttons").show().animate({"bottom":"20px"},"slow","easeOutBounce");
         } else {
            $("#"+panel+" .info h3, #"+panel+" .info ul").fadeIn(1000,function() {
                $("#"+panel+" p.buttons").show().animate({"bottom":"20px"},"slow","easeOutBounce");
            });
         }
    });
    $("#"+panel+" .redbar").show();
    $("#"+panel).show();
    
}

function cyclepanels() {
    $(".advert").hide();
    if ($(".advert.on").next().length) {
        $(".advert.on").removeClass("on").next().addClass("on").fadeIn(3500);
    } else {
        $(".advert.on").removeClass("on");
        $("#sideAds .advert:first").addClass("on").fadeIn(3500);
    }
}