$(document).ready(function() {
	
	$countryName = $('#currentCountryName').val();
	$countryId = $('#currentCountryId').val();
	
	
	if($countryName  != "") {
		var ddlObj1=$("#select-country-placeholder").finalselect({id:"country",viewWidth:'136px',selectImage:'/images/front/hg-customselect.gif', selectText:$countryName, viewMouseoverColor:"fff" });	
	} else {
		var ddlObj1=$("#select-country-placeholder").finalselect({id:"country",viewWidth:'136px',selectImage:'/images/front/hg-customselect.gif', selectText:$countryName, viewMouseoverColor:"fff" });		
	}
	
	if ($countryId) {
		loadLanguages($countryId);
	}	
	
	ddlObj1.addItem($('#selector-country').html());
	$('#country-select').css('z-index', 200);
	
	$("#select-country-placeholder div.row").bind('click',function(){
		$("#select-country-placeholder .textshow").html($(this).html());
	});
	
	$("#select-country-placeholder").bind('click', function() {
		$("#country-selectshow").css('z-index', 1200);
		$("#language-selectshow").css('z-index', 1);
	});
	
	$("#select-language-placeholder").bind('click', function() {
		$("#language-selectshow").css('z-index', 1200);
		$("#country-selectshow").css('z-index', 1);
	});	
	
	$('#select-country-placeholder div.row').bind('click', function() {
		$id = $(this).attr('id');
		loadLanguages($id);
	});
});
var $url = false;
function loadLanguages($id) {

	$.ajax({
	   type: "GET",
	   url: "/index/get-languages/for/" + $id,
	   success: function($msg){
	 		$('#select-language-label').fadeIn();
	 		$("#select-language-placeholder").fadeIn();
	 		var ddlObj2 = null;
			var ddlObj2=$("#select-language-placeholder").finalselect({id:"language",viewWidth:'136px',selectImage:'/images/front/hg-customselect.gif', selectText:'select language', viewMouseoverColor:"#fff" });	
			ddlObj2.addItem($msg);	
			$('#countryId').val($id);					
			
			$("#select-language-placeholder div.row").bind('click',function(){
				$langId = $(this).attr('id');
				 $.ajax({
					   type: "GET",
					   url: "/language/get-domain/countryId/" + $id + "/languageId/" + $langId,
					   success: function($result){
							$("#select-language-placeholder .textshow").html($(this).html());
							
							var adres = new String(window.location);
							var len = adres.length;
							
							
								
								$url = $result;
								
								$('#remember-box').fadeIn('slow');
								
								$("#go").bind('click', function(){
									
									var $remember = $('#remember-me').attr('checked');
									
									if($remember){
										window.location= "http://www.hg.eu/index/index/remember/1/address/" + $url.replace(/\//i, '|');
									}else{
										window.location= "http://" + $url;
									}
									
								});

							
					   }
					 });
				
				$("#select-language-placeholder .textshow").html($(this).html());
			});		
								
	   }
	 });	
}