// JavaScript Document
$(document).ready(function() {
    // Clearfix
    $('<style type="text/css">.clearfix:after {visibility: hidden;display: block;font-size: 0;content: " ";clear: both;height: 0;}* html .clearfix { zoom: 1; }*:first-child+html .clearfix { zoom: 1; }</style>').appendTo("head");
    //	$("span.tabs").each(function(){
    //		var tabsOffset = $(this).offset();
    //		var tabsOffsetLeft = tabsOffset.left + 82;
    //		$(this).children("span.tab-arrow").css('left',tabsOffsetLeft+'px');
    //	});
    // Tab
    $(".innerTabContent").hide();

    var qrySTR = window.location.href;
    var qryval = "";

    qryval = qrySTR.substring(qrySTR.lastIndexOf('#') + 1, qrySTR.length);


    if (qrySTR.lastIndexOf('#') > 0) {
        $("ul.innerTab li").eq(qryval - 1).addClass("active").show();
        $(".tabContentContainer .innerTabContent").eq(qryval - 1).show();
    }
    else {
        $("ul.innerTab li:first").addClass("active").show();
        $(".innerTabContent:first").show();
    }

    $("ul.innerTab li").click(function() {
        if ($(this).hasClass("active")) {
            return false;
        } else {
            $("ul.innerTab li").removeClass("active");
            $(this).addClass("active");
            $(".innerTabContent").slideUp();

            var activeTab = $(this).find("a").attr("href");
            $(activeTab).slideDown();
            return false;
        }
    });
    //$("ul.innerTab li:first-child").addClass("tabFirstLi");
    //$("ul.innerTab li:last-child").addClass("tabLastLi");
    $("ul.innerTab li:last-child").css('padding-right', '0');
    $(".spiralinaTabCont").hide();
    $("ul.spiralinaTab li:first").addClass("active").show();
    $(".spiralinaTabCont:first").show();

    $("ul.spiralinaTab li").click(function() {
        if ($(this).hasClass("active")) {
            return false;
        } else {
            $("ul.spiralinaTab li").removeClass("active");
            $(this).addClass("active");
            $(".spiralinaTabCont").slideUp();

            var activeTab = $(this).find("a").attr("href");
            $(activeTab).slideDown();
            return false;
        }
    });


    $(".caroteneTabCont").hide();
    $("ul.caroteneTab li:first").addClass("active").show();
    $(".caroteneTabCont:first").show();

    $("ul.caroteneTab li").click(function() {
        if ($(this).hasClass("active")) {
            return false;
        } else {
            $("ul.caroteneTab li").removeClass("active");
            $(this).addClass("active");
            $(".caroteneTabCont").slideUp();

            var activeTab = $(this).find("a").attr("href");
            $(activeTab).slideDown();
            return false;
        }
    });


    //jquery first child	
    $("div.formsOffWra:first").css('borderTop', '0');
    //$("div.formsOffWra:first").css('paddingTop','0');
    $(".formsOffBlk:first-child").css('borderLeft', '0');

    // Sub Menu
    $("span.caroteneSubLinksTrigger").hover(function() {
        var pareNt = $(this).parent()
        pareNt.find("ul.cSLlinks").stop(true, true).slideDown('fast');
        pareNt.find("span.caroteneSubLinksTrigger").addClass("hovered");
        pareNt.hover(function() {
        }, function() {
            pareNt.find("ul.cSLlinks").slideUp('fast');
            pareNt.find("span.caroteneSubLinksTrigger").removeClass("hovered");
        });
    });
    highlight();

    // Show Light Box
    $("a.showLightBox").click(function() {
        var arrPageSizes = getPageSize();
        var lightBoxData = $(this).attr("href");
        $("#lightbox").css({
            width: arrPageSizes[0],
            height: arrPageSizes[1],
            opacity: 0.75
        });
        var LbW = $(lightBoxData).outerWidth();
        var LbMl = LbW / 2;
        var LbH = $(lightBoxData).outerHeight();
        var LbHl = LbH / 2;
        $(lightBoxData).css({
            marginLeft: '-' + LbMl + 'px',
            marginTop: '-' + LbHl + 'px'
        });
        $("#lightbox").fadeIn(500);
        $(lightBoxData).fadeIn(500);
        $('#idhidden').val($(this).attr('rel'));
        return false;
    });

    // Lightbox Close
    $("a.lightboxClose,#lightbox").click(function() {
        closeLightBox();
    });

});

