﻿// Vars
var bforexVar = {};
bforexVar.menuElemUl = "#menuElem";
bforexVar.loginLinksDiv = "#loginLinks";
bforexVar.ForgotPasswordLink = "#ForgotPasswordInner";
bforexVar.ForgotPassword = "#ForgotPassword";
bforexVar.forgotPasswordBox = ".forgotPasswordBox";
bforexVar.loginBox = "#loginBox";
bforexVar.closeBoxLink = ".closeBox";
bforexVar.innerMenuLi = ".rootCMSListMenuLI";
bforexVar.quotes_quotesNavigation = "#quotesNavigation a";
bforexVar.quotes_quotesContainer = ".quotesContainer";
bforexVar.toolsBox = ".toolsBox";
bforexVar.hiddenLoginUrl = "#hiddenLoginUrl";

// Funcs
var bforexFunc = {};

// show/hide submenu on hover
bforexFunc.handleMenu = function () {
    $(bforexVar.menuElemUl).children("li").hover(

    function () {
        $(this).addClass("hover").find("ul").show();
    },

    function () {
        $(this).removeClass("hover").find("ul").hide();
    });
};

// toggle "divID" visibility based on "action"
bforexFunc.toggleBoxView = function (divId, action) {
    var $div = $(divId);

    if (action === "show") {
        $div.fadeTo(0).show().fadeTo(200, 1);
    }
    else {
        $div.fadeTo(200, 0, function () { $div.hide(); });
    }
};

// handles "login", "forgot password" and "close box" links
bforexFunc.handleLogin = function () {

	// show box when link is clicked
	$(bforexVar.loginLinksDiv).children("a").click(function () {

		var $this = $(this),
            hrefAttr = $this.attr("href");

		bforexFunc.toggleBoxView(hrefAttr, "show");

		return false;

	});

	// show box when link is clicked
	$(bforexVar.ForgotPassword).click(function () {

		bforexFunc.toggleBoxView(bforexVar.forgotPasswordBox, "show");

		return false;

	});

	// hide box when close link is clicked
	$(bforexVar.closeBoxLink).click(function () {

		var hrefAttr = $(this).attr("href");
		bforexFunc.toggleBoxView(hrefAttr, "hide");
		return false;

	});

	// hide box when close link is clicked
	$(bforexVar.ForgotPasswordLink).click(function () {

		bforexFunc.toggleBoxView(bforexVar.forgotPasswordBox, "show");
		bforexFunc.toggleBoxView(bforexVar.loginBox, "hide");
		return false;

	});
};

// hide box when close link is clicked
function closeForgotPasswordBox() {

	bforexFunc.toggleBoxView(bforexVar.forgotPasswordBox, "hide");
	return false;

};

// handles quotes box
bforexFunc.handleQuotesBox = function () {

    var $quotesNavigationLinks = $(bforexVar.quotes_quotesNavigation),
        $quotesContainers = $(bforexVar.quotes_quotesContainer);

    $quotesNavigationLinks.first().addClass('active'); // add 'active' class to first tab link
    $quotesContainers.not(":first").css({ 'display': 'none' }); // hide all containers but the first one

    $quotesNavigationLinks.click(function () {

        var $this = $(this),
            containerId = $this.attr("href");

        $quotesNavigationLinks.removeClass('active'); // remove 'active' class from all tab links
        $this.addClass('active');  // add 'active' only to the clicked tab link

        if (containerId.length > 1) {
            $quotesContainers.stop(true, true).animate({ // hide all containers and then show only the corresponding one, based on the tab link href attr
                opacity: 0
            }, 400, function () {
                $(this).css({ 'display': 'none' });
                $(containerId).css({ 'display': 'block' }).fadeTo(400, 1);
            });
        }

        return false;

    });
};

bforexFunc.handleBoxClick = function () {
    $(bforexVar.toolsBox).click(function () {
        window.location = $(this).find(".toolsBox_textLink").attr("href");
        return false;
    });
};

// Reload VideoBox for Video Page
function callVideoButtonEvent(link) {
    if (link == 'FirstVideoLink')
        $(".FirstVideoButton").click();
    else
        $(".SecondVideoButton").click();
}

// Open PopUp for Print
var popupStatus = 0;

function OpenPrintPage() {

    $("#print_popup").html($("#mainBlock_center").html());
    $("#print_popup #printDiv").show();
    $("#print_popup #printIcon").hide();

    //getting height and width of the message box
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#print_popup").height();
    var popupWidth = $("#print_popup").width();

    //centering  
    $("#print_popup").css({
        "position": "absolute",
        "top": 90,
        "left": windowWidth / 2 - popupWidth / 2
    });

    if (popupStatus == 0) {
        $("#container").css({
            "opacity": "0.6"
        });
        $("#container").fadeIn("slow");
        $("#print_popup").fadeIn("slow");
        popupStatus = 1;
    }
}

function OpenNewsPage(divId) {	
	$("#print_popup").html($("#" + divId).html());

	//getting height and width of the message box
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#print_popup").height();
	var popupWidth = $("#print_popup").width();

	//centering  
	$("#print_popup").css({
		"position": "absolute",
		"top": 400,
		"left": windowWidth / 2 - popupWidth / 2
	});

	if (popupStatus == 0) {
		$("#container").css({
			"opacity": "0.6"
		});
		$("#container").fadeIn("slow");
		$("#print_popup").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup() {
    //disables popup only if it is enabled  
    if (popupStatus == 1) {
        $("#container").css({
            "opacity": "1"
        });
        $("#print_popup").fadeOut("slow");
        popupStatus = 0;
    }
}

// Print Content
function Print() {
    $("#print_popup").printArea();
}

function onDocumentReady() {
	bforexFunc.handleMenu();
	bforexFunc.handleLogin();
	bforexFunc.handleQuotesBox();
	bforexFunc.handleBoxClick();
}

// on document ready
$(onDocumentReady);

// Open New English blog Link
function openInNewWindow(link) {
   	// Change "_blank" to something like "newWindow" to load all links in the same new window
   	var newWindow = window.open(this.getAttribute(link), '_blank');
   	newWindow.focus();
   	return false;
}
