// Next/Previous Slide Function
function aDSSNextPrev(argNP) {
	
	// Check time to make fade animation is complete
	vDate = new Date();
	vTime = vDate.getTime();
	vTimeElapsed = vTime - vDSSTime;
	
	// Do nothing and return if fader is still running
	if( vTimeElapsed < (vDSSFadeTime + 350) )
		return;
	
	// Find out where we are
	if( argNP == "n" )
		vDSSCurrent++;
	if( argNP == "p" )
		vDSSCurrent--;
	if( vDSSCurrent >= vDSSAmount )
		vDSSCurrent = 0;
	if( vDSSCurrent < 0 )
		vDSSCurrent = vDSSAmount - 1;
		
	clearInterval(vSlideTimerID);
	vSlideTimerID = setInterval ( "aDSSNextPrev('n')", 7000 );
		
	// Change Pic, text, hyperlinks
	if( (vDSSCurrent % 2) == 0 ) {
		$("#iMainAdf").attr("src", aDSSs[vDSSCurrent]);
	} else {
		$("#iMainAdb").attr("src", aDSSs[vDSSCurrent]);
	}
	
}


// Onload
$(document).ready(function(){
													 
	// Set Scroller
	fScrollerInit();
													 
	// Set Opacity To Full
	$('#dMainAdf').css({opacity: 1.0});
	$('#dMainAdb').css({opacity: 1.0});
	
	// When text changes in search boxes copy values about
	$('#dFrmKwf0').keyup(function() {
		vVal = $(this).val();
		$('#dFrmKdf2').val( vVal );
	});
	$('#dFrmKdf2').keyup(function() {
		vVal = $(this).val();
		$('#dFrmKwf0').val( vVal );
	});
	
	// Click handlers for radio buttons
	$('#fFrmOpkk').bind('click', function() {
		ChangeSearchType();
	});
	$('#fFrmOpdd').bind('click', function() {
		ChangeSearchType();
	});
	$('#fFrmOpkd').bind('click', function() {
		ChangeSearchType();
	});

	// Left Right Mouseovers
	$('#iLeftA').hover(function () {
		this.src = 'http://sx.newsprints.co.uk/i/arrow-left-white.gif';
	}, function () {
		this.src = 'http://sx.newsprints.co.uk/i/arrow-left-grey.gif';
	});
	$('#iRightA').hover(function () {
		this.src = 'http://sx.newsprints.co.uk/i/arrow-right-white.gif';
	}, function () {
		this.src = 'http://sx.newsprints.co.uk/i/arrow-right-grey.gif';
	});
	
	// Left Right click handlers
	$('#iLeftA').bind('click', function() {
		aDSSNextPrev('p');
	});
	$('#iRightA').bind('click', function() {
		aDSSNextPrev('n');
	});
	
	// Main Image Load Event (front)
	$('#iMainAdf').load(function() {

		// Started
		if( vDSSStarted ) {
			
			// Capture current time (for anim fader timer)
			vDSSDate = new Date();
			vDSSTime = vDSSDate.getTime();
			
			$("#dMainAdf").animate({ opacity: 1.0 }, vDSSFadeTime );
			$("#dMainAdb").animate({
				opacity: 0.0
			}, vDSSFadeTime, function() {
				$(this).css({'z-index' : '10000'});
				$("#dMainAdf").css({'z-index' : '10001'});
			});
			
		}
		
		// Set the text (hide hrefs from other div)
		$("#dMainAdf1").text(aDSSt[vDSSCurrent]);
		$("#dMainAdf2").text(aDSSp[vDSSCurrent]);
		$("#dMainAdf3").html( '<a href="' + aDSSv[vDSSCurrent] + '" title="View">View</a><a href="' + aDSSb[vDSSCurrent] + '" title="Buy">Buy</a><a href="' + aDSSe[vDSSCurrent] + '" title="Email">Email</a>'  );

		// Turn On
		if( ! vDSSStarted )
			vDSSStarted = true;

	});

	// Main Image Load Event (front)
	$('#iMainAdb').load(function() {
		
		// Capture current time (for anim fader timer)
		vDSSDate = new Date();
		vDSSTime = vDSSDate.getTime();
			
		$("#dMainAdb").animate({ opacity: 1.0 }, vDSSFadeTime );
		$("#dMainAdf").animate({
			opacity: 0.0
		}, vDSSFadeTime, function() {
			$(this).css({'z-index' : '10000'});
			$("#dMainAdb").css({'z-index' : '10001'});
		});

		// Set the text (hide hrefs from other div)
		$("#dMainAdb1").text(aDSSt[vDSSCurrent]);
		$("#dMainAdb2").text(aDSSp[vDSSCurrent]);
		$("#dMainAdb3").html( '<a href="' + aDSSv[vDSSCurrent] + '" title="View">View</a><a href="' + aDSSb[vDSSCurrent] + '" title="Buy">Buy</a><a href="' + aDSSe[vDSSCurrent] + '" title="Email">Email</a>'  );
		
		// Turn On
		if( ! vDSSStarted )
			vDSSStarted = true;
		
	});

	// Click event for text box to get rid of default values
	$('#dFrmKwf0').focus(function() {
		if( this.value == "Choose a search option by clicking a button below...") {
			this.value = '';
			$('#dFrmKdf2').val('');
		}
	});
	$('#dFrmKdf2').bind('click', function() {
		if( this.value == "Choose a search option by clicking a button below...") {
			this.value = '';
			$('#dFrmKwf0').val('');
		}
	});
	
	// Date Pickers
	$(function() {

		vDate = $("#dFrmKdf3").val();
		$("#dFrmKdf3").datepicker( {
			onSelect: function(date) {
				$("#dFrmDdf3").val( date );
    		vToDate = $("#dFrmKdf4").val();
				if( vToDate.length < 8 ) {
					$("#dFrmKdf4").val( date );
					$("#dFrmDdf4").val( date );
				}
			}
    });
		$("#dFrmKdf3").datepicker( "option", "dateFormat", 'dd/mm/yy' );
		$("#dFrmKdf3").datepicker( "option", "defaultDate", 0 );
		$("#dFrmKdf3").val(vDate);

		vDate = $("#dFrmKdf4").val();
		$("#dFrmKdf4").datepicker( {
			onSelect: function(date) {
				$("#dFrmDdf4").val( date );
    		vToDate = $("#dFrmKdf3").val();
				if( vToDate.length < 8 ) {
					$("#dFrmKdf3").val( date );
					$("#dFrmDdf3").val( date );
				}
			}
    });
		$("#dFrmKdf4").datepicker( "option", "dateFormat", 'dd/mm/yy' );
		$("#dFrmKdf4").datepicker( "option", "defaultDate", 0 );
		$("#dFrmKdf4").val(vDate);

		vDate = $("#dFrmDdf3").val();
		$("#dFrmDdf3").datepicker( {
			onSelect: function(date) {
				$("#dFrmKdf3").val( date );
    		vToDate = $("#dFrmDdf4").val();
				if( vToDate.length < 8 ) {
					$("#dFrmDdf4").val( date );
					$("#dFrmKdf4").val( date );
				}
			}
    });
		$("#dFrmDdf3").datepicker( "option", "dateFormat", 'dd/mm/yy' );
		$("#dFrmDdf3").datepicker( "option", "defaultDate", 0 );
		$("#dFrmDdf3").val(vDate);

		vDate = $("#dFrmDdf4").val();
		$("#dFrmDdf4").datepicker( {
			onSelect: function(date) {
				$("#dFrmKdf4").val( date );
    		vToDate = $("#dFrmDdf3").val();
				if( vToDate.length < 8 ) {
					$("#dFrmDdf3").val( date );
					$("#dFrmKdf3").val( date );
				}
			}
    });
		$("#dFrmDdf4").datepicker( "option", "dateFormat", 'dd/mm/yy' );
		$("#dFrmDdf4").datepicker( "option", "defaultDate", 0 );
		$("#dFrmDdf4").val(vDate);

	});
	
	// Newspaper Search default
	$('#fIOID').val(vPrefPapID);
	$('#fIOTitle').val(vPrefPapTxt);
	$('#dFrmNwp').hide();
	$('#dFrmNwpA').show();
	$('#dFrmNwpA').html( '<span>Searching:</span> ' + vPrefPapTxt );
	if( vPrefPapID == 0 )
		$('#fFrmOpnp').removeAttr('checked');
	else {
		$('#fFrmOpnp').attr('checked','checked');
	}

	
	// Newspaper Search Tick Box
	$('#fFrmOpnp').bind('click', function() { 
		if( ! $(this).attr('checked') ) {
			$('#dFrmNwpf').val('');
			$('#fIOID').val(0);
			$('#fIOTitle').val('All Newspapers Pictures');
			$('#dFrmNwp').hide();
			$('#dFrmNwpA').show();
		} else {
			if( $('#fIOID').val() == 0 ) {
				$('#dFrmNwpA').hide();
				$('#dFrmNwp').show();
			} else {
				$('#dFrmNwp').hide();
				$('#dFrmNwpA').show();
			}
		}
		$('#dFrmNwpA').html( '<span>Searching:</span> ' + $('#fIOTitle').val() );
	});
	
	// Newspaper Search Text Box
	$('#dFrmNwpf').blur(function() {
		OutOfFocusNewspaperSearch();
	});	
	
	// Autocomplete
	$(function() {
		$.ajax({
			url: "/global.xml",
			dataType: "xml",
			success: function( xmlResponse ) {
				var xmldata = $( "n", xmlResponse ).map(function() {
					return {
						value: $( "t", this ).text(),
						label: $( "a", this ).text() + " " + $( "t", this ).text(),
						//nwptext: $( "a", this ).text(),
						id: $( "id", this ).text()
					};
				}).get();
				
				$( "#dFrmNwpf" ).autocomplete({
					source: xmldata,
					minLength: 0,
					position: { my : "right bottom", at: "right top" },
					
					focus: function( event, ui ) {
						$( "#dFrmNwpf" ).val( ui.item.nwptext );
						return false;
					},					
					select: function( event, ui ) {
						$( "#dFrmNwpf" ).val( ui.item.value );
						$( "#fIOTitle" ).val( ui.item.value );
						$( "#fIOID" ).val( ui.item.id );
						OutOfFocusNewspaperSearch();
						return false;
					}
				})
				
				.data( "autocomplete" )._renderItem = function( ul, item ) {
					return $( "<li></li>" )
						.data( "item.autocomplete", item )
						.append( "<a>" + item.value + "</a>" )
						.appendTo( ul );
				};
			}
		});
	});
	
	// Search Form Button
	//$('#iSrchFrmBtn').bind('click', function() {
	//	VerifySearchForm();
	//});
	
	$('#fSearch').submit( function() {
	  return VerifySearchForm();
	});
	
	
	// Click the radio buttons to hide/show search form parts
	if( vPrefSearchType == "k" )
		$('#fFrmOpkk').click();
	if( vPrefSearchType == "d" )
		$('#fFrmOpdd').click();
	if( vPrefSearchType == "b" )
		$('#fFrmOpkd').click();
	
});


