﻿var xmlhttp;
var xmlhttp_country;
var xmlhttp_courselength;
var divid;
var homeproduct;
var hometarget;
var targetpage;
var selectedLink;
var funnelversion;
firstPageLoaded = false;

function loadContent(group, product, div, pagename, reloaded, onclickVar) {
    createHTTPRequest(group, product, pagename, reloaded, onclickVar);
    divid = div;
    homeproduct = product;
    hometarget = group;
    targetpage = pagename;
    selectedLink = onclickVar;   
}

function createHTTPRequest(group, product, pagename, reloaded, onclickVar) {
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp == null) {
        alert("Your browser does not support XMLHTTP!");
        return;
    }
    var verVal = document.location.search.substring(1);
    var verPairs = verVal.split("&");
    for (var i = 0; i < verPairs.length; i++) {
        var pos = verPairs[i].indexOf('=');
        if (pos == -1) continue;

        var argname = verPairs[i].substring(0, pos);
        var value = verPairs[i].substring(pos + 1);

        if (argname == 'version') {
            funnelversion = value;
        }
    }
    if (funnelversion != 'defaultalt') {
        funnelversion = '';
    }
    var url = "/AjaxCall/funnelContent.aspx?pagename=" + pagename + "&version=" + funnelversion;
    
    if (group != "" && product == "")
        url = url + "&target=" + group;
    else if (product != "" && group == "")
        url = url + "&product=" + product;
    else if (group != "" && product != "")
        url = url + "&target=" + group + "&product=" + product;
    url = url + "&reloaded=" + reloaded;
    if (group == '' && product == '') {
        $('#content').addClass('home');
        xmlhttp.onreadystatechange = stateChanged;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    } else {
    if (product == '' || reloaded == 'true') {
            $('.home-panel').fadeOut(400);
            $('.quick-search').fadeOut(400);
            $('.module-panel').fadeOut(400);
            $('#ctl00_Content_bottomlinks').fadeOut(400);
            $('.options').fadeOut(400);
            $('#footer').fadeOut(400, function() {
                $('#content').removeClass('home');
                $('#inner-content').addClass('clearfix programs');
                $('#heading').html('<h1>&nbsp;</h1>');
                $('.options').removeClass('no-heading');
                $('#qsearch').hide();
                $('#ctl00_fatfooter_footerlocaloffice').hide();
                xmlhttp.onreadystatechange = stateChanged;
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);
                // loading image
                progressBar = '<div id="loading"><img src="http://media.ef.com/_imgs/FunnelPages2010/ajax-loader.gif" /></div>';
                $('#inner-content').empty().append(progressBar).hide().fadeIn('slow').ajaxStart(function() {
                    $(progressBar).hide();
                }).ajaxStop(function() {
                    $(progressBar).hide();
                });
            });
        } else {
            $('#right-col').fadeOut(400, function() {
                xmlhttp.onreadystatechange = stateChanged;
                xmlhttp.open("GET", url, true);
                xmlhttp.send(null);
                // loading image
                progressBar = '<div id="loading"><img src="http://media.ef.com/_imgs/FunnelPages2010/ajax-loader.gif" /></div>';
                $('#right-col').empty().append(progressBar).hide().fadeIn('slow').ajaxStart(function() {
                    $(progressBar).hide();
                }).ajaxStop(function() {
                    $(progressBar).hide();
                });
            });
            if (divid != 'right-col') {
                if (targetpage != 'smallmarkets') {
                    $('#footer').fadeOut(400);
                    $('.module-panel').fadeOut(400);
                }
            }
        }
    }
}


