/*
Title:				Base Javascript
Author: 			Sapient
Date Modified: 		April 2009
Version:			2.0
*/


document.write( '<link rel="stylesheet" type="text/css" href="http://www.phones4u.co.uk/css/base.js.css">' );



if ( window.Phones4U == undefined ) Phones4U = {};


// Navigation
Phones4U.Navigation =
{
	openNavigationItem: null,
	openSubNavigation:null,
	timer: null,

	setup: function()
	{
		jQuery( '#Navigation > li' ).each( function(i)
		{
			var link = jQuery( this ).find( 'a:first' );
			var subnav = jQuery( this ).find( '.subNavigation' );
		
			if ( subnav.length )
			{
				link.addClass( 'dynamicSubnav-' + i );
				subnav.addClass( 'dynamicSubnavContents-' + i );

				// bind navigation events
				link.bind( 'mouseover', Phones4U.Navigation.open );
				link.bind( 'mouseover', Phones4U.Navigation.cancelClose );
				link.bind( 'mouseout', Phones4U.Navigation.timeClose );

				// bind subnavigation events
				subnav.bind( 'mouseover', Phones4U.Navigation.cancelClose );
				subnav.bind( 'mouseout', Phones4U.Navigation.timeClose );
			}
			else
			{
				link.bind( 'mouseover', Phones4U.Navigation.close );				
			}
		} );
	},
	
	open: function(e)
	{
		if ( Phones4U.Navigation.openNavigationItem ) Phones4U.Navigation.close();

		// close any live search if open
	//	Phones4U.Search.close();

		Phones4U.Navigation.openNavigationItem = jQuery( this );
		Phones4U.Navigation.openNavigationItem.addClass( 'active' );
	
		// get subnav id
		var id = this.className.match( /dynamicSubnav-([0-9]+)/ )[1];

		// get subnav contents
		Phones4U.Navigation.openSubNavigation = jQuery( this ).parents( 'li' ).find( '.dynamicSubnavContents-' + id );
		if ( Phones4U.Navigation.openSubNavigation.length ) jQuery( 'body' ).append( Phones4U.Navigation.openSubNavigation );
		else Phones4U.Navigation.openSubNavigation = jQuery( 'body' ).find( '.dynamicSubnavContents-' + id );

		// calculate subnav position
		var position = { x:jQuery( this ).offset().left, y: jQuery( this ).offset().top + jQuery( this ).parent().height() };

		// display and position subnav
		Phones4U.Navigation.openSubNavigation.css( { display:'block', top:position.y } );
		if ( position.x - jQuery( '#Container' ).offset().left + Phones4U.Navigation.openSubNavigation[0].offsetWidth < jQuery( '#Container' ).width() ) Phones4U.Navigation.openSubNavigation.css( { left:position.x } );
		else Phones4U.Navigation.openSubNavigation.css( { left:'auto', right:jQuery( '#Container' ).offset().left } );

		// hide dropdowns in ie6
		//alert(jQuery( '#Navigation li' ).index( jQuery( this ).parents( 'li' ) ));
		if ( jQuery.browser.msie && parseInt( jQuery.browser.version ) == 6 && (jQuery( '#Navigation li' ).index( jQuery( this ).parents( 'li' ) ) == 20 ||  jQuery( '#Navigation li' ).index( jQuery( this ).parents( 'li' ) ) < 2))
		{	
			//alert(jQuery( '#Navigation li' ).index( jQuery( this ).parents( 'li' ) ));
			jQuery( '#PhoneFinder select' ).css( { visibility:'hidden' } );
		}

		e.preventDefault();
	},

	timeClose: function(e)
	{
		Phones4U.Navigation.timer = setTimeout( Phones4U.Navigation.close, 500 );
	},

	cancelClose: function(e)
	{
		clearTimeout( Phones4U.Navigation.timer );
	},

	close: function()
	{
		if ( Phones4U.Navigation.openNavigationItem )
		{
			Phones4U.Navigation.openNavigationItem.removeClass( 'active' );
		
			Phones4U.Navigation.openSubNavigation.css( { display:'none' } );
			
			Phones4U.Navigation.openNavigationItem = false;
			Phones4U.Navigation.openSubNavigation = false;

			// show dropdowns in ie6
			if ( jQuery.browser.msie && parseInt( jQuery.browser.version ) == 6 ) jQuery( '#PhoneFinder select' ).css( { visibility:'visible' } );
		}
	}
}
jQuery( document ).ready( Phones4U.Navigation.setup );

// Phone Finder
Phones4U.PhoneFinder = 
{
	fields: null,
	rules: null,

	setup: function()
	{
		if ( jQuery( '#PhoneFinder' ).length )
		{
			// get initial state
			var firstDropdown = jQuery( '#' + Phones4U.PhoneFinder.fields[0] );
		
			// reload ruleset
			Phones4U.PhoneFinder.loadRules( firstDropdown[0].selectedIndex );
			
			// add events to dropdowns
			jQuery( '#PhoneFinder select' ).bind( 'change', Phones4U.PhoneFinder.change );
		}
	},
	
	change: function(e)
	{
		// get changed dropdown
		var changedDropdown = this.id;
		for ( var x = 0; x < Phones4U.PhoneFinder.fields.length; x++ )
		{
			if ( Phones4U.PhoneFinder.fields[x] == changedDropdown )
			{
				var changedDropdownIndex = x;
				break;
			}
		}

		Phones4U.PhoneFinder.loadRules( changedDropdownIndex );
	},
	
	loadRules: function(i)
	{
		// disable and reset all dropdowns after the current one
		for ( var x = i + 1; x < Phones4U.PhoneFinder.fields.length; x++ )
		{
			jQuery( '#' + Phones4U.PhoneFinder.fields[x] )[0].disabled = 'disabled';
			jQuery( '#' + Phones4U.PhoneFinder.fields[x] )[0].selectedIndex = 0;
		}

		// set up current dropdown state according to the first item in the current dropdown
		var firstDropdown = jQuery( '#' + Phones4U.PhoneFinder.fields[0] );
		var currentDropdown = jQuery( '#' + Phones4U.PhoneFinder.fields[i] );
		var firstItem = currentDropdown.find( 'option:first' );
		
		// get ruleset
		if ( i == 0 ) var rules = Phones4U.PhoneFinder.rules[ currentDropdown.val() ];
		else if ( i == 1 ) var rules = Phones4U.PhoneFinder.rules[ firstDropdown.val() ];

		for ( var x in rules )
		{
			var nextDropdown = jQuery( '#' + x );
			var nextDropdownData = rules[x];
		}

		if ( i == 1 )
		{
			for ( var x in nextDropdownData )
			{
				if ( nextDropdownData[x].value.toString() == currentDropdown.val().toString() )
				{
					nextDropdown = jQuery( '#' + nextDropdownData[x].field );
					nextDropdownData = nextDropdownData[x][ nextDropdownData[x].field ];
					break;
				}
			}
		}

		// populate next dropdown
		if ( nextDropdown )
		{
			nextDropdown.find( 'option:not(:first)' ).remove();
			jQuery.each( nextDropdownData, function() { nextDropdown.append( '<option value="' + this.value + '">' + this.title + '</option>' ) } );
	
			// enable the next dropdown
			nextDropdown[0].disabled = '';
		}
	}
}
jQuery( document ).ready( Phones4U.PhoneFinder.setup );



