// JavaScript Document
jQuery.fn.reverse = [].reverse;
$.noConflict();

var RecaptchaOptions = { theme : 'white' };

jQuery(document).ready(function($) {


	var agent   = navigator.userAgent.toLowerCase(); 
	var browser = false;

    var PAGE = window.location.href.replace(/\/?.*\//, '').replace(/\?.*/, '');
    PAGE = (PAGE == '' ? 'index.php' : PAGE);

	if ($.browser.msie) {

		$('body').addClass('ie');
		$('body').addClass('ie'+$.browser.version.substr(0,1));

	} else if (agent.match(/iphone/i)) {

		$('body').addClass('iphone');

	}

	$(':first-child').addClass('first-child');
	$(':last-child').addClass('last-child');

	$('body#homePage #sociallinks').appendTo('#homescroller');

	if ($('#sidebar') && $('#sidebar + .whitecontainer')) {

		var sidebarheightdiff = 20;

		var sidebarheight = $('#sidebar').height();
		var containerheight = $('#sidebar + .whitecontainer').height();
		var sidebarpadding = parseInt($('#sidebar').css('padding-top')) + parseInt($('#sidebar').css('padding-bottom'));

		if (sidebarheight + sidebarpadding < containerheight) {
			//Sidebar is SHORTER than the content next to it - Extend the SIDEBAR height

			$('#sidebar').css('min-height', containerheight - sidebarpadding - sidebarheightdiff);
			$('#sidebar + .whitecontainer').css('min-height', containerheight);

		} else {
			//Sidebar is LONGER than the content next to it - Extend the CONTENT height

			$('#sidebar + .whitecontainer').css('min-height', sidebarheight + sidebarpadding + sidebarheightdiff);
			$('#sidebar').css('min-height', sidebarheight);

		}

	}

    addCheckRate();

	$('#topMenu table th').first().addClass("first");
	$('#topMenu table th').reverse().first().addClass("last");
    $('#topMenu table th')
        .mouseover(function(e) { $(this).addClass('menuhover'); })
        .mouseout(function(e) { $(this).removeClass('menuhover'); });

	$('.hoverImage')
		.mouseover(function(e) { $(this).find('.default').first().css('display', 'none'); $(this).find('.hover').first().css('display', 'block'); })
		.mouseout(function(e) { $(this).find('.hover').first().css('display', 'none'); $(this).find('.default').first().css('display', 'block'); });

	$('.clearOnFocus').focus(function() { if ($(this).val() == $(this).attr('alt')) { $(this).val(''); } $(this).addClass('hasfocus'); $(this).addClass('hascontent'); });
	$('.clearOnFocus').blur(function()  { if ($(this).val() == '') { $(this).val($(this).attr('alt')); $(this).removeClass('hascontent'); } $(this).removeClass('hasfocus'); });

	$('.clearParentOnFocus').focus(function() { $(this).css('background-color', $(this).parent().css('background-color')); });
	$('.clearParentOnFocus').blur(function()  { if ($(this).val() == '') { $(this).css('background-color', 'transparent'); } });
	$('.clearParentOnFocus').each(function() { if ($(this).val() != '') { $(this).css('background-color', $(this).parent().css('background-color')); } });

	//Validate newsletter form (basic validation!)
	if ($('.newsletter_form').length > 0) {

		$('.newsletter_form').submit(function() {

			var returnVal = true;
			var firstOffender = false;

			$(this).find('input:text').each(function() {

				if (firstOffender == false) {

					if ($(this).val() == '' || $(this).val() == $(this).attr('alt')) {
						returnVal = false;
						firstOffender = $(this);
					} else if ($(this).hasClass('email-input')) {
						if ($(this).val().indexOf('@') < 0 || $(this).val().lastIndexOf('.') < $(this).val().indexOf('@')) {
							returnVal = false;
							firstOffender = $(this);
						}
					}

				}

			});

			firstOffender.focus();
			return returnVal;

		});

	}
	//End Validate newsletter form

	//Orders page
	$('.collapsible .packageIntro').click(function(e) {

		$pl = $(this).siblings('.productList');

		if ($.browser.msie) {

			$pl.toggle();

		} else if ($pl.is(':visible')) {

			$(this).find('.plus').show();
			$(this).find('.minus').hide();
			$pl.slideUp('slow');

		} else {

			$(this).find('.plus').hide();
			$(this).find('.minus').show();
			$pl.find('div.prodDesc').hide();
			$pl.slideDown('slow');

		}

	});

	//Orders page
	$('.productList .readMore').click(function(e) {

		$pd = $(this).closest('tr').next('tr').find('div.prodDesc').first();

		if ($.browser.msie) {

			$pd.toggle();

		} else if ($pd.is(':visible')) {

			$pd.slideUp('slow');

		} else {

			$pd.slideDown('slow');

		}

		//Cancel the anchor tag click
		return false;

	});

	//Orders page
	$('.productList .prodRow').click(function(e) {

		if (e.target.tagName != 'INPUT') {

			$(this).find(':checkbox').first().click();

		}

	});

	//Orders page
	$('.singleselect :checkbox').click(function(e) {

		var state = ($(this).is(':checked') ? 'checked' : '');

		$(this).closest('.singleselect').find(':checkbox').removeAttr('checked');
		$(this).attr('checked', state);

	});

	$('.submitOnChange').change(function() { $(this).closest('form').submit(); });

});