function stateChanged() {
    if (xmlhttp.readyState == 4) {        
        if (divid.length > 0) {
            if (targetpage == "smallmarkets") {
                $('#inner-content').queue(function() {
                    targetCont = '#';
                    targetCont += divid;
                    $(targetCont).removeClass('small-market-intro');
                    document.getElementById(divid).innerHTML = xmlhttp.responseText;
                    $('#footer').fadeIn();
                    $('#ctl00_fatfooter_footerlocaloffice').hide();
                    $('#ctl00_Content_bottomlinks').fadeIn(400);
                    $(".quick-search").css("display", "none");
                    initBinding();
                    $('#inner-content').addClass('small-market-clicked');
                    $(this).dequeue();
                    $("#left-col ul li").each(function() {
                        $(this).removeClass('selected');
                    });
                    $("#left-col ul li a").each(function() {
                        targetLink = $(this).attr('name').replace(/^\s*|\s*$/g, '').replace(/ /gi, "");
                        if (targetLink == selectedLink) {
                            $(this).parent().addClass('selected');
                        }
                    });
                });
            } else {
            $('#inner-content').queue(function() {

                // add new data - empty container and append data
                targetCont = '#';
                targetCont += divid;
                document.getElementById(divid).innerHTML = xmlhttp.responseText;
                $('#inner-content').css({ 'height': 'auto' });
                productlength = $('#left-col li').length;
                if (productlength == 1 && firstPageLoaded == false) {
                    $(targetCont).hide().fadeIn();
                    $('#right-col').hide();
                    var hash = $('#left-col a').attr('name').replace('loadContent(', '').replace(')', '');
                    hash = hash.replace(/^.*#/, '');
                    firstComma = hash.indexOf(',');
                    secondCommaPos = firstComma + 1;
                    hash2 = hash.substr(secondCommaPos);
                    secondComma = hash2.indexOf(',');
                    thirdCommaPos = secondComma + 1;
                    hash3 = hash2.substr(thirdCommaPos);
                    thirdComma = hash3.indexOf(',');
                    fourthCommaPos = thirdComma + 1;
                    hash4 = hash3.substr(fourthCommaPos);
                    fourthComma = hash4.indexOf(',');
                    var1 = 'agegroup=' + hash.substring(0, firstComma).replace(/'/gi, "").replace(' ', '');
                    var2 = '/product=' + hash2.substring(0, secondComma).replace(/'/gi, "").replace(' ', '');
                    var3 = '/target=' + hash3.substring(0, thirdComma).replace(/'/gi, "").replace(' ', '');
                    var4 = '/site=' + hash4.substring(0).replace(/'/gi, "").replace(' ', '');
                    hash = var1 + var2 + var3 + var4;
                    firstPageLoaded = true;
                    $.history.load(hash);
                } else {
                    $(targetCont).hide().fadeIn(400);
                }
                if (quickSearchVar == false) {
                    $('.quick-search').fadeIn(400);
                    quickSearchVar = true;
                }

                // fade in footer
                document.getElementById("ctl00_efheader_headerlinks").setAttribute("style", "display:block");
                $('#footer').fadeIn();
                $(this).jqTransform();
                $(this).removeClass('jqtransformdone');
                initBinding();
                $(this).dequeue();
                $("#left-col ul li").each(function() {
                    $(this).removeClass('selected');
                });
                $("#left-col ul li a").each(function() {
                    targetLink = $(this).attr('name').replace(/^\s*|\s*$/g, '').replace(/ /gi, "");
                    //targetLink = targetLink
                    if (targetLink == selectedLink) {
                        $(this).parent().addClass('selected');
                    }
                });
            });
            }
        }

        if (homeproduct.length > 0 || hometarget.length > 0) {
            document.getElementById("ctl00_Content_bottomlinks").setAttribute("style", "display:none;");
            document.getElementById("ctl00_fatfooter_footerlocaloffice").setAttribute("style", "display:none;");
            document.getElementById("ctl00_efheader_headerlinks").setAttribute("style", "display:block");
            document.getElementById("ctl00_Content_qsearch").setAttribute("style", "display:none;");
            document.getElementById("inner-content");

        }
        if (targetpage == 'smallmarkets') {
            $('.module-panel').show();
        }
        //divid = '' is commented out on purpose in order to make the fade in fade out effect can work
        //divid = '';
        homeproduct = '';
        hometarget = '';

    }
}

function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}


function AjaxDriveDropDown(target, country, courselength) {
    //alert(url + id + "," + value);   
    var url = "/AjaxCall/SearchBoxAjax.aspx";
    var queryString = "?target=" + target + "&country=" + country + "&courselength=" + courselength;
    url += queryString;
    if (country == "All") {
        xmlhttp_country = GetXmlHttpObject();
        if (xmlhttp_country == null) {
            alert("Your browser does not support XMLHTTP!");
            return;
        }
        xmlhttp_country.onreadystatechange = CountrystateChanged;
        xmlhttp_country.open("GET", url, true);
        xmlhttp_country.send(null);
    }
    if (courselength == "All") {
        xmlhttp_courselength = GetXmlHttpObject();
        if (xmlhttp_courselength == null) {
            alert("Your browser does not support XMLHTTP!");
            return;
        }
        xmlhttp_courselength.onreadystatechange = CourseLengthstateChanged;
        xmlhttp_courselength.open("GET", url, true);
        xmlhttp_courselength.send(null);
    }
}

function CourseLengthstateChanged() {
    if (xmlhttp_courselength.readyState == 4) {
        //document.getElementById("destCountryDiv").innerHTML=xmlhttp.responseText; 
        $('#courseLengthDiv').queue(function() {
            $(this).html(xmlhttp_courselength.responseText);
            $(this).jqTransform();
            $(this).removeClass('jqtransformdone');
            $(this).dequeue();
        });
    }
}

function CountrystateChanged() {
    if (xmlhttp_country.readyState == 4) {
        //document.getElementById("destCountryDiv").innerHTML=xmlhttp.responseText; 
        $('#destCountryDiv').queue(function() {
            $(this).html(xmlhttp_country.responseText);
            $(this).jqTransform();
            $(this).removeClass('jqtransformdone');
            $(this).dequeue();
        });
    }
}

//Common Utility Function to hide all the div from the home page
function SetPostContentLinkClass() {
    var counter = $('.module').length;
    if (counter == 2) {
        document.getElementById("ctl00_Content_bottomlinks").className = "module-panel clearfix two-modules";
    } else if (counter == 3) {
        document.getElementById("ctl00_Content_bottomlinks").className = "module-panel clearfix three-modules";
    }
    else {
        document.getElementById("ctl00_Content_bottomlinks").className = "module-panel clearfix";
    }
    if (targetpage == "smallmarkets") {
        $("#ctl00_Content_bottomlinks").addClass("grey-highlight");
    }
}

//Top Header Slider Function
function SlideTopHeader() {
    var imgArrow = jQuery('div#wrapHeader div.action li.language a#slideme');
    var languageDiv = jQuery('div#wrapHeader div.action li.language div');
    var languageUl = jQuery('div#wrapHeader div.action li.language ul');
    var languageUlheight = languageUl.height();
    languageUl.css({ "width": jQuery('div#wrapHeader div.action li.language').width() - 20, "height": "0px", "visibility": "visible" });
    jQuery('div#wrapHeader div.action li a#slideme').toggle(
        function() {
            imgArrow.css("background-image", "url(http://media.ef.com/_imgs/ly/2010/header/arrowUp.gif)");
            languageDiv.animate({ height: languageUlheight + 35 }, 200);
            languageUl.animate({ height: languageUlheight }, 200);
        },
        function() {
            imgArrow.css("background-image", "url(http://media.ef.com/_imgs/ly/2010/header/arrowDown.gif)");
            languageUl.animate({ height: "0px" }, 450);
            languageDiv.animate({ height: "24px" }, 450);
        }
    );
}

function PopUpMultilingualCountry() {
    // run code
    $(document).ready(function() {
        // select the overlay element - and "make it an overlay"
        $(".overlayWrapper").overlay({
            // custom top position
            top: 272,
            // some expose tweaks suitable for facebox-looking dialogs
            expose: '#445566',
            closeOnClick: true,
            // we want to use the programming API
            api: true,
            onBeforeLoad: function() {
                // grab wrapper element inside content
                var wrap = this.getContent().find(".contentWrap");
                urlPath = '/Forms/MultiLanguage.aspx';
                $('.simple_overlay').addClass('multilanguage-country-form-popup');
                $('.simple_overlay').removeClass('text-pop-up');
                // ajax call
                $.ajax({
                    url: urlPath,
                    success: function(data) {
                        // Queue functions
                        $(wrap).queue(function() {
                            // add new data - empty container and append data
                            $(this).html(data);
                            $(this).empty().append(data);
                            // fade in conent and add a class
                            $(this).hide().fadeIn();
                            // rebind jquery
                            $('form').removeClass('jqtransformdone');
                            initBinding();
                            $(this).dequeue();
                        });
                    }
                });
            }
            // load it immediately after the construction
        }).load();
    });
}

//$('.target').change(function() {
//    var str = "";
//    $("select option:selected").each(function() {
//        str += $(this).text() + " ";
//    });
//    $("div").text(str);
//})
//

