// New sherlock search Javascript
//
// Uses Prototype.js and internet spanners
//
// Andy Gale, EPC Direct Ltd, Bristol

window.onload = page_onload_innit;

/*
var selected_holidaytype     = false;
var selected_destination     = false;
var selected_region          = false;
var selected_version         = false;
var sherlock_default_version = false;
*/

function page_onload_innit()
{
/*
	if ($('holidaytype')) { 
		$('holidaytype').onchange  = header_search;
		$('destination').onchange  = header_search;
		$('region').onchange       = header_search;
		$('doquicksearch').onclick = header_do_search;
		header_search();
	}
	
	
	if ($('seodropdowns')) { 
		sidebar_select();
	}
	
	header_menu_select()*/
	seo_image_innit();
}

/*
function header_search()
{
	selected_holidaytype = $('holidaytype').options[$('holidaytype').selectedIndex].value;
	selected_destination = $('destination').options[$('destination').selectedIndex].value;
	selected_region      = $('region').options[$('region').selectedIndex].value;
	selected_version     = $('version').options[$('version').selectedIndex].value;
	
	new Ajax.Request('/AJAX/sherlock-search',
    {
        method: 'get',
        parameters: { holidaytype: selected_holidaytype, 
		              destination: selected_destination,
					  region:      selected_region, 
					  version:     selected_version },
        onSuccess: header_update,
        onFailure: function(){ alert('Sorry, there was an error with your search. Please try again later.') }    
     } );
}

function header_update(transport)
{
	var response = transport.responseText || '';
	var sherlock_types = new Array();
	
	eval(response);
	
	//  Holiday types
	 
	$('holidaytype').options.length = 0;
	$('holidaytype').options[0] = new Option('I don\'t mind', '');

	me = -1;
	
	for (i = 0; i < sherlock_types.length; i++) { 	
		$('holidaytype').options[i + 1] = new Option(sherlock_types[i][0], sherlock_types[i][1]);
		if (selected_holidaytype == sherlock_types[i][1]) { 
			me = i + 1;
		}
	}
	
	if (me > -1) { 
		$('holidaytype').selectedIndex = me;
	}

	// Destinations/Countries

	$('destination').options.length = 0;
	$('destination').options[0] = new Option('I don\'t mind', '');
	
	me = -1;
	
	for (i = 0; i < sherlock_countries.length; i++) { 	
		$('destination').options[i + 1] = new Option(sherlock_countries[i][0], sherlock_countries[i][1]);
		if (selected_destination == sherlock_countries[i][1]) { 
			me = i + 1;
		}
	}
	
	if (me > -1) { 
		$('destination').selectedIndex = me;
	}

	// Regions
	
	$('region').options.length = 0;
	$('region').options[0] = new Option('I don\'t mind', '');
	
	me = -1;
	
	for (i = 0; i < sherlock_regions.length; i++) { 	
		$('region').options[i + 1] = new Option(sherlock_regions[i][0], sherlock_regions[i][1]);
		if (selected_region == sherlock_regions[i][1]) { 
			me = i + 1;
		}
	}
	
	if (me > -1) { 
		$('region').selectedIndex = me;
	}
	
}


function header_do_search()
{
	selected_holidaytype = $('holidaytype').options[$('holidaytype').selectedIndex].value;
	selected_destination = $('destination').options[$('destination').selectedIndex].value;
	selected_region      = $('region').options[$('region').selectedIndex].value;
	selected_version     = $('version').options[$('version').selectedIndex].value;
	
	if((selected_holidaytype=="") && (selected_destination=="")){
		alert("Please select a holiday type or destination");
		return false;
	}
	
	seo_tags = new Array();
	
	if (selected_holidaytype) { 
		seo_tags.push(selected_holidaytype);
	}
	
	if (selected_destination) { 
		seo_tags.push(selected_destination);
	}
	
	if (selected_region) { 
		seo_tags.push(selected_region);
	}

	url = '/' + seo_tags.join('/');

	if (selected_version != sherlock_default_version) { 
		url += '?version=' + selected_version;
	}
	
	self.location.href = url;

	return false;
}

// Simple Sidebar JS

function sidebar_select()
{
	Element.getElementsByClassName($('seodropdowns'), 'sidebarselect').each(function(item) {
		item.onchange = sidebar_change;
    });
}

function sidebar_change()
{
	self.location.href = '/' + this.options[this.selectedIndex].value;
}
*/