// Verify
function VerifySearchForm() {
	
	// Min lengths
	vKWMin = 3;

	// Get value from radio button (kk=Keyword, dd=Date, kd=Keyword and Date)
	vRadVal = $('input:radio[name=fFrmOp]:checked').val();

	// Verify Keyword Search
	if( vRadVal == "kk" ) {
		
		// Get Values
		vKW = jQuery.trim( $('#dFrmKwf0').val() );
		
		if( vKW == "Choose a search option by clicking a button below..." ) {
			alert("Please enter your keyword(s)");
			return false;
		}
		if( vKW.length < vKWMin ) {
			alert("Please enter your keyword(s) with at least " + vKWMin + " characters");
			return false;
		}
		
		// Set form action and submit
		$("#fSearch").attr("action","/search/" + vPrefSortBy + "/k/f/1");
		return true

	}
	
	// Verify Date Search
	if( vRadVal == "dd" ) {
		
		// Get Values
		vDFrom = $('#dFrmDdf3').val();
		vDTo = $('#dFrmDdf4').val();
		
		// No Dates
		if( vDFrom.length < 8 ) {
			alert("Please enter the 'from' date");
			return false;
		}
		if( vDTo.length < 8 ) {
			alert("Please enter the 'to' date");
			return false;
		}

		// Try and process dates
		try {
			aFrom = vDFrom.split("/");
			vFrom = new Date(aFrom[2], (aFrom[1] - 1), aFrom[0],0,0,0);
			aTo = vDTo.split("/");
			vTo = new Date(aTo[2], (aTo[1] - 1), aTo[0],0,0,0);
		}
		catch(err) {
			alert("Please format your dates as dd/mm/yyyy");
			return false;
		}
		
		// Make sure to date is GTE from date
		if( vTo < vFrom ) {
			alert("The make sure the 'from' date is the same as or less than the 'to' date");
			return false;
		}
		
		// Set form action and submit
		$("#fSearch").attr("action","/search/" + vPrefSortBy + "/d/f/1");
		$('#fSearch').submit();

	}

	// Verify Date Search
	if( vRadVal == "kd" ) {
		
		// Get Values
		vKW = jQuery.trim( $('#dFrmKdf2').val() );
		
		if( vKW == "Choose a search option by clicking a button below..." ) {
			alert("Please enter your keyword(s)");
			return;
		}
		if( vKW.length < vKWMin ) {
			alert("Please enter your keyword(s) with at least " + vKWMin + " characters");
			return;
		}

		
		// Get Values
		vDFrom = $('#dFrmKdf3').val();
		vDTo = $('#dFrmKdf4').val();
		
		// No Dates
		if( vDFrom.length < 8 ) {
			alert("Please enter the 'from' date");
			return;
		}
		if( vDTo.length < 8 ) {
			alert("Please enter the 'to' date");
			return;
		}

		// Try and process dates
		try {
			aFrom = vDFrom.split("/");
			vFrom = new Date(aFrom[2], (aFrom[1] - 1), aFrom[0],0,0,0);
			aTo = vDTo.split("/");
			vTo = new Date(aTo[2], (aTo[1] - 1), aTo[0],0,0,0);
		}
		catch(err) {
			alert("Please format your dates as dd/mm/yyyy");
			return;
		}
		
		// Make sure to date is GTE from date
		if( vTo < vFrom ) {
			alert("The make sure the 'from' date is the same as or less than the 'to' date");
			return;
		}
		
		// Set form action and submit
		$("#fSearch").attr("action","/search/" + vPrefSortBy + "/b/f/1");
		$('#fSearch').submit();

	}
	
}


