/*javascript for all pages*/

//These variables are declared in the parent page
//var infoId = "ctl00_Maincontent1_ctl00_txtParamsBox";
//var infoIdMini = "ctl00_New_search_simple_txtParamsBox";
//var infoId = "paramsDiv";

$(document).ready(function(){
	
	//search tweaks
	//siteSearch.init();

	$("#in_region").dropdownToDiv();
	
	if($('#scotland').attr('src') != null){	

		//asset behaviour
		map.init();
	}
	else{
		// In Regional Flavours Map @ /trips/regional-flavours/default.html
		regionalFlavoursMap.init();
	}
        
});

(function($){
	$.fn.dropdownToDiv = function(options)
	{
	
	var selectItem = $(this);
	var optionItems = $(this).find('option');
	
	//doing the magic
	$(selectItem).hide();
	$(selectItem).after('<div id="newDD">Please select</div><ul id="newDDElements" style="display:none;"></ul>');
	$(optionItems).each (
	function(){
		$('#newDDElements').append('<li onClick="getSubOptionsMini(' + $(this).attr('value') + ');"><a href="'+ $(this).attr('value') + '">'+ $(this).text() +'</a></li>')
	})
	
	$('#newDD').click(function(){ $('#newDDElements').toggle();});
	var newDDli = $('#newDDElements').find('li');
	
	$(newDDli).click(function(){
		$('#newDDElements').hide();
    	$('#newDD').text($(this).text());
		return false;
   });
  }
})(jQuery);


var regionalFlavoursMap = {

	init: function () {
		
		var regexp = '#';  
		var map = $('#regional-flavours-map');
		var mapDefaultImage = $(map).attr('src');

		$('area').each(function(){
		
			var area = this;
			var areaName = $(this).attr('href'); 
			var areaImage = $(this).attr('href').replace(regexp,''); 
			var areaLink = $('a[href='+ areaName +']');
			var areaId = $(this).attr('id').replace('a_','');
			var id = $(this).attr('id').replace('a_','');
			
			// replace href
			$(this).attr('href', '/trips/regional-flavours/' + areaImage + '.html');
			
				$(area).mouseover(function(){
			
				$(map).attr('src', '/MasterPages/Default/Images/map-regions/map_' + areaImage + '.gif');
				$(areaLink).css('color', '#201963');
				
				$(area).mouseout(function(){
						$(map).attr('src', mapDefaultImage);
						$(areaLink).css('color', '#4F924C');
				});
				return false;
			});	
			
		});
	}
}


var map = {
	init: function () {
		
		var regexp = '#';  
		var map = $('#scotland');
		var mapDefaultImage = $(map).attr('src');
		
		//var infoId = "ctl00_Maincontent1_ctl00_txtParamsBox";

		$('area').each(function(){
								
			var area = this;
			var areaName = $(this).attr('href'); 
			var areaImage = $(this).attr('href').replace(regexp,''); 
			var areaLink = $('a[href='+ areaName +']');
			var areaId = $(this).attr('id').replace('a_','');
			var areaLi = document.getElementById("li_"+areaImage);
			var id = $(this).attr('id').replace('a_','');
			var RegionId = $(areaLink).attr('id').replace('a_','');
				$(area).mouseover(function(){
			
				$(map).attr('src', '/MasterPages/Default/Images/map-regions/map_' + areaImage + '.gif');
				$(areaLink).css('color', '#201963');
				
				$(area).mouseout(function(){
					if(document.getElementById(infoId).value.indexOf("regionID:" + RegionId)){
					//if(document.getElementById(infoId).innerHTML.indexOf("regionID:" + RegionId)){					
						$(map).attr('src', mapDefaultImage);
						$(areaLink).css('color', '#4F924C');
					}
				});
				return false;
			});	
			
			$(areaLink).mouseover(function(){
				$(map).attr('src', '/MasterPages/Default/Images/map-regions/map_' + areaImage + '.gif');			
				
				$(areaLink).mouseout(function(){
					if(document.getElementById(infoId).value.indexOf("regionID:" + RegionId)){
					//if(document.getElementById(infoId).innerHTML.indexOf("regionID:" + RegionId)){
						$(map).attr('src', mapDefaultImage);
					}
				});
			
				return false;	
			});	
				// Ajax for the advanced search - Start //
			$(areaLink).click(function(){
				if (RegionId == -1) {
					document.getElementById(infoId).value = "";
					document.getElementById('locations_box').style.display = "none";
					getTotalCount("counters", false);
					$('ul.regions a').css('color', '#4F924C');
					$('ul.regions a').css('font-weight', 'normal');					
					$(this).css('color', '#201963');
					$(this).css('font-weight', 'bold');					
					resetForm();
					return false;
				}
					$('ul.regions a').css('color', '#4F924C');
				$('ul.regions a').css('font-weight', 'normal')
				
				//document.getElementById(infoId).value	= "regionID:" + RegionId;
				//document.getElementById(infoId).innerHTML = "regionID:" + RegionId;
				$(this).css('color', '#201963');
				$(this).css('font-weight', 'bold');	
					if(document.getElementById('locations_box').style.display == "none")
					document.getElementById("locations_box").style.display = "block";
				
				getSubOptions('cities', RegionId);
			
				return false;	
			});		
				$(area).click(function(){
				
				$('ul.regions a').css('color', '#4F924C');
				$('ul.regions a').css('font-weight', 'normal')
				
				//document.getElementById(infoId).value	= "regionID:" + RegionId;
				//document.getElementById(infoId).innerHTML = "regionID:" + RegionId;
				$(areaLink).css('color', '#201963');
				$(areaLink).css('font-weight', 'bold');	
					if(document.getElementById('locations_box').style.display == "none")
					document.getElementById('locations_box').style.display = "block";
							
				getSubOptions('cities', RegionId);							
				
				return false;	
			});	
		
			// Ajax for the advanced search - End//
		});
	}
}
	
