/**
*
* Author: 	M. Mertens
* Created:	01-03-2011
* 
* Version: 	1.2
* Changed:	17-08-2011
* 
*
* Browser behavior for SAM Entrypage.
*
**/


/**
 * 
 * Check if the visitor want to perform a cookie reset.
 * 
 */
if(document.location.search.indexOf("?reset") !== -1) {
	$.cookie("sam-entrypage", null);
}




$(document).ready(function () {
	$("body").hide(); // Hide the body contents, first let the HTML parse and check for cookie existence.
	
	// Definition of global vars.
	var countyCookieName = "sam-entrypage" // No named arrays exist in JavaScript, so this is the best way.
	var countyCookieValue = $.cookie(countyCookieName); 

	
	
/**
 * 
 * Check if the country cookie exists.
 * 
 */
	if(countyCookieValue != null && countyCookieValue != "") {
		try {
			arrCookie = countyCookieValue.split("|");
			redirectTo(arrCookie[0], arrCookie[1]);
		} catch(error) {
			// We dont care about the exception :-) 	
		}
	} else {
		$("body").fadeIn(500);
	}
	
	
/**
 * 
 * Listeners bound to the selection buttons in the first step.
 * 
 */
	$("div.first_option input.btn").live("click", function() {
		$("div.first_option input.btn").attr("id", "");
		
		$(this).attr("id", "pressed");
		setPanelActive("div.option:eq(1)");
	});
	
	
    $("div.first_option input.btn").hover(
		function () {
			$(this).parent().addClass("hover");
		},
		function () {
			$(this).parent().removeClass("hover");
		}
	);
    

    
/**
 * 
 * Listeners bound to the select list with available countries.
 * 
 */
    $("div.second_option input#rbFix").live("click", function() {
    	setPanelActive("div.option:eq(2)");
    });
    
    $("div.second_option input#rbNoFix").live("click", function() {
    	checkValidOption();
    });
	
	$("div.second_option select#selectCountry").live("click", function() {
		checkValidOption();
	});
    
    $("div.second_option select#selectCountry").change(function() {
    	checkValidOption();
    	
    	$("input#rbNoFix").attr("checked", true); 
    });
    

    
/**
 * 
 * Listeners bound to the disagree and agree button.
 * 
 */
    $("div.last_option input#agreed").live("click", function() {
    	redirectStartpage();
    });
    
//    $("div.last_option input#disagreed").live("click", function() {
//    	alert($(this).attr("alt"));
//    }); 
    
    
    
    function checkValidOption() {
    	if($("div.second_option option:selected").val() == "0") {
    		$(".tbDisclaimer").val("");
    		setPanelInactive("div.option:eq(2)");

    		return false;
    	} else {
    		setPanelActive("div.option:eq(2)");
    	}
    	
    	return true;
    }
    
    
    function redirectTo(country, investor) {
		if(document.location.search.indexOf("origin=") !== -1) {
			arrOrigin = document.location.search.split("origin=");
			var redir = arrOrigin[arrOrigin.length-1];
		} else {
			var source = $("div[name=disclaimer-" + country + "]");
			redir = source.attr("url-" + investor);
		}
		
		window.location.replace(redir);
    }
    
    function redirectStartpage() {
    	var button 		= $("div.second_option input#rbFix"); // Country
    	var select 		= $("div.second_option option:selected"); // Country List
		var choice 		= $("div.first_option input#pressed"); // Investor
    	var country 	= "";
		var investor	= "private"; // Defaulting to private
    		
    	if(button.attr("checked")) {
    		country = button.val();
    	} else if(select.val() != "0") {
    		country = select.val();
    	}
		
		if(choice.hasClass("btnProfessional")) {
    		investor = "professional";
    	}
    	
    	if(country != "") {
    		$.cookie(countyCookieName, country + "|" + investor, { expires: 31 });
    		redirectTo(country, investor);
    	}
    }
    
    
    
    function setPanelActive(panel) {
    	$(panel).find("#overlay").fadeOut(400, function() {
    		$(this).removeClass("overlay");
    	});
    	
    	updateDisclaimer(false);
    }
    
    
    
    function setPanelInactive(panel) {
    	$(panel)
    		.find("#overlay")
    		.show()
    		.addClass("overlay");
    }
    
    
    
    function getInvestorType() {
    	var button = $("div.first_option input#pressed");
    	
    	if(button.hasClass("btnPrivate")) {
    		return "priv";
    	}
    	
    	return "prof"; // Defaulting to 'prof'.
    }
    
    
    
    function updateDisclaimer() {
    	var button 	= $("div.second_option input#rbFix");
    	var select 	= $("div.second_option option:selected");
    	var country = ""; 
    		
    	if(button.attr("checked")) {
    		country = button.val();
    	} else if(select.val() != "0") {
    		country = select.val();
    	}
    	
    	if(country != "") {
    		var source = $("div[name=disclaimer-" + country + "]");
    		$(".tbDisclaimer").val(source.text());
    	}
    }
});