// Clean up Newspaper Search
function OutOfFocusNewspaperSearch() {

	// Check values
	if( $('#fIOID').val() > 0 ) {
		$('#fFrmOpnp').attr('checked','checked');
		$('#dFrmNwpA').html( '<span>Searching:</span> ' + $('#fIOTitle').val() );
	} else {
		$('#fFrmOpnp').removeAttr('checked');
		$('#dFrmNwpA').html( '<span>Searching:</span> ' + 'All Newspapers Pictures' );
	}
	
	// Show label div and hide form
	$('#dFrmNwp').hide();
	$('#dFrmNwpA').show();
	
}


// Reload adverts
function ChangeSearchType() {
	
	// Get value from radio button (kk=Keyword, dd=Date, kd=Keyword and Date)
	vRadVal = $('input:radio[name=fFrmOp]:checked').val();
	
	// Get rid of newspaper search box
	OutOfFocusNewspaperSearch();
	
	// Remove text from search box
	if( $("#dFrmKwf0").val() == "Choose a search option by clicking a button below..." )
		$("#dFrmKwf0").val("");
	if( $("#dFrmKdf2").val() == "Choose a search option by clicking a button below..." )
		$("#dFrmKdf2").val("");

	// Hide Show Keyword
	if( vRadVal == "kk" ) {
		$("#dFrmKw").css({'display': 'block', 'visibility': 'visible'});
	} else {
		$("#dFrmKw").css({'display': 'none', 'visibility': 'hidden'});
	}

	// Hide Show Date Range
	if( vRadVal == "dd" ) {
		$("#dFrmDd").css({'display': 'block', 'visibility': 'visible'});
	} else {
		$("#dFrmDd").css({'display': 'none', 'visibility': 'hidden'});
	}

	// Hide Show Keyword Range 
	if( vRadVal == "kd" ) {
		$("#dFrmKd").css({'display': 'block', 'visibility': 'visible'});
	} else {
		$("#dFrmKd").css({'display': 'none', 'visibility': 'hidden'});
	}
	
}



// Timer for slide
vSlideTimerID = setInterval ( "aDSSNextPrev('n')", 7000 );


// Reload adverts
function ReloadAds(argad) {
	if( argad == 0 )
		document.getElementById("iAd0").contentDocument.location.reload(true);
	if( argad == 1 )
		document.getElementById("aRAd1").contentDocument.location.reload(true);
	if( argad == 2 )
		document.getElementById("aRAd2").contentDocument.location.reload(true);
}


// Timer for adverts
//vAdTimer0 = setInterval ( "ReloadAds(0)", 11343 );
//vAdTimer1 = setInterval ( "ReloadAds(1)", 13343 );
//vAdTimer2 = setInterval ( "ReloadAds(2)", 15343 );

