// Inside the event in scrollShow that changes the picture, window.pause is set to true. We call
// initPause to set window.pause == false because the event within scrollShow is called on load. 
// Next, intervalID is established to simulate a 'click' on one of the navigation buttons, every five seconds.
// So long as the user does not actually click one of the scroll buttons, window.pause will continue to be set equal 
// to false. If the user does, in fact, click on the page, window.pause will be set equal to 'true' in the scrollShow event
// and will not be reset to false. This will cause the ad window to stop autoscrolling. This does not mean that the setInterval 
// that was controlling the scroll gets destroyed, rather it will continue to be called every five seconds, but fail
// to do anything. See jquery.scrollShow.js @ line 145 for the corresponding window.pause evaluation.

var initPause = setTimeout("initWindowPause()",3000);
var intervalID = setInterval('intervalScroll()', 5000);

function initWindowPause(){
	window.pause = false;
}

function intervalScroll(){
	if(window.pause == false){
		$('#rightLink').click();
		window.pause = false;
	}
}

var generalUi = {
	init : function() {
		this.initProductsNav();
		this.initAccordion();
	//	this.initAdScreen();
		this.initFlashVideo();
	},
	initProductsNav : function() {
		jQuery('ul#productsNav').superfish({
			animation: {height: 'show'},
			autoArrows: false
		});
	},
	initAccordion : function() {
		var conf = {
		    header: 'div.accTitle', 
			autoHeight: true,
			fillSpace: true
		};
		if(jQuery('#accordion').length > 0) {
			if(jQuery.browser.version == '6.0' && jQuery.browser.msie) {
				conf['fillSpace'] = false;
			}
			jQuery('#accordion').accordion(conf);
		}
	},
	initAdScreen : function() {
		if(jQuery('#adScreen').length > 0) {
			jQuery('#adScreen').scrollShow({
				view:'#view',
				content:'#images',
				easing:'backout',
				wrappers:'link,crop',
				navigators:'a[id]',
				navigationMode:'sr',
				circular:true,
				start:0,
				itemSize:{
					height:329,
					width:256
				},
				popupWidth:500,
				popupHeight:680
			});
		}
	},
	initFlashVideo : function() {
		if(jQuery('.flashVideo').length > 0) {
			jQuery.fn.media.defaults.mp3Player = 'fileadmin/templates/flash/player.swf';
			jQuery('.flashVideo').media({ 
				width: 464, 
				height: 256, 
				autoplay: 1,
				params: {allowfullscreen: true, autostart:true}
			});
		}
	}
}

var warrantyUi = {
	init : function() {
		jQuery('#fieldSale_date').datepicker();
		warrantyUi.toggleDealerFields();
		jQuery('#fieldOther_dealer').click(function() {
			warrantyUi.toggleDealerFields();
		});
	},
	toggleDealerFields : function() {
		if(jQuery('#fieldOther_dealer').attr('checked')) {
			warrantyUi.showDealerFields();
		} else {
			warrantyUi.hideDealerFields();
		}
	},
	showDealerFields : function() {
		jQuery('#fieldDealer_id').attr("disabled", true);
		jQuery('#dealerSelector .error').hide();
		jQuery('#otherDealer_wrap').show();
	},
	hideDealerFields : function() {
		jQuery('#fieldDealer_id').attr("disabled", false); 
		jQuery('#dealerSelector .error').show();
		jQuery('#otherDealer_wrap').hide();
	}
}

$(function(){
	generalUi.init();
	warrantyUi.init();
});