﻿/* Lightbox in jQuery
-------------------------------------------------------------------------------------------------------------------------------------*/

/* Sets the lightbox */
//0 means disabled; 1 means enabled;
var lightboxStatus = 0;
var dialogName = "";

/* loading lightbox with jQuery magic */
function loadLightbox(){
	//loads popup only if it is disabled
	if(lightboxStatus==0){
		jQuery("#Lightbox").css({"opacity": "0.7"});
		jQuery("#Lightbox").fadeIn("slow");				
		jQuery(dialogName).fadeIn("slow");
		lightboxStatus = 1;
	}
}

/* Disables the lightbox with jQuery magic */
function disableLightbox(){
	//disables lightbox only if it is enabled
	if(lightboxStatus==1){
		jQuery("#Lightbox").fadeOut("slow");
		jQuery(".Lightbox_Dialog").fadeOut("slow");
		lightboxStatus = 0;		
	}
	
	dialogName = "";
}

/* centers the lightbox */
function centerLightbox(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var lightboxHeight = jQuery(dialogName).height();
	var lightboxWidth = jQuery(dialogName).width();
	
	//centering - only need force for IE6
	jQuery(dialogName).css({
		"position": "absolute",
		//"top": windowHeight/2-lightboxHeight/2,
		"top": 90,
		"left": windowWidth/2-lightboxWidth/2
	});
		
	jQuery("#Lightbox").css({"height": windowHeight});	
}

function ScrollToTop() {
    jQuery('html, body').animate({scrollTop:0}, 'slow', 
	    function() {                
            centerLightbox(dialogName); //centering with css		
	        loadLightbox(dialogName);   //load lightbox
        }
    ); 	
}

jQuery(window).resize(function() {
	if(lightboxStatus==1){
        centerLightbox();
    }                      
});

//Resets the flying contact us URL
function ResetIframeContactUsURL() {   
    var url = document.location.href;    
    if (url.toLowerCase().indexOf("/tabid/165/") >= 0) {        
        jQuery('iframe#Iframe_ContactUs').attr('src', "/FlyingContactUs/tabid/164/Default.aspx");
        jQuery('iframe#Iframe_ContactUs').load(function() { });
    }
}

//Controlling events in jQuery
jQuery(document).ready(function() {

    //Loads lightbox - For terms of use
    jQuery("#TermsOfUse").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    /* Group of Images
    -----------------------------------------------------------------------------------------------------*/
    //Loads lightbox - For Complex & Advanced Orders
    jQuery("#Complex_Orders_1").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - For Complex & Advanced Orders
    jQuery("#Complex_Orders_2").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - For Complex & Advanced Orders
    jQuery("#Custom_Spread_Trading_1").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - For Complex & Advanced Orders
    jQuery("#LME_Outrights_Carries_1").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });


    /* Global Vision Software Download 
    -----------------------------------------------------------------------------------------------------*/
    //Loads lightbox - Coal
    jQuery("#Coal").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - Emissions
    jQuery("#Emissions").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - For Complex & Advanced Orders
    jQuery("#EuropeanPower").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - Freight
    jQuery("#Freight").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - Gasoline
    jQuery("#Gasoline").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - NaturalGas
    jQuery("#NaturalGas").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    //Loads lightbox - UKPower
    jQuery("#UKPower").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    /* Contact Us
    -----------------------------------------------------------------------------------------------------*/
    //Loads lightbox - NaturalGas
    jQuery("#ContactUs").click(function() {
        dialogName = "#" + jQuery(this).attr("name");
        ScrollToTop();
    });

    /* Close Lignbox 
    -----------------------------------------------------------------------------------------------------*/
    //Closes lightbox - Only if it is enabled
    jQuery(".Lightbox_Dialog_Close").click(function() {
        disableLightbox();

        //Checks if the user is on the flying contact us form. if yes then we reset the iframe source URL       
        ResetIframeContactUsURL();        

    });

    //Closes lightbox when clicking outside the lightbox
    jQuery("#Lightbox").click(function() {
        disableLightbox();

        //Checks if the user is on the flying contact us form. if yes then we reset the iframe source URL       
        ResetIframeContactUsURL(); 
    });

    //Closes lightbox when Pressing on Escape key
    jQuery(document).keypress(function(e) {
        if (e.keyCode == 27 && lightboxStatus == 1) {
            disableLightbox();

            //Checks if the user is on the flying contact us form. if yes then we reset the iframe source URL       
            ResetIframeContactUsURL(); 
        }
    });

    jQuery(window).resize(function() {
        centerLightbox();
    });

});
