﻿// debug crossbrowser logging - fails softly on ie
if (typeof console == "object") log = console.log;
else log = function() { };

// Replace fonts
Cufon.replace('.cufonLight', { fontFamily: 'Archer Light' }); // Archer Extra Light
Cufon.replace('.cufonBold', { fontFamily: 'Archer Bold' });
Cufon.replace('.cufonBoldHover', { fontFamily: 'Archer Bold', hover: true });
Cufon.replace('.cufonLightHover', { fontFamily: 'Archer Light', hover: true });



// When document is ready
$(document).ready(function() {

    // Front page hover control
    $('#FrontPageListControl .item').hover(function() {
        $(this).addClass("itemHover");
    }, function() {
        $(this).removeClass("itemHover");
    });

    $(".pagination").addClass("clearfix");

    // Left menu open/close logic
    $(".toggleLevel1").parent("li").hover(leftMenuIn, leftMenuOut);

    // Absolute top menu actions
    $(".buttonContainer").click(function() {
        if ($(this).hasClass("loggedIn")) {
            return;
        }
        var obj = $(this).siblings(".hiddenContainer");
        if (obj.css("display") == "none") {
            $(this).find(".arrow span").addClass("open");
        } else {
            $(this).find(".arrow span").removeClass("open");
        }
        obj.slideToggle();
    });
    if (trim($(".buttonContainer").siblings(".hiddenContainer").find(".failure").html()) != "") {
        $(".buttonContainer").siblings(".hiddenContainer").show();
    }



    // Show hide list
    $(".showHide .element .header").click(function() {
        if ($(this).siblings(".hiddenContainer").css("display") == "none") {
            $(this).find(".state").removeClass("plus").addClass("minus");
        } else {
            $(this).find(".state").removeClass("minus").addClass("plus");
        }
        $(this).siblings(".hiddenContainer").slideToggle("fast");
    });


    // Needs a bit of fixing
    /*
    if ($("#pagepeelContainer").length != 0) {
    $("#pagepeelContainer").css({ "width": "100px", "height": "100px", "overflow": "hidden" });
    $("#pagepeelContainer").hover(function() {
    $(this).css({ "width": "310px", "height": "310px", "overflow": "auto" });
    }, function() {
    $(this).css({ "width": "100px", "height": "100px", "overflow": "hidden" });
    });
    }
    */

    // Center the menu-item text
    $("ul.level1 a span").each(function() {
        var pH = $(this).parent().height();
        var tH = $(this).height();
        $(this).css("padding-top", ((pH / 2) - (tH / 2)) + "px");
    });

    // Start jFlow
    $("#focusAreaListControl").jFlow({
        parent: "#focusAreaListControl",
        slides: ".imgContainer",
        pageControl: ".pageControl",
        width: "391px",
        height: "160px",
        item: ".imgItem",
        autoplay: "true",
        interval: "3"
    });
    $("#sectionListControl").jFlow({
        parent: "#sectionListControl",
        slides: ".imgContainer",
        pageControl: ".pageControl",
        width: "391px",
        height: "160px",
        item: ".imgItem",
        autoplay: "true",
        interval: "3"
    });

    // Start jTabFlow
    $(".tabControl").jTabFlow({
        parent: ".tabControl",
        tabs: ".maintabs",
        subtabs: ".subtabs",
        views: ".tabViewContainer",
        slides: ".tabContainer",
        item: ".tabItem",
        pageControl: ".pageControl",
        width: "485px",
        height: "170px",
        autoplay: "true",
        interval: "3"
    });
    
    equalHeight($(".tabControl .maintabs h2 a"));


    $('ul.dropdown td').hover(selectImageIfAny, fixDDHeight);
    $('ul.dropdown .PlaceAndStudyListControl td:first').hover();
    $('ul.dropdown .StudyAndPlaceListControl td:first').hover();


    // Equalize heights on certain columns
    equalHeight($('.eQtext'));
    equalHeight($('.eQitem'));
    //equalHeight($('.eQcol'));

    $("a.tipAFriend").fancybox({ 'zoomSpeedIn': 500, 'zoomSpeedOut': 300, 'zoomOpacity': true,
        'overlayShow': false, 'enableEscapeButton': true, 'hideOnContentClick': false,
        'frameWidth': 400, 'frameHeight': 80,
        'easingIn': 'easeOutBack', 'easingOut': 'easeInBack'
    });


    /*
    if ($("#smallClocks").length > 0) {

        var currentBlock = 0;
        $("#smallClocks #pager .counter").html("1-" + $("#smallClocks .block:nth(" + currentBlock + ")").children().size());
        $("#smallClocks #pager .left").addClass("disabled");
        $("#smallClocks .block:not(:nth(" + currentBlock + "))").hide();

        $("#smallClocks #pager .left").click(function() {
            $("#smallClocks .block:nth(" + currentBlock + ")").hide();
            if (currentBlock > 0) {
                $("#smallClocks #pager .right").removeClass("disabled");
                //$("#smallClocks .outercontainer").scrollTo({ left: "-=517px", top: "0" }, 500);
                currentBlock--;
                $("#smallClocks #pager .counter").html((((currentBlock) * 8) + 1) + "-" + ((((currentBlock) * 8)) + $("#smallClocks .block:nth(" + (currentBlock) + ")").children().size()));
                $("#smallClocks .block:nth(" + currentBlock + ")").fadeIn("fast");
            }
            if (currentBlock == 0) {
                $("#smallClocks #pager .left").addClass("disabled");
            }
        });
        $("#smallClocks #pager .right").click(function() {
            $("#smallClocks .block:nth(" + currentBlock + ")").hide();
            if (currentBlock < $("#smallClocks .block").size()) {
                $("#smallClocks #pager .left").removeClass("disabled");
                //$("#smallClocks .outercontainer").scrollTo({ left: "+=517px", top: "0" }, 500);
                currentBlock++;
                $("#smallClocks #pager .counter").html((((currentBlock) * 8) + 1) + "-" + ((((currentBlock) * 8)) + $("#smallClocks .block:nth(" + (currentBlock) + ")").children().size()));
                $("#smallClocks .block:nth(" + currentBlock + ")").fadeIn("fast");
            }
            if (currentBlock == $("#smallClocks .block").size() - 1) {
                $("#smallClocks #pager .right").addClass("disabled");
            }
        });
    }
    */
    
    $(".forgotPassword").click(function() {
        $(".hideWhenResetPassord").hide();
        $(".showWhenResetPassord").show();
    });

    // Must be last, if equalHeight($('.eQcol')); is on
    //if ($("#Flickr").length != 0) {
    //    flickrFetcher();
    //}

    // debugging
    //$("#pagepeelContainer").css("border", "solid 1px #000");
});


