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

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');

	}

    addCheckRate();

	$('#topMenu table th').first().addClass("first");
	$('#topMenu table th').reverse().first().addClass("last");
    $('#topMenu table th')
        .bind('click', function(e) {

            if (e.target.nodeName != 'A') {

                window.location = $(this).find('a').attr('href');

            }

        })
        .mouseover(function(e) { $(this).addClass('menuhover'); })
        .mouseout(function(e) { $(this).removeClass('menuhover'); });

	if ($('#topMenu table th.selected a').html() == 'Home') {

		$('#latestSites').append('<div id="topMenuPageIndicator"></div>');

	} else {

		$('#topMenu table th.selected').prepend('<div id="topMenuPageIndicator"></div>');

	}

	if ($('#topMenuPageIndicator').length > 0 && !($.browser.msie && $.browser.version.substr(0,1) == '7')) {

		$selTH  = $('#topMenu table th.selected');
		offset = $.browser.msie ? 0 : $selTH.offset().left - $selTH.parent().offset().left;
		$left   = offset + ($selTH.width() / 2);

		$('#topMenuPageIndicator').css('left', $left).show();

	}

	$('#siteHeader #accountLogin')
		.prepend('<div class="acclog_left"></div><div class="acclog_right"></div>')
		.hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); })
        .bind('click', function(e) {

            if (e.target.nodeName != 'A') {

				//Open new window
                window.open($(this).find('a').attr('href'), 'InnuoClientLogin');

            }

        });

/*
	var footerHeight = $('#siteFooter').height();
	var footerPadding = parseInt($('#siteFooter').css('padding-top')) + parseInt($('#siteFooter').css('padding-bottom'));
	var windowHeight = $(window).height();
	var bodyHeight = $('body').height() + footerHeight;

$('body').append('<span><!-- Footer: '+footerHeight+', '+
				 'Footer Padding: '+footerPadding+', '+
				 'Window: '+windowHeight+', '+
				 'Body: '+bodyHeight+', '+
				 'Top: '+(Math.max(bodyHeight, windowHeight) - footerHeight)+'px'+' --></span>');
*/
//	$('body').css('padding', '0px 0px '+(footerHeight + footerPadding)+'px 0px');
//	$('#siteFooter').css('top', (Math.max(bodyHeight, windowHeight) + footerPadding - footerHeight)+'px').show();

	$('.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'); });

	//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);

	});

	//Use jQuery corners....
//    $('.shadowedBox').corner();

    //... or use CSS's DIVs
/*
    $('.shadowedBox')
        .append('<div class="decor shadowedBoxTop"></div>' +
                 '<div class="decor shadowedBoxRight"></div>' +
                 '<div class="decor shadowedBoxBottom"></div>' +
                 '<div class="decor shadowedBoxLeft"></div>' +
                 '<div class="decor shadowedBoxTL"></div>' +
                 '<div class="decor shadowedBoxTR"></div>' +
                 '<div class="decor shadowedBoxBL"></div>' +
                 '<div class="decor shadowedBoxBR"></div>');
*/

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

});
