$( document ).ready( function() {
	

	
	
	/*
		ROUNDED CORNERS
	*/
	$( "div.rounded" ).each( function( i ) {
		$( this ).prepend( '<span class="rounded_top_left"></span>' );
		$( this ).prepend( '<span class="rounded_top_right"></span>' );
		$( this ).append( '<span class="rounded_bottom_left"></span>' );
		$( this ).append( '<span class="rounded_bottom_right"></span>' );
	});
	
	// Add rounded corners to the left corners
	$( "div.rounded_left" ).each( function( i ) {
		$( this ).prepend( '<span class="rounded_top_left"></span>' );
		$( this ).append( '<span class="rounded_bottom_left"></span>' );
	});
		
	// Add rounded corners to the right corners
	$( "div.rounded_right" ).each( function( i ) {
		$( this ).prepend( '<span class="rounded_top_right"></span>' );
		$( this ).append( '<span class="rounded_bottom_right"></span>' );
	});
	
	// Add rounded corners to the top corners
	$( "div.rounded_top" ).each( function( i ) {
		$( this ).prepend( '<span class="rounded_top_left"></span>' );
		$( this ).prepend( '<span class="rounded_top_right"></span>' );
	});
	
	// Add rounded corners to the bottom corners
	$( "div.rounded_bottom" ).each( function( i ) {
		$( this ).append( '<span class="rounded_bottom_left"></span>' );
		$( this ).append( '<span class="rounded_bottom_right"></span>' );
	});
	
	
	
	$( "div.rounded_dark" ).each( function( i ) {
		$( this ).prepend( '<span class="rounded_top_left rounded_top_left_dark"></span>' );
		$( this ).prepend( '<span class="rounded_top_right rounded_top_right_dark"></span>' );
		$( this ).append( '<span class="rounded_bottom_left rounded_bottom_left_dark"></span>' );
		$( this ).append( '<span class="rounded_bottom_right rounded_bottom_right_dark"></span>' );
	});

	
	
	
	
	
	
	/*
		CLOCK
	*/
	function updateClock() {
		var currentTime = new Date();

		var currentHours = currentTime.getHours();
		var currentMinutes = currentTime.getMinutes();
		var currentSeconds = currentTime.getSeconds();
		
		// Add zeros to single digits
		currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
		currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

		// AM or PM
		var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

		// use a 12 hour clock format
		currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

		// Midnight should be 12 not 0
		currentHours = ( currentHours == 0 ) ? 12 : currentHours;

		// Concatenate it all together
		var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
		
		// Update the clock element
		$( '#site_clock_time' ).text( currentTimeString );
		
		// Run this function again in 1 second
		setTimeout( updateClock,1000 );
	}
	updateClock();
	
	
		
		
		
	
	
	
	/*
		POPUP EVENT
	*/
	$( ".popup" ).click( function( event ) { 
		// Stop the browser from following the link
		event.preventDefault();
		
		// Grab the target of the link
		var sTarget = $( this ).attr( "href" );
		
		// Set the width and height of the popup
		var iWidth = 800;
		var iHeight = 600;
		var iScrollbars = 2;
		
		// Center the window on the clients screen
		var iLeft = (screen.width)?(screen.width-iWidth)/2:100;
		var iTop = (screen.height)?(screen.height-iHeight)/2:100;
		
		// Put the window config together
		var sSettings='width='+iWidth+',height='+iHeight+',top='+iTop+',left='+iLeft+',scrollbars='+iScrollbars+',location=yes,directories=no,status=yes,menubar=yes,toolbar=yes,resizable=yes';
		
		// Create the new window
		win=window.open( sTarget, "", sSettings );
	} );
	
	
	
	/*
		HIDE DELIVERY FIELDS ON VOUCHER FORM
	*/
	function toggleDeliveryDetails() {
		if( $('#sameascardholder:checked').length > 0 ) {
			$("#delivery_details").slideUp( 1000 );
		} else {
			$("#delivery_details").slideDown( 1000 );
		}
	}
	
	if( $("#sameascardholder").length > 0 && $("#delivery_details").length > 0 ) {
		$("#sameascardholder").click( function( event ) {
			toggleDeliveryDetails();
		});
		toggleDeliveryDetails();
	}




	if( $("#tristanvideo").length > 0 ) {
		$('#tristanvideo').flash( {
			src: '/media/flash/market_kitchen.swf',
			width: 238,
			height: 149
		} );
	}







	if( $("#ourfilm #danddvideo a").length > 0 ) {
		$( "#ourfilm #danddvideo a" ).click( function( event ) { 
			event.preventDefault();
			
			$( "#ourfilm #danddvideo a" ).remove();
			
			$('#ourfilm #danddvideo').flash( {
				src: '/media/flash/about.swf',
				width: 238,
				height: 149
			} );
		} );
	}

	
	
	/*
		CHEF GALLERY INFO BOX
	*/
	
	if( $("#chefgallery").length > 0 ) {
		swapChefGalleryImage();
		
		$( "#chefgallery span.hidden" ).each( function( i ) {
			$( this ).removeClass( "hidden" ).hide();
		} );
	}
	
	
	
	if( $( "#homepage #landing_panel .eveningstandard" ).length > 0 ) {
		setTimeout( "loadNextHomepageImage()", 6000 );
	}
	
	
	
	
	
	
	
	/*
	*	VOUCHER GA TRACKING
	*/
	
	$('#restaurant-vouchers-form').submit(function(event){
		//event.preventDefault();
		
		
		
		//Sets Variables
		var $iOrderRef = $('#orderref').attr('value');
		var $sAffiliation = $('#orderinfo').attr('value');
		var $iTotal = $('#inputamount').attr('value');
		var $iPostage = $('#postagecosts').attr('value');
		var $sTown = $('#deliverytown').attr('value');
		var $sCounty = $('#deliverycounty').attr('value');
		var $sCountry = "UK"; //$('#deliverycountry').attr('value')
		var _gaq = _gaq || []; 
		
		//Sets Products into an array
		var $sProducts = $('#productlist').attr('value');		
		var $aProducts = $sProducts.split("|");
		var $aVouchers = new Array();
		$.each($aProducts, function(index, value) { 
			var $aVoucherParts = value.split(",");
			var $iVoucherValue = $aVoucherParts[0];
			var $iVoucherQuantity = $aVoucherParts[1];			
			$aVouchers[index] = new Array($iVoucherValue,$iVoucherQuantity);
 
		});

		
				
		//Submits required tracking information
		_gaq.push(['_setAccount', 'UA-19102492-1']); 
		_gaq.push(['_setDomainName', 'none']); 
		_gaq.push(['_setAllowLinker', true]); 
		_gaq.push(['_trackPageview']); 
		
		
		// Main transaction details
		_gaq.push(['_addTrans', 
			$iOrderRef,           // order ID - required 
			$sAffiliation,  // affiliation or store name 
			$iTotal,          // total - required 
			0,           // tax 
			$iPostage,              // shipping
			$sTown,       // city 
			$sCounty,     // state or province 
			$sCountry             // country 
		]); 
  
  
  
		//Goes through each product and submits item
		$.each($aVouchers, function(index, value) {
			var $iVoucherValue = value[0];
			var $iVoucherQuantity = value[1];
			
			_gaq.push(['_addItem', 
				$iOrderRef,			// order ID - required 
				'unlimited',		// SKU/code - required 
				$iVoucherValue + ' Pound Voucher',	// product name 
				'Vouchers',   		// category or variation 
				$iVoucherValue,		// unit price - required 
				$iVoucherQuantity 	// quantity - required 
			  ]);
 
		});
		
  		//submits transaction to the Analytics servers 
		_gaq.push(['_trackTrans']); 
  
  		//Google Object
		(function() { 
			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
			var s = document.getElementsByTagName('script')[0]; 
			s.parentNode.insertBefore(ga, s); 
		})();
						 
	});
	
	
	
	
	
	
	
	
	
} );






