// Hide flash on Apple devices, or hide the gif replacement on others.
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))  {
	document.write ('<style type="text/css"><!--#swf{display:none;}--></style>');
} else {
 	document.write('<style type="text/css"><!--#no-swf{display:none;}--></style></script>');
}

$(document).ready(function(){
	$.ajaxSetup({
		timeout: 10000
	});
	// Right banners rotation.
	$('ul#rightBanners').innerfade({
		speed: 1000,
		timeout: 10000,
		type: 'random_start',
		containerheight: '300px'
	});

	// Loads vogels bracket finder, once the user clicks inside the div.
	$("#vogelswrapper").one("click", function() {
		$('#vogelswrapper').html('<img src="/images2/spinner.gif" alt="Loading..." />');
		$('#vogelswrapper').load('http://' + window.location.host + '/ajax.php?m=Ajax_Plugins_VogelsBracketFinder');
	});
	$("#vogelswrappermatch").one("click", function() {	
		$('#vogelswrappermatch').load('http://' + window.location.host + '/ajax.php?m=Ajax_Plugins_VogelsBracketMatcher');
	});

	// bind change event to select
	$('#search_by_brand').change(function () {
		var url = $(this).val(); // get selected value
		if (url) { // require a URL
			window.location = url; // redirect
		}
		return false;
	});

	// Handles user requests for updated option prices.
	$('.optionlist').change(function() {
		var url = 'http://' + window.location.host + '/ajax.php?m=Ajax_Modules_OptionsChecker';
		var pids = $(this).attr('class');
		var pid = 0;
		pids = pids.split(' ');
		for (var i = 0, len = pids.length; i < len; ++i) {
			if(pids[i].indexOf('ol') == 0) {
				pid = pids[i].substr(2);
			}
		}
		//Select each option present
		url = url + '&pid=' + pid
		$('.optionlist.ol' + pid).each(function() {
			url = url + '&options[]=' + $(this).attr('value');
		});
		$.ajax({
			url: url,
			type: 'get',
			dataType: 'text',
			async: false,
			success: function(data) {
				result = data;
			}
		});
		if(result == 'fail' || result == '') {
			//Do nothing, no update.
			return;
		} 
		parts = result.split('|');
		if(parts.length == 2) {
			$('#priceoutputarea' + pid).html(parts[0]);
			$('#stockoutputarea' + pid).html(parts[1]);
		}
	});

	$("#call-info-box").dialog({
		autoOpen: false,
		height: 375,
		width: 525,
		modal: true,
		buttons: {
			Close: function() {
				$(this).dialog("close");
			}
		}

	});

	//Toggle call box on button click
	$(".show-call-box").click(function(e) {
		$("#call-wrapper").show();
    	if($('#call-info-box').dialog('isOpen')) {
    		$('#call-info-box').dialog('close');
		} else {
    		$('#call-info-box').dialog('open');
		}
		e.preventDefault();
		e.stopPropagation();
	});

	$("#security-info-box").dialog({
		autoOpen: false,
		height: 525,
		width: 525,
		modal: true,
		buttons: {
			Close: function() {
				$(this).dialog("close");
			}
		}
	});
	
	// Close all open dialogs on click anywhere.
	$('html').bind('click',function(){
    	if($('#security-info-box').dialog('isOpen')) {
    		$('#security-info-box').dialog('close');
		}
    	if($('#call-info-box').dialog('isOpen')) {
			$('#call-info-box').dialog('close');
		}
	});


	// Toggle security box on click.
	$(".show-security-box").click(function(e) {
		$("#security-wrapper").show();
    	if($('#security-info-box').dialog('isOpen')) {
    		$('#security-info-box').dialog('close');
		} else {
    		$('#security-info-box').dialog('open');
		}
		e.preventDefault();
		e.stopPropagation();
	});

	//IE Fix.
	if($.browser.msie) {
		objects = document.getElementsByTagName("object");
		for(var i = 0; i < objects.length; i++) {
   		 	objects[i].outerHTML = objects[i].outerHTML;
		}
	}
});



