 $(document).ready(function(){
		// Language Dropdown 
		
		jQuery('#ChangeLanguage a').click(function(){
			jQuery(this).next('ul').slideToggle('fast');
			return false;
		}
		);
		
		// Input Field Focus
	
		jQuery('ul#nav li.searchForm input[type="text"], .newsletterSignup input[type="text"]').each(function(){
		jQuery(this).focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
		});
		
		//Initialize Ajax Tabs
        jQuery('#ArticleBody > ul#AjaxTabs').tabs();

		// CheckBox and Radio button Replacement : http://www.protofunc.com/scripts/jquery/checkbox-radiobutton/
		
		jQuery('input[type=checkbox]')
			.checkBox()
			.bind('checkBoxchange', function(e,ui){
				
				jQuery('#HighlightItem .resultsRecommend dl').each(function(i){
				
					
				});
			});
		
		jQuery('dl.teasers input, .optionSelect input[type=radio]').checkBox();
		
		// CheckAll 
		
		$('.optionEverything input, #ShowAll').toggle(function(){ 
			$('input[type=checkbox]').checkBox('changeCheckStatus', true); 	
		},
		function(){
			//$(this).checkBox('changeCheckStatus', false); 	
			$('input[type=checkbox]').checkBox('changeCheckStatus', false); 	
		}
		); 
		
		// Filters 
		
		//jQuery('dl.filters dl').hide(); // To make the filters auto expanded when page is loaded first time
		jQuery('dt.categoryHead').toggle(function(){
			var $this = $(this);
			jQuery(this).next('dd').children('dl').slideUp(function(){
				jQuery($this).removeClass("active");
			});
			
		},
		function(){
			
			jQuery(this).next('dd').children('dl').slideDown();
			jQuery(this).addClass("active");
			
		}
		);
		
		// Select Box Replacement : http://www.scottdarby.com/plugins/stylish-select/0.3/
		
		jQuery('p.sortBy select,.dropDownSelect').sSelect({animationSpeed:0});
		
		// Accordion
		
		
		$('#AccordionList h2.accordionHead').click(function(){
			$(this).next().slideToggle();
			$(this).toggleClass("selected");
			return false;
		}).next().hide();
		
		// Active the first list
		
		//	$('#AccordionList h2.accordionHead:first').next().toggle();
		//	$('#AccordionList h2.accordionHead:first').addClass("selected");
			
			//Acitve all list
			$('#AccordionList h2.accordionHead').next().toggle();
			$('#AccordionList h2.accordionHead').addClass("selected");
		
		
		// Search Results : Show More Details 
		
		jQuery('ul.searchItems li').children('dl.details').hide();
		jQuery('ul.searchItems a.toggleControl, a.itemImage, ul.searchItems h3').click(function(){
			if(jQuery(this).parents('li').hasClass("active")){
				jQuery(this).parent('li').children('dl.details').slideUp(function(){
				  jQuery(this).parent('li').removeClass("active");
				  });
				jQuery(this).parent('li').children('.toggleControl').children('b').children('b').html("View details <img src='/apps/yoursingapore/resources/traveller/images/vn/arrow_down.gif' alt='' />");
			}
			else{
				jQuery(this).parent('li').addClass("active");
				jQuery(this).parent('li').find('dl.details').slideDown();
			  	jQuery(this).parent('li').children('.toggleControl').children('b').children('b').html("Close <img src='/apps/yoursingapore/resources/traveller/images/vn/arrow_up_normal.gif' alt='' />");
			}
			return false;
		});
		
		// Toggle Contact Form 
		
		jQuery('a.toggleForm').prev('.forms').hide();
		jQuery('a.toggleForm').toggle(function(){
			jQuery(this).prev('.forms').slideDown();
			jQuery(this).removeClass("arrowDownButton").addClass("arrowUpButton");
			jQuery(this).children('b').children('b').html("close");
		},
		function(){
			jQuery(this).prev('.forms').slideUp();
			jQuery(this).removeClass("arrowUpButton").addClass("arrowDownButton");
			jQuery(this).children('b').children('b').html("Ask now")
		})
		
		// Load Facebox 
		
		jQuery('a[rel*=facebox]').facebox() 
		
		// Quick Look  
		
		jQuery('.resultsRecommend dl dt').mouseover(function(){
			jQuery(this).children('a.quickView').show();
		}).mouseout(function(){
			jQuery(this).children('a.quickView').hide();
		});
		
		// Contact us Faq List
		
		jQuery('dl.faqList dd').hide();
		jQuery('dl.faqList dt, dl.faqList a.arrowUpButton').click(function(){
			if(jQuery(this).parents('dl').children('dt').hasClass("active")){
				jQuery(this).parents('dl').find('dd').slideUp();
				jQuery(this).parents('dl').find('dt').removeClass("active");
			}
			else{
				jQuery(this).parents('dl').find('dd').slideDown();
				jQuery(this).parents('dl').find('dt').addClass("active");
			}
			return false;
		});
		
		
		// Form Validation 
		
		if(jQuery('#SubscribeForm').length){jQuery('#SubscribeForm').validate(); }
		
		if(jQuery('#QuestionForm').length){jQuery('#QuestionForm').validate();}
		
		if(jQuery('#RequestInfoForm').length){jQuery('#RequestInfoForm').validate();}
		
		//$.metadata.setType("attr", "validate");	
		
		if(jQuery('#TellUsExperienceForm').length){jQuery('#TellUsExperienceForm').validate();}
		
		// Add Reference Links 
		
		var count=0;
		var maxcount=4;
		jQuery('#AddLink').click(function(){
			if (count<maxcount){
				count++;
				jQuery('<input type="text" id=link_'+count+' name="referenceLinks[]" />').insertBefore(this);
			}
			return false;
		});
		
		var filecount=0;
		var maxfilecount=2;
		jQuery('#AddFile').click(function(){
			if (filecount<maxfilecount){
				filecount++;
				jQuery('<input type="file" id="file_'+filecount+'" class="checkextension" name="fileUploads[]" />').insertBefore(this);
			}
			return false;
		});
		
		// Contact us - Tell us Experience Switching
		
		
		var selectedRadio = jQuery('fieldset.optionSelect input[type=radio]:checked').attr('rel');
		jQuery('#'+jQuery('fieldset.optionSelect input[type=radio]:unchecked').attr('rel')).find('textarea').removeClass("required");
		jQuery('#'+ selectedRadio).slideDown();
		//jQuery('#'+ selectedRadio).children('textarea').removeClass("required");
		jQuery('.optionSelect input[type=radio]').click(function(e){
			e.preventDefault();
			jQuery('#'+selectedRadio).hide();
			jQuery('#'+selectedRadio).find('textarea').removeClass("required")
			
			var rel = $(this).attr("rel");
			selectedRadio = rel;
			jQuery('#'+rel).find('textarea').addClass("required")
			$('#'+rel).show();
			return false;
		});
		
	
});
// Run when page loads
$(document).ready(function(){
						   if ($('img').length) {
							$('img').load(function() {
									jQuery('body.firstLevel .catListing, .catListing').equalHeights(true);

							});
							} else {
									jQuery('body.firstLevel .catListing, .catListing').equalHeights(true);

							}

});


