﻿var wordhelpActive = false;

$(document).ready(function () {
    initMenuBar();

    initColHeights();

    initGradients();

    initToolTip();

    initActionMenu();

    initShadows();
});

var mlogTimer;

function initMenuBar() {
	// show menu bar if needed
	if ($("#menu li.mi1").hasClass("selected")) {
		$("#cols .menu-bar").addClass("bar1");
		$(".cols-menu").addClass("theme1");		
	}
	else if ($("#menu li.mi2").hasClass("selected")) {
		$("#cols .menu-bar").addClass("bar2");
		$(".cols-menu").addClass("theme2");
	}
	else if ($("#menu li.mi3").hasClass("selected")) {
		$("#cols .menu-bar").addClass("bar3");
		$(".cols-menu").addClass("theme3");
	}
	else if ($("#menu li.mi4").hasClass("selected")) {
		$("#cols .menu-bar").addClass("bar4");
		$(".cols-menu").addClass("theme4");
	}
	else if ($("#menu li.mi5").hasClass("selected")) {
		$("#cols .menu-bar").addClass("bar5");
		$(".cols-menu").addClass("theme5");
	}
    else if ($("#menu li.mi6").hasClass("selected")) {
        $("#cols .menu-bar").addClass("bar6");
        $(".cols-menu").addClass("theme6");
    }
    else if ($("#menu li.mi7").hasClass("selected")) {
        $("#cols .menu-bar").addClass("bar7");
        $(".cols-menu").addClass("theme7");
    }
    else if ($("#menu li.mi8").hasClass("selected")) {
        $("#cols .menu-bar").addClass("bar8");
        $(".cols-menu").addClass("theme8");
    }
    else if ($("#menu li.mi9").hasClass("selected")) {
        $("#cols .menu-bar").addClass("bar9");
        $(".cols-menu").addClass("theme9");
    }
    else if ($("#menu li.mi10").hasClass("selected")) {
        $("#cols .menu-bar").addClass("bar10");
        $(".cols-menu").addClass("theme10");
    }
    else if ($("#menu li.mi11").hasClass("selected")) {
        $("#cols .menu-bar").addClass("bar11");
        $(".cols-menu").addClass("theme11");
    }
    else if ($("#menu li.mi12").hasClass("selected")) {
        $("#cols .menu-bar").addClass("bar12");
        $(".cols-menu").addClass("theme12");
    }

    if ($("#cols .menu-bar").attr("class") != "menu-bar") {
        
        var col = increase_brightness($("#cols .menu-bar").css("background-color"), 75);

        $(".cols-menu .active").css("background-color", col);
        $(".cols-menu .active a").css("color", "#595852");
    }
    
    /*Standard table styling*/
    $(".standardtable td:first-child").css("background-color", increase_brightness("#9A937D", 75));
    $(".standardtable").each(function (i, el) {
        $("tr:first", el).each(function (v, elm) {
            $("td", elm).css("background-color", increase_brightness("#9A937D", 75));
            $("td:first", elm).css("background-color", increase_brightness($("#cols .menu-bar").css("background-color"), 75));
        });
        $("tr", el).each(function (x, elmt) {
            $("td:last", elmt).css("border-right", "0px");
        });
    });
    $(".standardtable").css("border-color", $("#cols .menu-bar").css("background-color"));    
    $(".standardtable td").css("border-color", $("#cols .menu-bar").css("background-color"));
    /*Standard table styling end*/

    $("#menu-login").bind("mouseover", function () {
        if ($("#menu-login-dropdown:hidden").length > 0) {
            $("#menu-login-dropdown").css("top", $("#menu-login").offset().top + $("#menu-login").height() + 6);
            $("#menu-login-dropdown").css("left", $("#menu-login").offset().left);
            $("#menu-login-dropdown").slideDown("fast");
            $("#menu-login-dropdown").hover(function () {
                clearTimeout(mlogTimer);

                $("#menu-login-dropdown a").hover(
                    function () {
                        var p = $(this).offset();

                        $("#menu-login-dropdown .active-efx").css("top", p.top - 141);
                        $("#menu-login-dropdown .active-efx").show();

                    }, function () {
                        $("#menu-login-dropdown .active-efx").hide();
                    }
                );

            }, function () {
                clearTimeout(mlogTimer);
                $("#menu-login-dropdown").slideUp("fast");
                $("#menu-login-dropdown .active-efx").hide();
            });

            mlogTimer = setTimeout("$('#menu-login-dropdown').slideUp('fast');", 2000);
        }
    });
}