function seo_image_innit()
{	
	if ($('sherlockimageunderprev')) { 
		$('sherlockimageunderprev').onclick = seo_image_prev;
	}
	
	if ($('sherlockimageundernext')) { 
		$('sherlockimageundernext').onclick = seo_image_next;
	}
}

function seo_image_next()
{
	var seo_tag    = self.location.pathname.substr(1);
	var imagehrefs = $('sherlockimageimg').src.split('/');
	var image_href = imagehrefs[imagehrefs.length - 1];
	
	new Ajax.Request('/AJAX/sherlock-image',
    {
        method: 'get',
        parameters: { seo_tag:    seo_tag, 
		              image_href: image_href,
					  direction:  'next' },
        onSuccess: seo_image_update,
        onFailure: function(){ alert('Something went wrong...') }    
     } );
}

function seo_image_prev()
{
	var seo_tag    = self.location.pathname.substr(1);
	var imagehrefs = $('sherlockimageimg').src.split('/');
	var image_href = imagehrefs[imagehrefs.length - 1];
	
	new Ajax.Request('/AJAX/sherlock-image',
    {
        method: 'get',
        parameters: { seo_tag:    seo_tag, 
		              image_href: image_href,
					  direction:  'prev' },
        onSuccess: seo_image_update,
        onFailure: function(){ alert('Something went wrong...') }    
     } );
}

function seo_image_update(transport)
{
	var response = transport.responseText || '';
	var ajax_seo_image = false;
	
	eval(response);
	
	$('sherlockimageimg').src = ajax_seo_image.image;
	$('sherlockimageimg').alt = ajax_seo_image.caption;
	$('sherlockimagecaption').innerHTML = ajax_seo_image.caption;
	
	if (ajax_seo_image.next == true) { 
		$('sherlockimageundernext').style.visibility = 'visible';
	} else {
		$('sherlockimageundernext').style.visibility = 'hidden';
	}
	
	if (ajax_seo_image.prev == true) { 
		$('sherlockimageunderprev').style.visibility = 'visible';
	} else {
		$('sherlockimageunderprev').style.visibility = 'hidden';
	}
}	

/* >>moved to sitestat.xsl<<

function sitestat(ns_l) 
{ 
    ns_l += "&ns__t=" + (new Date()).getTime();
    ns_pixelUrl = ns_l; 
    ns_0 = document.referrer;
    ns_0 = (ns_0.lastIndexOf("/") == ns_0.length-1) ? ns_0.substring(ns_0.lastIndexOf("/"),0) : ns_0;
    if (ns_0.length > 0) 
    ns_l += "&ns_referrer="+escape(ns_0);
    if (document.images) {
        ns_1 = new Image();
        ns_1.src=ns_l;
    } else {
        document.write("<img src=\"+ns_l+\" width=\"1\" height=\"1\" />");
    }
}
*/

/* new epc header intialize */
/*
function header_menu_select(){

		href = window.location.pathname + window.location.search;
		
	
		
		if (href.substring(0, 1) != '/') { 
			href = '/' + href;
			
		}
	
		uls = document.getElementsByTagName('ul');

		for (i = 0; i < uls.length; i++) { 
			ul = uls[i];
		
			if (ul.className == 'topnav') { 
				
				for (j = 0; j < ul.childNodes.length; j++) {
					
					node = ul.childNodes[j];
					
		
					if (node.tagName == 'li' || node.tagName == 'LI') { 
	
						for (k = 0; k < node.childNodes.length; k++) { 
						
							inode = node.childNodes[k];
							
							if (inode.tagName == 'a' || inode.tagName == 'A') { 
								path = inode.pathname + inode.search;
								
								if (path.substring(0, 1) != '/') { 
									path = '/' + path;
								}
	
								if (path == href) { 
									//set li classname

									node.className = 'current';
								}
						
							}
		
						}
					}
				}
			}
	
	}
}
*/