function resolveLanguage(countryCode){
	countryCode = countryCode.toLowerCase();
	if(countryCode == "cn"){
		return "zh";
	}else if(countryCode == "id"){
		return "id";
	}else if(countryCode == "vn"){
		return "vi";
	}else if(countryCode == "kr"){
		return "ko";
	}else if(countryCode == "jp"){
		return "ja";
	}else{
		return "en";
	}
}

function changeLanguage(url, lang){
	url = url.split("/");
	langIndex = (url[0] != "http:")? 3 : 5;
	url[langIndex] = lang;
	return url.join("/");
}


function isHomePage(path){
	path = path.split("/");
	page = path[4].substring(0, 10);
	return (page=="experience")? true : false;
}

function styleHeaderFlag(){
	var timeout = 0;
	
	var showSubMenu = function(e) {
		var dropdown = $(this).next();
		if (dropdown.css('display') == 'none')
			dropdown.fadeIn();
			
		$('.selector').bind('mouseleave', startTimeout);
	}
	
	var startTimeout = function(){
		timeout = setTimeout(hideSubMenu, 500);
	}
	
	var resetTimer = function(){
		clearTimeout(timeout);
	}
	
	 
	var hideSubMenu = function() {
		var dropdown = $('.selector').next();
		dropdown.fadeOut();
	}
	
	
	$('.selector').bind('mouseenter', showSubMenu);
	$('.selector').next().bind('mouseleave', hideSubMenu);
	$('.selector').next().bind('mouseenter', resetTimer);

}