function loadNextHomepageImage() {
	
	var aIds = new Array();
	aIds[0] = "eveningstandard1";
	aIds[1] = "eveningstandard2";
	aIds[2] = "eveningstandard3";
	aIds[3] = "eveningstandard4";
	aIds[4] = "eveningstandard5";
	
	var oElement = $( "#homepage #landing_panel .eveningstandard" );
	var oNewElement = oElement.clone();
	
	var iCurrentIndex = jQuery.inArray(  oElement.attr( "id" ), aIds );
	
	var iNewIndex = iCurrentIndex + 1;
	iNewIndex = ( iNewIndex >= aIds.length ) ? 0 : iNewIndex;
	
	oNewElement.attr( "id", aIds[ iNewIndex ] );
	
	oNewElement.insertBefore( oElement );
	setTimeout( "removeOldHomepageImage()", 1000 );
	
}


function removeOldHomepageImage() {
	
	var oElement = $( "#homepage #landing_panel .eveningstandard" );
	console.log( oElement[1] );
	
	$( oElement[1] ).fadeOut( 1000, function () {
		$(this).remove();
	} );
	
	setTimeout( "loadNextHomepageImage()", 6000 );
}






function swapChefGalleryImage() {
	$( "#chefgallery span.visible" ).fadeOut(
		"slow",
		function() {
			var oVisibleElement = $( "#chefgallery span.visible" );
			oVisibleElement.removeClass( "visible" );
			
			if( oVisibleElement.next().length > 0 ) {
				oVisibleElement.next().addClass( "visible" ).fadeIn( "slow" );
			} else {
				var aChildren = oVisibleElement.parent().children();
				$( aChildren[0] ).addClass( "visible" ).fadeIn( "slow" );
			}
		}
	);
	
	setTimeout( "swapChefGalleryImage()", 3000 );
}


	/*
		NEWSLETTER AUTHENTICATION
	*/
	
function newsletterValidate(){
	

	var name= $("input[name='cm-name']").val();
	var email= $("input[name='cm-thkijy-thkijy']").val();

	if(name != "" && name != null && 
		email != "" && email != null){
		
	}
	
	
	else{
		alert('Please fill out all form fields');
		return false;
	}
}




//Makes the Dropdown Nav work, will replace in time
function openSelect( form ) { 
	var newIndex = form.RedirectURL.selectedIndex; 
	if ( newIndex != 0 ) { 
		url = form.RedirectURL.options[ newIndex ].value; 
		_gaq.push(['_link', url]); 
		window.location.assign( url ); 
	} 

} 
