/**
 * @author jasonroy
 */

var rowCounter = 0;
var isOptinChecked = false;
$(document).ready(function()
{
    $('.leadModule .row:even').addClass('alternate');
	$('.leadModule .row:last').addClass('last');
	
	adjustMatrixPage();
	if($.browser.msie)
	{
		$('.leadModule select').width('326px');
		fixFunctionButtons();
		if($.browser.version < 7)
		{
			
		}
		fixIEHover();

	}
	if($.browser.msie || $.browser.mozilla)
	{
	}

	checkIfUserLoggedIn();
    adjustHeights();
    adjustHomePage();
    adjustProductDetailPage();
    setupBreadCrumb();
	adjustTagControls();
	fixInputClasses();
	adjustLeadForm();
})

function fixIEHover()
{
    $('input[type=button]').hover(function()
    {
        $(this).addClass('hover');
    }, function()
    {
        $(this).removeClass('hover');
        $(this).removeClass('active');
    })
    $('input[type=button]').mousedown(function()
    {
        $(this).addClass('active');
    })
    $('input[type=button]').mouseup(function()
    {
        $(this).removeClass('active');
    })
    
}
function adjustLeadForm()
{
    $('.researchfields :checkbox').click(function()
    {
        if (!isOptinChecked) 
        {
            $('.optin :checkbox').attr('checked', true)
            isOptinChecked = true;
        }
    })
}


function fixFunctionButtons()
{
	$('.functionButtonPanel .button').wrap('<span class="buttonWrapper"></span>');
	$.each($('.functionButtonPanel'), function(){ 
		$(this).find('.buttonWrapper:first').addClass('final');
	})
}
function checkIfUserLoggedIn()
{
	if(String($('.welcome').text().toLowerCase()).indexOf('welcome') > -1)
	{
		$('body').addClass('loggedIn')
	}
}
function adjustTagControls()
{
	$('.tagButton').val('Add tags')
	
}
//Temporary fix
function fixInputClasses()
{
	$('input[type=text]').addClass('textbox');
	$('select').addClass('dropdownList');
}

function adjustHeights()
{
	var breadCrumbHeight = 0;
	if(jQuery('.contentPane').height() < jQuery('.leftSidebar').height())
	{
		if(!jQuery('form').hasClass('Home'))
		{
			breadCrumbHeight = 40;
		}
		jQuery('.contentPane').height(jQuery('.leftSidebar').height() - breadCrumbHeight)
	}
}
function setupBreadCrumb()
{
	jQuery("body").append('<div class="chevronOverlay main"></div>');
	jQuery(".breadcrumbMap").addClass("breadCrumb").jBreadCrumb(
        {
			maxFinalElementLength: 250,
			minFinalElementLength: 150,
	        minimumCompressionElements: 2,
	        endElementsToLeaveOpen: 0,
	        beginingElementsToLeaveOpen: 1
		});
}

function adjustMatrixPage()
{
    //Matrix Page Adjustments
    $('.matrixModule .functionButtonPanel:last').addClass('last');

	$('.functionButtonPanel').append('<div class="pointer"></div>')
	
}

function adjustHomePage()
{
	jQuery.each(jQuery('.Home .directoryModule ul li ul'), function(i, element){
		jQuery(element).find('a:not(:last)').append(',&nbsp;&nbsp;');
	});	
}
function adjustProductDetailPage()
{
	
	jQuery('.productDetailModule table tr:first').addClass('first');
	jQuery.each(jQuery('.productDetailModule table tr'), function(i, element){
		if(jQuery(element).hasClass('productDetailSectionHeader'))
		{
			rowCounter = 1;
		}
		else if(rowCounter == 1)
		{
			jQuery(element).addClass('alternate');
			rowCounter = 0;
		}
		else
		{
			rowCounter = 1;
		}
		if(jQuery(element).children('td[colspan=2]').length > 0)
		{
			jQuery(element).addClass("twoRow");
		}
	})
	
	if(jQuery('.ProductDetail .productDetailHeaderImage').find('img').length > 0)
	{
		jQuery('.ProductDetail').addClass('withImage');
	}
	
}

/*
 * IE Hover State Fix
 */