function goToSpecial(link){
	var url = "";
	if($.cookie('country_code')){
		
		var countryList = getCQCountryList();
		if($.cookie('country_code') != "int" && (jQuery.inArray($.cookie('country_code'), countryList) >= 0)){
			url=link+"/countryexclusive/"+$.cookie('country_code')+".html";
		}else{
			url=link+".html";
		}
		
	}else{
		url=link+".html";
	}
	
	window.location.href=url;
}

function setCountryCodeCookie(countryCode, countryName){
	
	$.cookie('country_code', countryCode, { path: '/', expires: 90});
	$.cookie('country_name', countryName, { path: '/', expires: 90});
			
}

function getHtmlFlagContent(){
	if($.cookie('country_code') && $.cookie('country_name')){
		htmlFlagContent = '<img src="/content/dam/yoursingapore/traveller/localcontent/'+$.cookie('country_code')+'.gif" width="16" height="11" />'+$.cookie('country_name');
	}else{
		htmlFlagContent = '<img src="/content/dam/yoursingapore/traveller/localcontent/int.gif" width="16" height="11" />Global';
	}
	$('.selector').html(htmlFlagContent);
	
}

function getLangFromUrl(){
	url = location.href;
	url = url.split("/")
	lang = url[5].substr(0,2); 
	return lang;
}

function verifyLanguageUrl(){
	cookieLang = resolveLanguage($.cookie('country_code'));
	if(getLangFromUrl() != cookieLang){
		path = changeLanguage(location.href, cookieLang);
		validatePage(path, function(){
			if(isValidPage){
				window.location.href = path;
			}else{
				// then proceed to country home page... if country home page exist, otherwise proceed to global home page
			    //window.location.href = "/content/traveller/"+cookieLang+"/experience.html";
			}
		});
	}
}

function changeUrlToCountryHomepage(url, coutryCode){
	
	url = url.split("/");
	if(url.length > 6){ // absolute URL
		url[6] = "experience_"+coutryCode+".html";
	}else{
		url[4] = "experience_"+coutryCode+".html";
	}
	
	return url.join("/");
}

function redirectToCountryHomepage(){
	cookieLang = resolveLanguage($.cookie('country_code'));
	url = location.href;
	url = url.split("/");
		
	if(url.length > 6 ){
		var countryHomePage = changeUrlToCountryHomepage(location.href, $.cookie('country_code'));
		if(url[6] == "experience.html" && cookieLang == "en" && $.cookie('country_code') != "int"){
			
			validatePage(countryHomePage, function(){
				if(isValidPage){
					window.location.href = countryHomePage;
				}
			}); 
			
		}else{
			if(url[6].substr(0,11) == "experience_" && cookieLang == "en" && 
					url[6].substr(11,2) != $.cookie('country_code')){
					validatePage(countryHomePage, function(){
					if(isValidPage){
						window.location.href = countryHomePage;
					}else{
						url[6] = "experience.html";
		   		    	window.location.href = url.join("/");
					}
					
				});
			}
		}
	}
}

function gotoExperience(url){
	
	cookieLang = resolveLanguage($.cookie('country_code'));
	url = url.split("/");
	if(url.length > 4 && url[4].substr(0, 10) == "experience" && cookieLang == "en" ){
			path = location.href;
			path = path.split("/");
			path[6] = "experience_"+$.cookie('country_code')+".html";
			if(validatePage(path.join("/"))){
				window.location.href = path.join("/");
			}else{
				//url[5] = cookieLang;
   		    	window.location.href = url.join("/");
			}
									
	}else{
		window.location.href = url.join("/");
	}
}

function validatePage(path, doSuccess){
	var chkPage = "/content/traveller/en/config/header/jcr:content/parasys/header.flag.xyz?action=checkIsValidPath";
	var result = false;
	$.ajax({type: "GET",
			url: chkPage+"&page="+path,
			dataType: "script",
			async:false,
			success: doSuccess});
}