function closeLightBox() {
    $(".lightboxcontent,#lightbox").fadeOut(500);
}
// Hide Popups by ESC Key
$(document).keyup(function(e) {
    if (e.which == 27) {
        if ($("div#lightbox").is(":visible")) {
            closeLightBox();
        }
    }

});

function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
};


function highlight() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    var strTest = sPage;
    if (strTest == 'organic-spirulina.aspx')
        $('.organic-spirulina').addClass('currentPage');
    else if (strTest == 'carotenoids.aspx' || strTest == 'beta-carotene.aspx' || strTest == 'tomato-lycopene-complex.aspx' || strTest == 'astaxanthin.aspx' || strTest == 'lutein-and-zeaxanthin.aspx')
        $('.carotenoids').addClass('currentPage');
    else if (strTest == 'food-ingredients.aspx')
        $('.food-ingredients').addClass('currentPage');
    else if (strTest == 'formulations.aspx')
        $('.formulations').addClass('currentPage');
}


function techpagination(pgnum) {
    goodOverlay();
    var url = "/ajax/ajax-techpaging.aspx?pgnum=" + pgnum;
    xmlHTTP.open('GET', url, false);
    xmlHTTP.send(null);
    if (xmlHTTP.responseText) {
        var response = xmlHTTP.responseText.split("_PH_SPLIT");
        document.getElementById('divpaging').value = '';
        document.getElementById('divpaging').innerHTML = '<div class="sertoppagination clearfix"><h1 class="red fL">Technical Papers</h1><p class="fR clearfix sertoppagination">' + response[1] + '</p></div>';
        document.getElementById('divbtmpaging').value = '';
        document.getElementById('divbtmpaging').innerHTML = '<p class="serpagination padTop10" >' + response[1] + '</p>';
        document.getElementById('divtech').value = '';
        document.getElementById('divtech').innerHTML = response[0];
        document.getElementById("overlay").style.display = "none";


        $("a.showLightBox").click(function() {
            var arrPageSizes = getPageSize();
            var lightBoxData = $(this).attr("href");
            $("#lightbox").css({
                width: arrPageSizes[0],
                height: arrPageSizes[1],
                opacity: 0.75
            });
            var LbW = $(lightBoxData).outerWidth();
            var LbMl = LbW / 2;
            var LbH = $(lightBoxData).outerHeight();
            var LbHl = LbH / 2;
            $(lightBoxData).css({
                marginLeft: '-' + LbMl + 'px',
                marginTop: '-' + LbHl + 'px'
            });
            $("#lightbox").fadeIn(500);
            $(lightBoxData).fadeIn(500);
            $('#idhidden').val($(this).attr('rel'));
            return false;
        });

        // Lightbox Close
        $("a.lightboxClose,#lightbox").click(function() {
            closeLightBox();
        });
    }
    Cufon.replace('h1', { hover: true }, { fontFamily: "BebasNeue" });

}