function getTotalCount(item, mini){
	
	if(mini){
		infoId = infoIdMini;
	}
	
    var qs = "update=" + item;
    
    if(item == "matches"){
	    if(document.getElementById(infoId).value != null)
    		qs += "&current=" + document.getElementById(infoId).value;
		else
			qs += "&current=";
	}
	
	var countTotal = 0;
	
	$.ajax({
	   method: 'POST',
	   url: '/ajax.aspx?',
	   data: qs,
	   success: function(html){
		 if(html){
         	countTotal = html;
         	if(item != "matches"){
	         	if(!mini){
			    	//document.getElementById('CounterTotal').innerHTML = "<em>The Foodie Guide</em> — search " + countTotal + " listings " + document.getElementById('CounterTotal').innerHTML;
			    	document.getElementById('CounterTotal').innerHTML = "<em>The Foodie Guide</em> — search " + countTotal + " listings";
	    		}
	    	}
		    document.getElementById('results').innerHTML = "" + countTotal + " matches";	
	 	 }
	   }
	  });
}

function addDeleteItem(chk){

	if(chk.name == 'option'){

		var listName = chk.id+'_options';
		
		if(chk.checked){
			if((chk.value != 'ev')&&(chk.value != 'tr')){
				if(chk.id == "eating_out"){
					getSubOptions(chk.value, 0);
					document.getElementById(listName).style.display = "block";
					document.getElementById("awards_opt").style.display = "block";
					document.getElementById("cuisine_opt").style.display = "block";
					document.getElementById("best_opt").style.display = "block";
				}
			}
			updateCriteria('group', chk.value, '', 1, false);
		}
		else{
			//if((chk.value != 'ev')&&(chk.value != 'tr')){
			if(chk.id == "eating_out"){
				
				document.getElementById("awards").selectedIndex = 0;				
				document.getElementById("cuisine").selectedIndex = 0;				
				document.getElementById("best").selectedIndex = 0;	
								
				document.getElementById(chk.id+'_options').style.display = "none";
				document.getElementById("awards_opt").style.display = "none";
				document.getElementById("cuisine_opt").style.display = "none";
				document.getElementById("best_opt").style.display = "none";
				
				document.getElementById(chk.value + '_div').innerHTML = '';
			}
			updateCriteria('group', chk.value, '', 0, false);
		}
	}
	else{ // sub-option items
		if(chk.checked){
			updateCriteria('subgroup', chk.name, chk.value, 1, false);
		}
		else{
			updateCriteria('subgroup', chk.name, chk.value, 0, false);
		}
	}
}
	
function addDeleteOptionItem(opt){


	var optName = opt.name; //  + " [" + opt.value +"]";

	if(opt.value != ""){
		updateCriteria('subgroup', optName, opt.value, 1, false);
	}
	else{
		updateCriteria('subgroup', optName, opt.value, 0, false);
	}

}
	
	
function replaceRegionID(current, regionID){

	var result = "";
	
	if(current.match("regionID")){		
		result = current.replace(/regionID:\d+/gi, "regionID:" + regionID);	
	}
	else{
		result = "regionID:" + regionID + current
	}

	return result;
}
	
// Populate Div with Sub-Options	
function getSubOptions(name, regionID){


    var qs = "";
	var current = "";
	

	if(regionID > 0){
		qs += 'regionID=' + regionID + '&';	  
		//document.getElementById(infoId).value = replaceRegionID(current, regionID);	
		current = document.getElementById(infoId).value;
		document.getElementById(infoId).value = replaceRegionID(current, regionID);	
	}
    qs += 'option=' + name;	  

	$.ajax({
	   method: 'POST',
	   url: '/ajax.aspx?',
	   data: qs,
	   success: function(html){
		 if(html){
				document.getElementById(name + '_div').innerHTML = html;
			//$('div.' + name + '_div').html(html);
			
	 	 }
	 	 getTotalCount("matches", false);
	   }
	  });	
	
}