function expandPagePeel() {
    //console.log("expanding");
    $("#pagepeelContainer").css({ "width": "310px", "height": "310px", "overflow" : "auto" });
    //$("#pagepeelFlash").css({ "width": "310px", "height": "310px" });
}
function collapsePagePeel() {
    //console.log("collapsing");
    $("#pagepeelContainer").css({ "width": "100px", "height": "100px", "overflow": "hidden" });
    //$("#pagepeelFlash").css({ "width": "100px", "height": "100px" });
}


function fixDDHeight() {
    var p = $(this).parent();
    var h = p.parent().find(".image").height();
    if (h > p.height()) {
        p.height(h);
    }
}

function selectImageIfAny() {
    var c = $(this).find(".rightSubContent");
    if (c.length != 0) {
        var r = $(this).parent().parent().parent().parent().parent().find(".rightInfo");
        r.html(c.html());
        r.find("h2").addClass("cufonBold");
        Cufon.replace('h2.cufonBold', { fontFamily: 'Archer Bold' });
    }
   fixDDHeight();
}

function leftMenuIn() {
    var obj = $(this).find(".toggleThisLevel2");
    if (obj.length == 0) {
        return true;
    }
    $(this).addClass("opened");
    obj.slideDown("fast");
    return false;
}
function leftMenuOut() {
    var obj = $(this).find(".toggleThisLevel2");
    if (obj.length == 0) {
        return true;
    }
    $(this).removeClass("opened");
    obj.stop().animate({ height: "hide" }, { queue: false, duration: "fast",  complete: function() { $(this).css("height", "auto"); } });
    return false;
}


