﻿/// <reference path="jquery-1.2.6-vsdoc.js" />

$(document).ready(function() {
    document.cookie = "cookie_test:test";
    if (document.cookie.indexOf("cookie_test") < 0) {
        $("<div id='no_script_error' class='error'><h1>You must have cookies enabled to view this website.</h1></div>").prependTo('body');
    }

    $('ul.sf-menu').supersubs({
        extraSubs: 1,
        speed: 100,
        delay: 200
    }).superfish().find('ul').bgIframe({ opacity: false });
    
    // create drop shadow on sidebar
	resizeBar();
	
	$('#create_quote').prepend('<div id="sidebar_drop_shadow">&nbsp;</div>');
	$('#create_quote').css('position', 'relative');
	$('#sidebar_drop_shadow').css({
		'background': 'transparent url(/Images/quote-shadow.png) repeat-y top right',
		position: 'absolute',
		right: '-4px',
		top: '0px',
		height: '100%',
		'z-index': '2000'
	});
	
	$('#main_navigation').prepend('<div id="top_menu_drop_shadow">&nbsp;</div>');
	$('#main_navigation').css('position', 'relative');
	$('#top_menu_drop_shadow').css({
		'background': 'transparent url(/Images/top-menu-shadow.png) repeat-x',
		position: 'absolute',
		right: '0px',
		top: '26px',
		width: '100%',
		'z-index': '3'
	});
	
	if ($.browser.msie && parseFloat($.browser.version) < 7) { 
			$('#content_pane').css('width', '660px').css('margin', '-3px');
	}
	
	$('#headerSignOut').bind('click', function(){
		AjaxManager.Customer.signOut();
		return false;
	});
	
	topMenuSubscribeToPublishers()
});

$(function() {
    if (readCookie('session_guid') == null || readCookie('session_guid') == '') {
        SESSION_GUID = $.createGuid();
        document.cookie = 'session_guid=' + SESSION_GUID + '; path=/';
    } else {
        SESSION_GUID = readCookie('session_guid');
    }
    
    setCustomerLoggedInState(AjaxManager.Customer.getAccount());
    $('#header_signin').show();
});

function topMenuSubscribeToPublishers(){
	setCustomerLoggedInState.subscribe(AjaxManager.Customer.onSignedIn);
	setCustomerLoggedInState.subscribe(AjaxManager.Customer.onNewAccountCreated);
	setCustomerLoggedInState.subscribe(AjaxManager.Customer.onAccountUpdated);	
	setCustomerLoggedOutState.subscribe(AjaxManager.Customer.onSignedOut);
}

function setCustomerLoggedInState(account){
    if (account === null || account === undefined) {
        return;
    }
	var displayName = (account.Name.length > 0)  ? account.Name : account.EmailAddress;
	$('#headerUserName').empty();
	$('#headerUserName').append('&nbsp;' + displayName);
	
	$('#headerSignInMessages').hide();
		
	$('#headerSignOut').show();
	$('#headerUserName').show();
}

function setCustomerLoggedOutState(){
	$('#headerSignInMessages').show();
		
	$('#headerSignOut').hide();
	$('#headerUserName').hide();
	$('#headerUserName').empty();
	
	$.deleteEntireCookieForGuid(SESSION_GUID, false);
	
	if( /.*\/Home\/SignIn.aspx/i.test(document.location)){
		return;
	}
	
	document.location = "/Home.aspx";
}

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 resizeBar() {
	if ($.browser.msie) {
		$('img[src$=.png]').ifixpng(); 
	}
}

function formatPhone(val) {
    if (val === null || val === '') {
        return null;
    }

    var phone = '';
    if (val.match(/[a-zA-Z]/)) {
        phone = '1-800-STEEMER';
    } else {
        phone = '(';
        val = val.split('');

        for (var i = 0; i < val.length; i++) {
            phone += val[i];

            if (i == 2) {
                phone += ')';
            } else if (i == 5) {
                phone += '-';
            }
        }
    }
    return phone;
} 