// Populate Div with Sub-Options	
function getSubOptionsMini(regionID){

    if (regionID == -1) {
        getTotalCount("counters", true);
        resetForm();
        return;
    };
	
    var qs = "";
	
	qs += "current=" + document.getElementById(infoIdMini).value + "&";
	if(regionID > 0){
		qs += 'region	=' + regionID;
	}

	$.ajax({
	   method: 'POST',
	   url: '/ajax.aspx?',
	   data: qs,
	   success: function(html){
		 if(html){
			//document.getElementById(name + '_div').innerHTML = html;
			//$('div.' + name + '_div').html(html);
			
			document.getElementById(infoIdMini).value = html + "|mode:mini";
	
			getTotalCount("matches", true);
	 	 }
	   }
	  });	
}

function addDeleteItemMini(chk, prmEO, prmFD, prmPR){
    var _eo = document.getElementById(prmEO);
    var _fd = document.getElementById(prmFD);
    var _pr = document.getElementById(prmPR);
    
    
    var qs = document.getElementById(infoIdMini);
    qs.value = qs.value.replace("|option:tr|option:ev", "");
    qs.value = qs.value.replace("|option:eo", "");
    qs.value = qs.value.replace("|option:fd", "");
    qs.value = qs.value.replace("|option:pr", "");
    
    
    if(_eo.checked){qs.value+="|option:eo"}
    if(_fd.checked){qs.value+="|option:fd"}
    if(_pr.checked){qs.value+="|option:pr"}
    
    if(!_eo.checked &&  !_fd.checked && !_pr.checked){  
        qs.value+="|option:eo|option:fd|option:pr|option:tr|option:ev"
    }
      
    if(_eo.checked ||  _fd.checked || _pr.checked){// include ev-ents and tr-ips by default  
        qs.value += "|option:tr|option:ev";
    }	
    
    
      
	if(chk.name == 'option'){
		var listName = chk.id+'_options';
		if(chk.checked){
			updateCriteriaMini('group', chk.value, '', 1);
		}
		else{
			updateCriteriaMini('group', chk.value, '', 0);
		}
	}
	
	

}


function updateCriteria(type, value, id, selected, mini){
	
	if(mini){
		infoId = infoIdMini;
	}
				
    var qs = "";
	    if(document.getElementById(infoId).value != null){
    		qs += "&current=" + document.getElementById(infoId).value;
    		if(mini){// include ev-ents and tr-ips by default
                //qs += "|option:tr|option:ev";
	    	}
    	}

	//qs += 'current=' + document.getElementById(infoId).innerHTML;
    qs += '&type=' + type;
    qs += '&item=' + value;	
    qs += '&id=' + id;		
	qs += '&selected='+selected;
    
	//alert(value + " - " + id+ " - " + selected);

	$.ajax({
	   method: 'POST',
	   url: '/ajax.aspx?',
	   data: qs,
	   success: function(html){
		 if(html){
			document.getElementById(infoId).value = html;			
			getTotalCount("matches");
	 	 }
	   }
	  });
}
function updateCriteriaMini(type, value, id, selected){	
    var qs = "&current=" + document.getElementById(infoIdMini).value;

	//qs += 'current=' + document.getElementById(infoId).innerHTML;
    qs += '&type=' + type;
    qs += '&item=' + value;	
    qs += '&id=' + id;		
	qs += '&selected='+selected;
    
	//alert(value + " - " + id+ " - " + selected);

	$.ajax({
	   method: 'POST',
	   url: '/ajax.aspx?',
	   data: qs,
	   success: function(html){
		 if(html){
			
			getTotalCount("matches");
	 	 }
	   }
	  });
}


// Include keyword in the criteria
function setKeyword(keyword, mini){
	
	if(mini){
		infoId = infoIdMini;
	}
	
	var qs = "";
	//qs += 'current=' + document.getElementById(infoId).innerHTML;
	qs += 'current=' + document.getElementById(infoId).value;
    qs += '&keyword=' + keyword;	  

	$.ajax({
	   method: 'POST',
	   url: '/ajax.aspx?',
	   data: qs,
	   success: function(html){
		 if(html){
			//document.getElementById(infoId).innerHTML = html;
			document.getElementById(infoId).value = html;
			getTotalCount("matches");
	 	 }
	   }
	  });	
}




// Keywords field
var interval = 200;
var currentMSecs = 0; // miliseconds
//var maxMSecs = 1500; // miliseconds // 1.5secs is too slow
var maxMSecs = 300; // miliseconds
var counter;

function sendKeyWordRequest(input){
var keyword = input.value;
	if(currentMSecs < maxMSecs){
		clearTimeout(counter)
		currentMSecs = 0;
		checkInterval(keyword, false);	
	}
	else{
		currentMSecs = 0;
	}
}	

function sendKeyWordRequestMini(input){
var keyword = input.value;
	if(currentMSecs < maxMSecs){
		clearTimeout(counter)
		currentMSecs = 0;
		checkInterval(keyword, true);	
	}
	else{
		currentMSecs = 0;
	}
}	

function checkInterval(keyword, mini){
	if(currentMSecs > maxMSecs){
		currentMSecs = 0;
		clearTimeout(counter);
		setKeyword(keyword, mini);
	}
	else{
		currentMSecs = currentMSecs + interval;
		counter = setTimeout(function () { return checkInterval(keyword, mini); }, interval);
	}
}


	