function initActionMenu() {
	$("#actionWordHelp").toggle(
		function () {
			$(this).addClass("active");
			wordhelpActive = true;
			toggleWordHelp();
			Set_Cookie('skandiawordhelp', 'true', '', '/', '', '');
		},
		function () {
			$(this).removeClass("active");
			wordhelpActive = false;
			toggleWordHelp();
			Set_Cookie('skandiawordhelp', 'false', '', '/', '', '');
		}
	);

	$("#actionPrint").click(function () {
		window.print();
	});
}

function initColHeights() {
	// justify col heights
	if (window.location == window.parent.location) {
		var max = 0;
		$("#cols > div").not(".clear, .menu-bar").each(function() {
			if ($(this).height() > max)
				max = $(this).height();
		});
        $("#cols > div").not(".clear, .menu-bar").each(function () {
            $(this).css("min-height", max);
		    $(this).css("_height", max);
		});
	}
}

function initShadows() {
	// insert boxshadows
	$(".boxshadow").each(function () {
		$(this)
			.append("<div class='shadow-right'></div>")
			.append("<div class='shadow-bottom'></div>")
			.append("<div class='shadow-corner'></div>")
			.append("<div class='shadow-corner-left'></div>")
			.append("<div class='shadow-corner-top'></div>");
	});
}

