﻿google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");

google.setOnLoadCallback(function() {
$(document).ready(function() {
$(".accordian").hide();
        init();
    });
});

function init() {
	addNoSelect();
    rigNavigation();
    rigWysiwyg();
    rigEditor();
    rigTables();
    rigTreeView();
    rigGalleryOverlay();
    rigFaq();
    $("a.lightbox").fancybox();
    //rigH1Colour();
    simple_tooltip("#logo img", "tooltip");

}

function confirmBox(message) {
    var response = confirm(message);

    if (response == false) {
        return false;
    }
}

function simple_tooltip(target_items, name) {
    $(target_items).each(function(i) {
        $("body").append("<div class='" + name + "' id='" + name + i + "'><p>" + $(this).attr('alt') + "</p></div>");
        var my_tooltip = $("#" + name + i);

        $(this).removeAttr("title").mouseover(function() {
            my_tooltip.css({ opacity: 0.8, display: "none" }).fadeIn(400);
        }).mousemove(function(kmouse) {
            my_tooltip.css({ left: kmouse.pageX + 15, top: kmouse.pageY + 15 });
        }).mouseout(function() {
            my_tooltip.hide();
        });
    });
}


function rigH1Colour() {
    $("h1").css('color', $("h3").css('color'));
}

function addNoSelect() {
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function() {
			if ($.browser.mozilla) {//Firefox
				$(this).css('MozUserSelect', 'none');
			} else if ($.browser.msie) {//IE
				$(this).bind('selectstart', function() { return false; });
			} else {//Opera, etc.
				$(this).mousedown(function() { return false; });
			}
		});
	});
}

function rigWysiwyg() {
    var wysiwyg = $(".wysiwyg");

    if (wysiwyg.length > 0) {
        $(wysiwyg).rte({
                css: ['/content/css/screen.css'],
                controls_rte: rte_toolbar,
                controls_html: html_toolbar
        });
    }
}

function rigTables()
{
    $("tr:nth-child(odd)").addClass("odd");
}

function rigEditor() {
    $("input:radio[name=isPDF]").click(function() { checkMode(); })
    checkMode();
}

function checkMode() {
    var value = $("input:radio[name=isPDF]:checked").val();
    if (value == "true") {
        $("#PDFUpload").show(300);
        $("#HTMLContent").hide(300);
    }
    else {
        $("#PDFUpload").hide(300);
        $("#HTMLContent").show(300);
    }
}

function rigNavigation() {
    $(".navigationBar li").hover(function() { showChildren(this) }, function() { hideChildren(this) });
}

function showChildren(what) {

    var timer = 250;
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        $(" > ul", what).height($(" > ul", what).height());
        $(" > li", what).css({'margin-top': -6});
        $(" > ul", what).show();
        $(" > ul", what).width($(" > ul", what).width());
    }
    else {
        if ($(" > ul", what).parents('ul').size() >= 2) {
            $(" > ul", what).show('slide', { direction: 'left', easing: 'swing' }, timer);
        }
        else {
            //$(" > ul", what).parents('ul > li').addClass('current');
            $(" > ul", what).show('slide', { direction: 'up', easing: 'swing' }, timer);
        }
    
    }
}

function hideChildren(what) {
    if ($(what).parents('ul').size() == 1) {
            $(".navigationBar li").find('ul').hide();
        }
        else {
            $(what).find('ul:visible').hide();
        }
}



function rigTreeView() {
    $(".parentSelector").treeview({
        collapsed: true,
        animated: "medium",
        control: "#sidetreecontrol",
        persist: "location"
    });

    $(".parentSelector a").click(function(event) { updateSubOf(this); return false });
}

function updateSubOf(what) {
    $(".parentSelector a").removeClass("parentSelectedColor");
    $(what).addClass("parentSelectedColor");
    //var id = $(what).attr("rel");
    //$("#SubOf").val(id);
}

function formatCurrency(num) {
   	num = num.toString().replace(/\$|\,/g, '');
   	if (isNaN(num))
   		num = "0";
   	sign = (num == (num = Math.abs(num)));
   	num = Math.floor(num * 100 + 0.50000000001);
   	cents = num % 100;
   	num = Math.floor(num / 100).toString();
   	if (cents < 10)
   		cents = "0" + cents;
   	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
   		num = num.substring(0, num.length - (4 * i + 3)) + ',' +
num.substring(num.length - (4 * i + 3));
   	return (((sign) ? '' : '-') + '' + num + '.' + cents);
}

function rigGalleryOverlay() {
	$(".galleryOverlay").click(function() { alert(this.attr("href")); return null; });
}

function rigFaq() {
    var faq = $(".accordian");
    
    
    if (faq.html() != "") {
        //$(".accordian").hide();
        $("*[class^='accordian_toggle']").click(function() {
            $(this).next('.accordian').slideToggle('slow');
        });

        $("*[class^='accordian_toggle']").toggle(function() {
            $(this).removeClass('accordian_toggle_closed');
            $(this).addClass('accordian_toggle_open');
        }, function() {
            $(this).removeClass('accordian_toggle_open');
            $(this).addClass('accordian_toggle_closed');
        });
    };
}

function toggleAnswer(what) {
    $(this).next('div').slideToggle('slow');
}

function showTab(what) {
    $(what).closest(".tabs").find("li").removeClass("current");
    $(what).closest("li").addClass("current");
    var index = $(what).closest(".tabs").children("li").index($(what).closest("li"));
    var contentBlocks = $(what).closest(".tabGroup").children(".tabContent").hide();
    $(contentBlocks[index]).show();
}

function rigCurrencyChanger() {
    $(".flag.gb").click(function() { $.get("/async/changecurrency/0", function() { document.location = (document.location + "").split("#")[0]; }); });
    $(".flag.eu").click(function() { $.get("/async/changecurrency/1", function() { document.location = (document.location + "").split("#")[0]; }); });
    $(".flag.us").click(function() { $.get("/async/changecurrency/2", function() { document.location = (document.location + "").split("#")[0]; }); });
}

function showOverlay() {
    $("body").removeClass("printScope");
    $("html, body").animate({ scrollTop: 0 }, 100);
    //$("#overlayButtons").scrollFollow();
    $("#overlay").show(500, function() { fitOverlay() }).addClass("printScope");
    $("#overlay .closeButton").click(function() { hideOverlay(); });
}

function hideOverlay() {
    $("body").addClass("printScope");
    $("#overlay").hide(500).removeClass("printScope");
}

function fitOverlay() {
    $("#overlay").height($(document).height());
}