var $initial = true;
var $current_banner_index = 1;

$(function() {
	
	//Cufon.replace('.handle a', { fontFamily: 'HandelGothic BT', hover: true });
	//Cufon.replace('.handle', { fontFamily: 'HandelGothic BT', hover: true });
	Cufon.replace('.handle', { fontFamily: 'Handel Gothic D', hover: true });
	Cufon.replace('.handle a', { fontFamily: 'Handel Gothic D', hover: true });

	
	// Login Form
	$password_change = function() {
		
		$new_input = document.getElementByID('pinput');
		$(this).style.display='none';
		
		$(this).replaceWith($new_input);
		$new_input.style.display='block';
		$new_input.focus();
	}
	
	
	$('#login input.login').bind('focus blur', function(e) {
		
		switch(e.type) {
			case 'focus': {
				$(this).val($(this).val() == 'Email' ? '' : $(this).val());
				break;
			}
			case 'blur': {
				$(this).val($(this).val() == '' ? 'Email' : $(this).val());
				break;
			}
		}
	});
	
	$('#login #ptinput').live('focus blur', function(e) {

		$field = $(this);

		switch(e.type) {
			case 'focusin':
			case 'focus': {
				
				if($field.attr('type') == 'password')
					return true;
				
				$newfield = $(this).replaceWith($('<input />', {
					'id': $field.attr('id'),
					'name': $field.attr('name'),
					'value': '' + ($field.val() == 'Password' ? '' : $field.val()),
					'type': 'password',
					'class': 'password'
				}));
				
				$('#ptinput').focus();
				
				break;
			}
			case 'focusout':
			case 'blur': {
			
				if($field.attr('type') == 'text')
					return true;
					
				if($field.val() != '')
					return true;
				
				$newfield = $(this).replaceWith($('<input />', {
					'id': $field.attr('id'),
					'name': $field.attr('name'),
					'value': 'Password',
					'type': 'text',
					'class': 'password'
				}));
				
				break;
			}
		}
	});
	
	
	/*
	var $source_field = $('#pinput');
	$pw_change = function() {
		
		console.log($source_field);
		$new_input = $('<input />', {
			'id': 		$source_field.attr('id'),
			'type': 	'password',
			'name': 	$source_field.attr('name'),
			'value': 	$source_field.val(),
			'class': 	'password'
		});
		
		$('#pinput').replaceWith($new_input);
		$new_input.css{ 'display': 'block' }).focus();
	}
	*/
	//$pw_change();
	//$('#ptinput').focus($password_change);
	
	
	
	var $timeout;
	
	// Dropdown Menu

	$('.parent a').mouseover(function() {
		
		clearTimeout($timeout);
		$dropdown_menu = $(this).parent().find('ul.sub-nav');
		$dropdown_menu.css({'display':'block'});
		
	

	});
	
	$('.parent').mouseleave(function() {
		//console.log('leave');
		$dropdown_menu = $(this).find('ul.sub-nav');
		$timeout = setTimeout(function() { $dropdown_menu.not(':animated').slideUp({ queue: false, duration: 0, easing: 'easeOutQuad' }); }, 50);
		$dropdown_menu.css({'display':'none'});
	});
	
  
	//LOADING POPUP
	//Click the button event!
	$popup = function() {
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	}
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupClose").click(function(){
	disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
	disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

	setInterval(changeBG, 6000);
	
	
	$('#login form a.img').click(function(e) {
		e.preventDefault();
		$(this).parents('form').submit();
	});
});

	
function pwchange(){
	$new_input = document.getElementById('pinput');
	document.getElementById('ptinput').style.display='none';
	$new_input.style.display='block';
	$new_input.focus();
}
function submitLogin() {
	document.homelogon.submit();
}
function submitSearch(){
	document.quicksearch.submit();
	}
function nextStep(){
		if(check_selection1()== true){
			document.prodform.submit();
		}
	}
function submitSetlang(){
	document.setlang.submit();
	}
function submitSendEnq(){
	document.checkout.submit();
	}
function getBGImage(theDiv){
	
	
}


function changeBg2() {
	
	
	
	
}


function changeBG(){

	var $post_initial = false;
	var $background  = "http://www.flexicon.uk.com/assets/images/flexicon-";
	var $background2 = "full";
	var $background3 = "-header";
	var $imgNum = 0;//number of header image
	var $num = 0; // to define number of header images
	var $ext = '.png';
	var $thedivBck = '';
	$thediv = $('#banner');
	//$thedivBck = getStyle('banner','background-image');
	
	$thedivBck = $('#banner').css('backgroundImage');
	$imgNum = $thedivBck.substring($thedivBck.length-6,$thedivBck.length-5);//will need rethink if ever more than 9 images!
	
	// the next line is a hack to cope with safari and ie do not return '/" but webkit firefox and chrome do?
	if($imgNum == '.'){$imgNum = $thedivBck.substring($thedivBck.length-7,$thedivBck.length-6);}
	//window.alert($thedivBck+'  '+$imgNum);
	$imgNum = parseInt($imgNum);
	//window.alert($thedivBck+'  '+$imgNum);
	if($('#banner').hasClass('inner')) {
		var $num = 6; //number of header images
		var $background2 = "half";
	}
	else {
		var $num = 5; //number of header images
	}
	
	if($num == $imgNum) {
		$num = 1; //if the header is last in set reset
	}
	else {
		$num = $imgNum+1;
	}
	
	if($initial) {
		$initial = false;
		$num--;
	}
	
	//num  = Math.floor(Math.random()*num)+1
	var $backgroundURL = '';
	$backgroundURL = "url('" + $background + $background2 + $background3 + $num + $ext + "')";
	
	//console.log('Next in sequence:', $backgroundURL);
	//window.alert($backgroundURL);
	
	// Set the BG to the new image.
	$('#banner.default').css({ backgroundImage: $backgroundURL });
	$('.cover').delay(100).fadeTo(5000, 0, function() {
		$('#banner.default #bannertop,#banner.default #bannerbottom').css({ backgroundImage: $backgroundURL, 'opacity': '1' }).fadeIn(0, function() {
			
		});
		/*fadeTo(4000, 1, 'easeInOutQuad', function() {
			
		});*/			
	});

}

	


	function popup() {
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	}
	

