/*
 * ENABLE JS-ONLY STYLES
 */
$('html').addClass('js-enabled');
if(typeof(swfobject) !== 'undefined' && swfobject.hasFlashPlayerVersion("9.0.28")) {
	$('html').addClass('flash-enabled');
}
/*
 * ONLOAD EVENTS
 */
$(document).ready(function() {

	/*
	 * GLOBAL ENHANCEMENTS
	 */
	/* dynamically repeat paging navigation */
	if($('#paging-navigation').length !== 0) {
		var $newnav = $('#paging-navigation').clone();
		$newnav.attr('id',$newnav.attr('id') + '-repeat');
		$('#paging-navigation').after($newnav);
	}
	
 	/* open pages in new window */
	$('a.external-link, a.download-link').click(function() {
		window.open(this.href);
		return false;
	});

	/*
	 * SPECIFIC ENHANCEMENTS
	 */
	$('#view-summary').click(function() {
		//get id from link
		var $target = $($(this).attr('href'));
		if ($('#overlay-close').length === 0) {
			//add close button
			$('<img src="images/skin/button_close.gif" id="overlay-close" class="close-button" />').appendTo($target).click(function() {
				//ie7 has problem fading in transparent image
				$.browser.msie && parseInt($.browser.version) === 7 ? $target.slideUp() : $target.fadeOut();
			});
		}
		//open it - ie7 has problem fading in transparent image
		$.browser.msie && parseInt($.browser.version) === 7 ? $target.slideDown() : $target.fadeIn();
		return false;
	});

	/*
	 * FORM VALIDATION
	 */
	$('#order-form').submit(function() {
		var _ok = true;
		$('#order-form .required').each(function() {
			//no need to continue if one fails
			if(!_ok) { 
				return false; 
			}
			var $field = $(this);
			var _val = $.trim($field.val());
			var _msg = 'Please enter ';
			//handle empty cases
			if(_val === '' || (this.tagName.toLowerCase() === 'select' && _val === 'x')) {
				_ok = false;
			}
			//handle email
		 	else if($field.attr('id') === 'email') {
				_ok = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(_val);
				if(!_ok) {
					_msg += 'valid ';
				}
			}
			//handle quantity
			else if($field.attr('id') === 'quantity') {
				_ok = !isNaN(_val);
				if(!_ok) {
					_msg += 'a number for ';
				}
				//check shipping code while here
				if(_ok && _val > 3) {
					$field = $('#shipping');
					_ok = $field.val() !== '';
				}
			}
			//show message
			if(!_ok) {
				$field.focus();
				_msg += $('label[for='+ $field.attr('id') + ']').text();
				alert(_msg);
			}
		});
		//populate hidden fields
		$('#countryName').val($('#country option:selected').text());
		$('#roleName').val($('#role option:selected').text());
		return _ok;
	});
});

/* Executes proprietary command to force IE to cache all background images 
		which prevents the browser from fetching the same image repeatedly when using CSS hover background repositioning
	(http://support.microsoft.com/?scid=kb;en-us;823727&spid=2073&sid=global) */
if($.browser.msie && parseInt($.browser.version) === 6) {
	try {
		document.execCommand("BackgroundImageCache",false,true);
	} catch(e) {
		// just in case
	}
}