function CaptureVisitingUsers() {

    if (document.getElementById("txtName").value == "") {
        alert("Please enter name");
        document.getElementById("txtName").focus();
        return false;
    }
    if (document.getElementById("txtEmail").value == "") {
        alert("Please enter email");
        document.getElementById("txtEmail").focus();
        return false;
    }
    if (document.getElementById("txtEmail").value != "") {
        if (isEmail(document.getElementById("txtEmail").value) == false) {
            alert("Please enter your valid email address");
            document.getElementById("txtEmail").focus();
            return false;
        }
    }
    var url = "/ajax/ajax-tech-downloads.aspx?name=" + document.getElementById("txtName").value + "&email=" + document.getElementById("txtEmail").value + "&techid=" + $('#idhidden').val();
    xmlHTTP.open('GET', url, false);
    xmlHTTP.send(null);
    if (xmlHTTP.responseText) {
        var response = xmlHTTP.responseText;
        if (response != "failure") {
            window.open("Download-Tech-Paper.aspx?docs=" + response);
        }
        else 
        {
                $("a.showLightBox").click(function() {
                    var arrPageSizes = getPageSize();
                    var lightBoxData = $(this).attr("href");
                    $("#lightbox").css({
                        width: arrPageSizes[0],
                        height: arrPageSizes[1],
                        opacity: 0.75
                    });
                    var LbW = $(lightBoxData).outerWidth();
                    var LbMl = LbW / 2;
                    var LbH = $(lightBoxData).outerHeight();
                    var LbHl = LbH / 2;
                    $(lightBoxData).css({
                        marginLeft: '-' + LbMl + 'px',
                        marginTop: '-' + LbHl + 'px'
                    });
                    $("#lightbox").fadeIn(500);
                    $(lightBoxData).fadeIn(500);
                    $('#idhidden').val($(this).attr('rel'));
                    return false;
                });

                // Lightbox Close
                $("a.lightboxClose,#lightbox").click(function() {
                    closeLightBox();
                });
            Cufon.replace('h1', { hover: true }, { fontFamily: "BebasNeue" });
        }

    }
}
function ClearVisitingUsers() {
    document.getElementById("txtName").value = "";
    document.getElementById("txtEmail").value = "";
    return false;
}
function DownloadPapers(id) {
    var url = "/ajax/ajax-tech-downloads.aspx?techid=" + id;
    xmlHTTP.open('GET', url, false);
    xmlHTTP.send(null);
    if (xmlHTTP.responseText) {
        var response = xmlHTTP.responseText;
        if (response != "failure")
            window.location = "Download-Tech-Paper.aspx?docs=" + response;
       else 
        {
                $("a.showLightBox").click(function() {
                    var arrPageSizes = getPageSize();
                    var lightBoxData = $(this).attr("href");
                    $("#lightbox").css({
                        width: arrPageSizes[0],
                        height: arrPageSizes[1],
                        opacity: 0.75
                    });
                    var LbW = $(lightBoxData).outerWidth();
                    var LbMl = LbW / 2;
                    var LbH = $(lightBoxData).outerHeight();
                    var LbHl = LbH / 2;
                    $(lightBoxData).css({
                        marginLeft: '-' + LbMl + 'px',
                        marginTop: '-' + LbHl + 'px'
                    });
                    $("#lightbox").fadeIn(500);
                    $(lightBoxData).fadeIn(500);
                    $('#idhidden').val($(this).attr('rel'));
                    return false;
                });

                // Lightbox Close
                $("a.lightboxClose,#lightbox").click(function() {
                    closeLightBox();
                });
            Cufon.replace('h1', { hover: true }, { fontFamily: "BebasNeue" });
        }
    }
}
function isEmail(objValue) {
    var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if (!emailRegex.test(objValue))
        return false;
    return true;
}

//good read overlay
function goodOverlay() {
    var PopPos = $("#page").offset();
    var PopPosLeft = PopPos.left;
    var PopPosTop = PopPos.top;
    var PopPosHeight = $("#page").height();
    var PopPosWidth = $("#page").width();
    $("#overlay").css({
        left: PopPosLeft,
        top: PopPosTop,
        height: PopPosHeight + 20,
        width: PopPosWidth + 10,
        opacity: '0.8'
    }).show();
    return false;
}


var xmlHTTP = getreq();
function getreq() { // returns false if exists
    if (window.ActiveXObject) { // if IE
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                return;
            }
        }
    }
    else if (window.XMLHttpRequest) { // if Mozilla, Safari, etc.
        return new XMLHttpRequest();
    }
}


