/*  
Site Name: 		HES
Site URI: 		healthedsolutions.com
Description: 	Health Eductaion Solutions Nelnet Company
Version: 		1.0
Author: 		Emir Plicanic
Author URI: 	Nelnet.com

*/

var CalculateShoppingCartTotal;

$(function () {

    /* DISABLE CLICKS AND HOVERS ON ALL ITEMS INSIDE ui-state-disabled
    -----------------------------------------------*/

    //disable clicks on all disabled items
    $(".ui-state-disabled a").click(function () {
        return false;
    });

    //disable hover
    $(".ui-state-disabled a").hover(function () {
        $(this).next("ul").hide();
        $(this).css({ "background": "none", "cursor": "default", "text-decoration": "none" });
    },
	    function () {
	        $(this).next("ul").hide();
	        $(this).css({ "background": "none", "cursor": "default" });
	    });

    /* END DISABLE CLICKS AND HOVERS
    -----------------------------------------------*/


    /* Start select box style
    -----------------------------------------------*/
    if ($("#select_course").length > 0) {
        $('select#select_course').selectmenu({
            width: 530,
            menuWidth: 530,
            style: 'dropdown',
            format: addressFormatting,
            select: function () {
                $(".step-two").addClass("step-two-active");
            },
            open: function () {
                $(".step-two").removeClass("step-two-active");
            }
        });
    }
    /* END select box style
    -----------------------------------------------*/

    /* Start cycle right column
    -----------------------------------------------*/
    //if($('.cycle').length > 0 ){
    $('.cycle').cycle({
        fx: 'fade',
        speed: 500,
        timeout: 8000,
        pager: '#pager',
        pause: 1,
        containerResize: 1,
        height: 'auto',
        pagerClick: function (zeroBasedSlideIndex, slideElement) {
            $('#pager a').removeClass("activeSlide");
            $('#pager a:eq(' + zeroBasedSlideIndex + ')').addClass("activeSlide");
            $('.cycle').cycle(slideElement);
            $('.cycle').cycle('pause');
            $('#pager').addClass("clickedNav");
        },
        pagerEvent: 'click.pause',
        updateActivePagerLink: function (element, index) {
            if ($('#pager').hasClass("clickedNav") == false) {
                $('#pager a').removeClass("activeSlide");
                $('#pager a:eq(' + index + ')').addClass("activeSlide");
            }
        }
    });
    //}
    /* END cycle right column
    -----------------------------------------------*/

    /* Button styles
    ---------------------------------------------------------------------*/
    $(".button").button().css({ 'color': '#fff' });

    /* End Button styles
    ---------------------------------------------------------------------*/

    /* CART FUNCTIONS 
    ---------------------------------------------------------------------*/
    //calculate totals funtion
    var cart_totals = function () {
        var total_itemprice = 0;
        var total_expedite = 0;
        var total = 0;
        $(".cart-item .item-price").each(function () {
            total_itemprice = total_itemprice + parseFloat($(this).val());
        });

        $(".cart-item .expedite").each(function () {
            if ($(this).is(":checked")) {
                total_expedite = total_expedite + parseFloat($(this).val());
            }
        });

        total = total_itemprice + total_expedite;
        return total.toFixed(2);
    };

    CalculateShoppingCartTotal = function() {
        if ($(".cart-item").length < 1) {
            $(".cart-item-total").text("Cart Empty");
            $(".btn-check-out").hide();
            $(".discount_code_container").hide();
        } else {
            $(".discount_code_container").show();
            $(".btn-check-out").show();
            $(".cart-item-total").text("TOTAL: $" + cart_totals());
        }
    };

    /* EXPEDITE SHIPPING PRICE
    ---------------------------------------------------------------------*/
    $(".expedite").click(function () {
        var expedite_price = 0;

        if ($(this).is(":checked")) {
            expedite_price = $(this).val();
            $(this).parent("label").parent("p").children(".expedite_price").text("$" + expedite_price);
        } else {
            $(this).parent("label").parent("p").children(".expedite_price").text("$0");
        }

        var cart_total = cart_totals();
        $(".cart-item-total").text("TOTAL: $" + cart_total);
    });

    /* END EXPEDITE SHIPPING
    ---------------------------------------------------------------------*/

    /* END CART FUNCTIONS
    ---------------------------------------------------------------------*/

    /* Browser selected styles
    ---------------------------------------------------------------------*/
    $("#catalog-browser li a").click(function () {
        $("#catalog-browser li a").removeClass("selected");
        $(this).addClass("selected");
        var selected = $(this).attr("id");
        if (selected != "") {
            $("#catalog-selection ul").slideUp("fast");
            $("#catalog-selection ." + selected).slideToggle("fast", function () { });

        } else {
            $("#catalog-selection ul").slideDown("fast", function () { });
        }


        return false;
    });

    /* End Browser selected styles
    ---------------------------------------------------------------------*/


    /* = Media Player
    ---------------------------------------------------------------------*/
    $.fn.media.defaults.flvPlayer = 'flash/player.swf';
    $('.media').media({ width: 420, height: 377, autoplay: false });

    /* = End Media Player
    ---------------------------------------------------------------------*/


    /* = Table Roll Over
    ---------------------------------------------------------------------*/
    $(".highlight-rows tr").hover(function () {
        $(this).children("td").css({ "background-color": "#f1f1f1" });
        $(this).children("td").children("img").stop().fadeTo('fast', 0.8);

    }, function () {
        $(this).children("td").css({ "background-color": "#fff" });
        $(this).children("td").children("img").stop().fadeTo('fast', 0.5);

    });

    /* = End Table Roll Over
    ---------------------------------------------------------------------*/



    /* = Side Tab Browsing
    ---------------------------------------------------------------------*/
    //page settings
    var navcontainer = "#services"; 	//Add to the UL conaining the links
    var itemcontainer = ".item"; 	//Add to the DIV around each section
    var itemwrap = "item_wrap"; 		//A DIV element with this ID will be wrapped around all items
    var itemwrapbottom = "item_wrap-bottom"; //A DIV containing bottom corners of the box
    var selecteditem = "selecteditem"; //class assigned to the currently selected link

    /*****STYLES BLOCK*****/
    // Styles can also be defined in a separate CSS file. if you do so comment out or delete styles block 
    var styles = '<style>' +
	'#' + itemwrap + ' {background: #fdfdfd url(images/layout/gray-box-431x892.gif) no-repeat left top; padding: 20px 30px; float: left; width: 371px; margin:20px 0px 40px 0px; position:relative;}' +
	navcontainer + ' {float:left;font-size:12px;margin:40px 0 0;padding:0;width:244px;list-style: none;}' +
	navcontainer + ' li{border-bottom:1px solid #E8E8E8; margin:0px; padding:0px;  -moz-border-radius-topleft: 6px; -webkit-border-top-left-radius: 6px; border-top-left-radius: 6px; -moz-border-radius-bottomleft: 6px; -webkit-border-bottom-left-radius: 6px; border-bottom-left-radius: 6px; }' +
	navcontainer + ' li a{display:block; text-decoration:none;line-height:12px;padding:8px 0px 8px 10px;outline:0;font-size:11px;}' +
	navcontainer + ' .' + selecteditem + '{background: #f1f1f1;}' +
	navcontainer + ' .' + selecteditem + ' a{color: #555;}' +
	'.' + itemwrapbottom + '{background: url(images/layout/gray-box-bottom-431x10.gif) no-repeat left bottom; position:absolute; width:431px; height:10px; bottom:0px; left:0px;}' +
	'</style>';
    //add styles to the head
    $("head").append(styles);
    /*****END STYLES BLOCK*****/

    //create item_wrap div
    $(itemcontainer + ":first").before('<div id="' + itemwrap + '"> <div class="item_wrap-bottom"></div></div>');
    //add items to wrap
    $(itemcontainer).appendTo("#" + itemwrap);
    //show only first item
    $(itemcontainer).not(":first").hide();
    //mark first link as selected
    $(navcontainer + " li:first").addClass(selecteditem);
    //services click
    $(navcontainer + " a").click(function () {
        $(navcontainer + " li").removeClass(selecteditem);
        $(this).parent("li").addClass(selecteditem);
        var li_index = $(navcontainer + " a").index(this);
        $(itemcontainer).hide().eq(li_index).slideToggle("fast");
        return false;
    });
    /* = End Side Tab Browsing
    ---------------------------------------------------------------------*/


    /* = Side Tab Browsing wide
    ---------------------------------------------------------------------*/
    //page settings
    var navcontainer_wide = "#services_wide"; 	//Add to the UL conaining the links
    var itemcontainer_wide = ".item_wide"; 	//Add to the DIV around each section
    var itemwrap_wide = "item_wrap_wide"; 		//A DIV element with this ID will be wrapped around all items
    var itemwrapbottom_wide = "item_wrap-bottom_wide"; //A DIV containing bottom corners of the box
    var selecteditem_wide = "selecteditem_wide"; //class assigned to the currently selected link

    /*****STYLES BLOCK*****/
    // Styles can also be defined in a separate CSS file. if you do so comment out or delete styles block 
    var styles_wide = '<style>' +
	'#' + itemwrap_wide + ' {background: #f1f1f1 url(images/layout/gray-box-835x600.gif) no-repeat left top; padding: 20px 30px; float: left; width: 775px; margin:20px 0px 40px 0px; position:relative;}' +
	navcontainer_wide + ' {float:left;font-size:12px;margin:40px 0 0;padding:0;width:140px;list-style: none;}' +
	navcontainer_wide + ' li{border-bottom:1px solid #E8E8E8; margin:0px; padding:0px;  -moz-border-radius-topleft: 6px; -webkit-border-top-left-radius: 6px; border-top-left-radius: 6px; -moz-border-radius-bottomleft: 6px; -webkit-border-bottom-left-radius: 6px; border-bottom-left-radius: 6px; }' +
	navcontainer_wide + ' li a{display:block; text-decoration:none;line-height:12px;padding:8px 0px 8px 10px;outline:0;font-size:11px;}' +
	navcontainer_wide + ' .' + selecteditem_wide + '{background: #f1f1f1;}' +
	navcontainer_wide + ' .' + selecteditem_wide + ' a{color: #555;}' +
	'.' + itemwrapbottom_wide + '{background:#f1f1f1 url(images/layout/gray-box-bottom-835x5.gif) no-repeat left bottom; position:absolute; width:835px; height:5px; bottom:0px; left:0px;}' +
	'</style>';
    //add styles to the head
    $("head").append(styles_wide);
    /*****END STYLES BLOCK*****/

    //create item_wrap div
    $(itemcontainer_wide + ":first").before('<div id="' + itemwrap_wide + '"> <div class="item_wrap-bottom_wide"></div></div>');
    //add items to wrap
    $(itemcontainer_wide).appendTo("#" + itemwrap_wide);
    //show only first item
    $(itemcontainer_wide).not(":first").hide();
    //mark first link as selected
    $(navcontainer_wide + " li:first").addClass(selecteditem_wide);
    //services click
    $(navcontainer_wide + " a").click(function () {
        $(navcontainer_wide + " li").removeClass(selecteditem_wide);
        $(this).parent("li").addClass(selecteditem_wide);
        var li_index_wide = $(navcontainer_wide + " a").index(this);
        $(itemcontainer_wide).hide().eq(li_index_wide).slideToggle("fast");
        return false;
    });
    /* = End Side Tab Browsing wide
    ---------------------------------------------------------------------*/


    /* = FAQ
    ---------------------------------------------------------------------*/

    //just some regular style sheets. change them as you see fit
    var styling = ".question{font-size:14px; font-weight:bold; cursor:pointer; line-height:normal; margin:15px 0px 10px 0px;}" +
					  ".answer{display:block; border-bottom:1px solid #f1f1f1;padding:10px 0px 10px 27px; margin-bottom:20px;}" +
					  ".opened{color:#666666;}" +
					  ".question span{margin-right:10px;}" +
					  ".closed{color:#FF9900;}";
    //attach style to the page
    var style = document.createElement("style");
    style.type = "text/css";
    try {
        style.appendChild(document.createTextNode(styling));
    } catch (e) {
        if (style.styleSheet) {
            style.styleSheet.cssText = styling;
        }
    }
    document.body.appendChild(style);
    //style all questions as closed
    $(".question").addClass("closed");

    //make sure first question is styled as open
    $(".question:first").removeClass("closed").addClass("opened");
    $(".answer").hide(); //hide answers
    $(".answer:first").show(); //show first answer
    //add icons
    $(".closed").prepend('<span class="ui-icon ui-icon-plusthick ui-corner-all alignleft"></span>');
    $(".opened").prepend('<span class="ui-icon ui-icon-minusthick ui-corner-all alignleft"></span>');
    //question click
    $(".question").click(function () {
        $(".answer").slideUp("fast");
        $(".question").removeClass("opened").addClass("closed");
        $(".question").children('.ui-icon').removeClass("ui-icon-minusthick").addClass("ui-icon-plusthick");

        if ($(this).next(".answer").is(":hidden")) {
            $(this).next(".answer").slideDown("fast");
            $(this).removeClass("closed").addClass("opened");
            $(this).children('.ui-icon').removeClass('ui-icon-plusthick').addClass('ui-icon-minusthick');

        }
    });
    /* = End FAQ
    ---------------------------------------------------------------------*/



    /* MAKE SURE navigation bar items on Windows are spaced appropriatly
    (due to differences in sub-pixel rendering between win & mac)
    ---------------------------------------------------------------------*/
    if (navigator.appVersion.indexOf("Win") != -1) {
        $("#nav a").css({ 'padding-right': '21px', 'padding-left': '21px' });
        $("#nav a").not("#nav ul li ul li a").hover(function () {
            if ($(this).parent("li").hasClass("firstlink")) {
                $(this).css({ 'padding-right': '20px', 'padding-left': '21px' });
            } else if ($(this).parent("li").hasClass("lastlink")) {
                $(this).css({ 'padding-right': '21px', 'padding-left': '20px' });
            } else {
                $(this).css({ 'padding-right': '20px', 'padding-left': '20px' });
            }
        }, function () {
            $(this).css({ 'padding-right': '21px', 'padding-left': '21px' });
        });
    }
    /* END Navigation bar
    ---------------------------------------------------------------------*/

});


		
//a custom format option callback
var addressFormatting = function(text){
	var newText = text;
	//array of find replaces
	var findreps = [
		{find:/^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>'},
		{find:/([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
		{find:/([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2'},
		{find:/([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
		{find:/(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-price">$1</span>'}
	];
	
	
	for(var i in findreps){
		newText = newText.replace(findreps[i].find, findreps[i].rep);
	}
	return newText;
}