function initGradients() {
	// insert gradient bars
	var selBar = $(".menu-bar");

	if (selBar.hasClass("bar1")) {
	    selBar.gradient({ from: '62A146', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: '62A146', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#62A146");
	} else if (selBar.hasClass("bar2")) {
	    selBar.gradient({ from: '60AFDD', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: '60AFDD', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#60AFDD");
	} else if (selBar.hasClass("bar3")) {
	    selBar.gradient({ from: 'EAAF0F', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: 'EAAF0F', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#EAAF0F");
	} else if (selBar.hasClass("bar4")) {
	    selBar.gradient({ from: '006188', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: '006188', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#006188");
	} else if (selBar.hasClass("bar5")) {
	    selBar.gradient({ from: '004236', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: '004236', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#004236");
	} else if (selBar.hasClass("bar6")) {
	    selBar.gradient({ from: 'D14414', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: 'D14414', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#D14414");
	} else if (selBar.hasClass("bar7")) {
	    selBar.gradient({ from: 'CF0072', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: 'CF0072', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#CF0072");
	} else if (selBar.hasClass("bar8")) {
	    selBar.gradient({ from: '006F53', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: '006F53', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#006F53");
	} else if (selBar.hasClass("bar9")) {
	    selBar.gradient({ from: 'BCDC05', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: 'BCDC05', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#BCDC05");
	} else if (selBar.hasClass("bar10")) {
	    selBar.gradient({ from: '63305E', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: '63305E', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#63305E");
	} else if (selBar.hasClass("bar11")) {
	    selBar.gradient({ from: '9A937D', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: '9A937D', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#9A937D");
	} else if (selBar.hasClass("bar12")) {
	    selBar.gradient({ from: 'AD8799', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 197 });
	    $(".barAuto").gradient({ from: 'AD8799', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#AD8799");
	}
	else {
		$(".barAuto").gradient({ from: '9e957e', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 }).css("backgroundColor", "#9e957e");
	}

    $(".bar1").gradient({ from: '62A146', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar2").gradient({ from: '60AFDD', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar3").gradient({ from: 'EAAF0F', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar4").gradient({ from: '006188', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar5").gradient({ from: '004236', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar6").gradient({ from: 'D14414', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar7").gradient({ from: 'CF0072', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar8").gradient({ from: '006F53', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar9").gradient({ from: 'BCDC05', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar10").gradient({ from: '63305E', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar11").gradient({ from: '9A937D', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });
    $(".bar12").gradient({ from: 'AD8799', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });

	/*$(".bar").gradient({ from: '9e957e', to: 'f5f4f2', direction: 'vertical', position: 'right', length: 67 });*/
}

/*
Basil Green		[ #62A146 ]
Sky Blue		[ #60AFDD ]
Warm Yellow		[ #EAAF0F ]
Ocean			[ #006188 ]
Deep Forest		[ #004236 ]
Hot Red			[ #D14414 ]
Magenta			[ #CF0072 ]
Skandia Green	[ #006F53 ]
Lime			[ #BCDC05 ]
Violet			[ #63305E ]
Warm Silver		[ #9A937D ]
Lavender		[ #AD8799 ]
*/

var arDictionary = new Object();

function initToolTip() {
	// Get dictionary
	$.ajax({url: "/ui/handlers/dictionaryfeed.ashx", cache: false, success: toolTipData});
}

function toolTipData(xml) {
	arDictionary = $.xml2json(xml);

	setupWordHelp();

	if (Get_Cookie('skandiawordhelp') == "true") {
	    if (!$("#actionWordHelp").hasClass("active")) {
	        $("#actionWordHelp").click();
	    }
	}	
}

function setupWordHelp() {
	/*if (jQuery.browser.msie && jQuery.browser.version < 7) {
		$("#tooltip-container")
					.attr('writing-mode', 'tb-rl');
					.css('background-image', 'none')
					.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="/ui/images/tooltip/black_arrow.png",sizingMethod="scale")');
	}*/

    /*
	var len = arDictionary.entry.length;

	for (var i = 0; i < len; i++) {
		if (arDictionary.entry[i].isPublished == "true" || arDictionary.entry[i].isPublished == "True") {
			$('.cols-content div, .cols-content p').replaceText(arDictionary.entry[i].word, '<span class="tip disabled" title="' + String(arDictionary.entry[i].tooltip).replace("\n", "<br /><br />") + '">$1<\/span>', false);
		}
	}*/

    //$('.cols-content .tooltip').tooltip({ tip: '#tooltip-container', relative: false, effect: 'toggle', slideOffset: 10, direction: 'down', predelay: 300, bounce: true });

    //toggleWordHelp();
    /*
    $('.tip[title]').tooltips({
        tip_follows_cursor: "on", //on/off
        tip_delay_time: 300 //milliseconds
    });*/

}

var initWordhelp = false;

function toggleWordHelp() {
    if (wordhelpActive) {
        if (!initWordhelp) {

            var len = arDictionary.entry.length;

            for (var i = 0; i < len; i++) {
                if (arDictionary.entry[i].isPublished == "true" || arDictionary.entry[i].isPublished == "True") {
                    $('.cols-content div, .cols-content p').replaceText(arDictionary.entry[i].word, '<span class="tip disabled" title="' + String(arDictionary.entry[i].tooltip).replace("\n", "<br /><br />") + '">$1<\/span>', false);
                }
            }

            $('.tip[title]').tooltips({
                tip_follows_cursor: "on", //on/off
                tip_delay_time: 300 //milliseconds
            });

            initWordhelp = true;
        }
		$('#cols .cols-content .tip.disabled').each(function (index) {
			$(this).removeClass("disabled");
			/*var elem = $(this).data('tooltip');
			elem.show = elem.orig_show;*/
		});
	}
	else {
		$('#cols .cols-content .tip').each(function (index) {
			if(!$(this).hasClass("disabled"))
				$(this).addClass("disabled");
			/*var elem = $(this).data('tooltip');
			elem.orig_show = elem.show;
			elem.show = function () { };*/
		});        
	}
}

function getCirclePoint(radius, step, stepMax, centerX, centerY) {

    var p = new Object();

    p.x = centerX;
    p.y = centerY;

    p.x = (centerX + radius * Math.cos(2 * Math.PI * (step - 1) / stepMax));
    p.y = (centerY + radius * Math.sin(2 * Math.PI * (step - 1) / stepMax));

    return p;
}

function mouseX(evt) {
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
    else return null;
}
function mouseY(evt) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
    else return null;
}

function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
    if (!isNaN(x)) {
        return ("0" + parseInt(x).toString(16)).slice(-2);
    } else {
        return ("0" + "0");
    }

}

function increase_brightness(color, percent) {
    var hex = color;
    if (hex.substr(0, 3) == 'rgb') {
        hex = rgb2hex(color)
    }
    var r = parseInt(hex.substr(1, 2), 16),
        g = parseInt(hex.substr(3, 2), 16),
        b = parseInt(hex.substr(5, 2), 16);
    return '#' +
       ((0 | (1 << 8) + r + (256 - r) * percent / 100).toString(16)).substr(1) +
       ((0 | (1 << 8) + g + (256 - g) * percent / 100).toString(16)).substr(1) +
       ((0 | (1 << 8) + b + (256 - b) * percent / 100).toString(16)).substr(1);
}

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? ";secure" : "");
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) 
        document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
