$(document).ready(function() {
	
	var quoteRequestFunctionDate = $('#quoteRequestFunctionDate');
	if(quoteRequestFunctionDate.length > 0) $(quoteRequestFunctionDate).datePicker().val(new Date().asString()).trigger('change');
	
	var contactFunctionDate = $('#contactFunctionDate');
	if(contactFunctionDate.length > 0) $(contactFunctionDate).datePicker().val().trigger('change');

    bVersion = getInternetExplorerVersion();
    
    /* if ($("body").hasClass("frontpage")) {
        frontpage_image_resize();
        $(window).resize(frontpage_image_resize);
    } */

    $('.qaBlockQuestion').click(function(e) {
        $('.qaBlockAnswer').stop(true, true);
        selectedElement = $(this).parent();
        answerHeight = $('.qaBlockAnswerContainer', selectedElement).height();
        if(!$(selectedElement).hasClass('qaBlockQuestionOpen')) {
            if($('.qaBlockQuestionOpen').length == 0) {
                $('.qaBlockAnswer', selectedElement).animate({height: 15+answerHeight+'px'},500, "swing");
                $(selectedElement).addClass('qaBlockQuestionOpen');
            } else {
                $('.qaBlockQuestionOpen .qaBlockAnswer').animate({height: '0px'},500, "swing");
                $('.qaBlock').removeClass('qaBlockQuestionOpen');
                $('.qaBlockAnswer', selectedElement).animate({height: 15+answerHeight+'px'},500, "swing");
                $(selectedElement).addClass('qaBlockQuestionOpen');
            }            
        }
    });
    
    updateQuoteRequestProductCount();

	var addLinkText = 'Add <span>+</span>';
	var removeLinkText = 'Remove <span>-</span>';
	$('a.quoteRequestProductUpdate').click(function() {
        var theLink = $(this);
        var linkUrl = theLink.attr('href');
        $.ajax({
        	url: linkUrl + '?rest=true',
        	success: function() {
                if (theLink.hasClass('brownBtn')) {
                	var newLinkUrl = linkUrl.replace(/\/add\//, '/remove/');
                	theLink.removeClass('brownBtn').addClass('greenBtn').html(removeLinkText).attr('href', newLinkUrl);
                	//$('.proceedQuoteProductRecord').show();
                } else {
                	var newLinkUrl = linkUrl.replace(/\/remove\//, '/add/');
                	theLink.removeClass('greenBtn').addClass('brownBtn').html(addLinkText).attr('href', newLinkUrl);
                	//$('.proceedQuoteProductRecord').hide();
                }
                updateQuoteRequestProductCount();
        	},
		    error: function() { alert('an error occurred'); }
        });
        return false;
	});
});

function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function init() {
    $('a.popupLink').bind('click', function(e) { popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''),'width=550, height=400, scrollbars'); e.preventDefault(); });
}

function popupWindow(url, name, attributes){
	var newWindow = window.open(url, name, attributes);
	newWindow.focus();
}
 
function frontpage_image_resize() {
    var frontpage_image = $("#frontPageImage img");
    
    var w = frontpage_image.attr("width");
    var h = frontpage_image.attr("height");
    var r = w/h;
    
    var window_w = $(window).width();
    var window_h = $(window).height();
    var window_r = window_w/window_h;
    
    var new_w; var new_h;
    if (window_r >= r) {
        new_w = window_w;
        new_h = window_w/r;
    } else {
        new_w = window_h*r;
        new_h = window_h;
    }
    frontpage_image.attr("width", new_w);
    frontpage_image.attr("height", new_h);
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    	var c = ca[i];
    	while (c.charAt(0)==' ') c = c.substring(1,c.length);
    	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


function updateQuoteRequestProductCount() {
	var totalQuoteRequestProducts = ""+parseInt(readCookie('totalQuoteRequestProducts'));
	
	if(totalQuoteRequestProducts == '' || totalQuoteRequestProducts == 'null') {
		totalQuoteRequestProducts = 0;	
	}
	$('#headerQuoteRequestProductCount').html('&nbsp;(' + totalQuoteRequestProducts + ')');
}