function flickrFetcher(photoSetId) {
    var apiKey = 'e78ef7b25b2ec6cc6c8cda5fac02ce38';
    if (photoSetId == '') {
        return false;
    }
    $("#Flickr").show();
    //the initial json request to flickr
    //to get your latest public photos, use this request: http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=' + apiKey + '&user_id=29096781@N02&per_page=15&page=2&format=json&jsoncallback=?
    
    
    $.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=' + photoSetId + '&format=json&jsoncallback=?',
    function(data) {

        var current = 0;
        var limit = 1;
        // loop through the results with the following function
        $.each(data.photoset.photo, function(i, item) {

            if (current >= limit) {
                return;
            }
            // build the url of the photo in order to link to it
            var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_m.jpg'
            // append the image to the control
            $("#Flickr .items").append("<div class='flickr_item'><img src='" + photoURL + "' /></div>"); // <span>" + item.title + "</span>
            current++;
        });
    });
}

// Helper functions
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

function equalHeightwPadding(group) {
    tallest = 0;
    group.each(function() {
        thisPaddingT = $(this).css("padding-top");
        thisPaddingB = $(this).css("padding-bottom");
        thisPaddingT = parseInt(thisPaddingT.substring(0, thisPaddingT.length - 2));
        thisPaddingB = parseInt(thisPaddingB.substring(0, thisPaddingB.length - 2));
        thisPadding = thisPaddingT + thisPaddingB;

        thisMarginT = $(this).css("margin-top");
        thisMarginB = $(this).css("margin-bottom");
        thisMarginT = (thisMarginT.indexOf("px") != -1) ? parseInt(thisMarginT.substring(0, thisMarginT.length - 2)) : 0;
        thisMarginB = (thisMarginB.indexOf("px") != -1) ? parseInt(thisMarginB.substring(0, thisMarginB.length - 2)) : 0;
        thisMargin = thisMarginT + thisMarginB;

        thisHeight = $(this).height() + thisPadding + thisMargin;
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.each(function() {
        thisPaddingT = $(this).css("padding-top");
        thisPaddingB = $(this).css("padding-bottom");
        thisPaddingT = parseInt(thisPaddingT.substring(0, thisPaddingT.length - 2));
        thisPaddingB = parseInt(thisPaddingB.substring(0, thisPaddingB.length - 2));
        thisPadding = thisPaddingT + thisPaddingB;

        thisMarginT = $(this).css("margin-top");
        thisMarginB = $(this).css("margin-bottom");
        thisMarginT = (thisMarginT.indexOf("px") != -1) ? parseInt(thisMarginT.substring(0, thisMarginT.length - 2)) : 0;
        thisMarginB = (thisMarginB.indexOf("px") != -1) ? parseInt(thisMarginB.substring(0, thisMarginB.length - 2)) : 0;
        thisMargin = thisMarginT + thisMarginB;
        $(this).height(tallest - thisPadding - thisMargin);
    });
}

function showTextInControl(text, controlId) {
    ctrl = document.getElementById(controlId);
    if (ctrl) {
        ctrl.innerHTML = text;
    }
}

function populateLink(text, value, controlId) {
    ctrl = document.getElementById(controlId);
    if (ctrl) {
        ctrl.innerHTML = text;
        ctrl.href = value;
    }
}


// TRIM
// Removes leading whitespaces
function LTrim(value) {

    if(value == null) return "";
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim(value) {

    if(value == null) return "";
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim(value) {

    return LTrim(RTrim(value));

}
