$(document).ready(function () {

    //--------------------------------------------------
    // New window

    $('.external').attr('target', '_blank').attr('title', 'Opens in a new window');

    //--------------------------------------------------
    // IE Helpers

    $('input').hover(function () {
        $(this).toggleClass('hover');
    });

    $('input').focus(function () {
        $(this).toggleClass('focus');
    });



    //--------------------------------------------------
    // Lanaugage Selector

    if ($('#language').length > 0) {

        $('#language ul').hide();
        $('#language').click(function () {
            $('#language ul').slideToggle();
            $(this).toggleClass('active');
        });

    }


    //--------------------------------------------------
    // Navigation filters

    if ($('#navigation').length > 0) {
        $('#navigation').addClass('js')
        $('#navigation ul').hide();
        $('#navigation h2 strong ').prepend('Select your ');
        $('#techniques').prepend('<h3><span><strong>Select your technique</strong></span></h3>');
        $('#subNavigation').addClass('disabled');

        $('#navigation h2, #navigation h3, #navigation span').hover(function () {
            $(this).toggleClass('hover');
        })

        $('#navigation h2').click(function () {
            $('#community').slideToggle();
        })

        $('#navigation li span').click(function () {
            //				var techniques = $(this).next();
            //				$(techniques).clone().appendTo('#techniques');
            //				$('#techniques h3').addClass('active');
            $('#community').slideToggle();
            //				$('#subNavigation').removeClass('disabled');
        });

        $('#techniques h3').click(function () {
            $('#techniques ul').slideToggle();
            $('#navigation h2').click(function () {
                $('#techniques ul').slideUp(function () {
                    $(this).remove();
                });
                $('#techniques h3').removeClass('active');
            })
        });

        $('#subNavigation li:first').addClass('first');
    }

    //--------------------------------------------------
    // Search field

    if ($('#fldSearch').length > 0) {
        $('#fldSearch').val("Search");
        $('#fldSearch').focus(function () {
            if ($('#fldSearch').val() === "Search") {
                $('#fldSearch').val("");
            }
        });
        $('#fldSearch').blur(function () {
            if ($('#fldSearch').val().length === 0) {
                $('#fldSearch').val("Search");
            }
        });
    }

    //--------------------------------------------------
    // Product Search field

    if ($('#keyText').length > 0) {
        $('#keyText').val("Search");
        $('#keyText').focus(function () {
            if ($('#keyText').val() === "Keyword") {
                $('#keyText').val("");
            }
        });
        $('#keyText').blur(function () {
            if ($('#keyText').val().length === 0) {
                $('#keyText').val("Keyword");
            }
        });
    }

    //--------------------------------------------------
    // Register

    if ($('.register').length > 0) {
        $('.register a').attr('href', $('.register a').attr('href') + '');
        $('.register a').fancybox({
            'overlayColor': '#000',
            'overlayOpacity': '0.8',
            'type': 'iframe',
            'padding': '0',
            'width': 640,
            'height': 650
        });
    }
   if ($('.register').length > 0) {
        //$('.register').attr('href', $('.register').attr('href') + '');
        $('.register').fancybox({
            'overlayColor': '#000',
            'overlayOpacity': '0.8',
            'type': 'iframe',
	    'url':'/register.aspx',
            'padding': '0',
            'width': 640,
            'height': 650
        });
    }

    //--------------------------------------------------
    // Request

    if ($('.request').length > 0) {
        $('.request a').attr('href', $('.request a').attr('href') + '');
        $('.request a').fancybox({
            'overlayColor': '#000',
            'overlayOpacity': '0.8',
            'type': 'iframe',
            'padding': '0',
            'width': 640,
            'height': 540
        });
    }
   

    //--------------------------------------------------
    // Tabbed content

    if ($('.tabbed').length > 0) {

        $(".tabbed").each(function (i) {
            var tabbed = $(this).attr('id');
            var display_news = [];
            var menu = "";
            var current = " selected";


            $("#" + tabbed + ' h3').each(function (i) {
                menu = menu + "<li class=\"item_" + i + "\"><a href=\"#\" id=\"tab_" + i + "\" class=\"tabControl" + current + "\">" + $(this).text() + "</a></li>";
                current = "";
            }).hide();
            $("#" + tabbed).prepend("<ul class=\"tabNavigation clear\">" + menu + "</ul>").find('.tab:not(:first)').hide().parent().find('.tab:first').show();

            //add event listeners + functionality
            $("#" + tabbed + ' .tabControl').click(function () {
                $("#" + tabbed + ' .tabControl').removeClass('selected')
                display_news = $(this).addClass('selected').parent().attr('class').split("_");
                $("#" + tabbed + ' .tab').hide();
                $("#" + tabbed + ' .tab:nth(' + display_news[1] + ')').show();
                return false;
            });
        })

    }


});

/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

//pre-load initial product finer box


//product finder AJAX

var xmlHttp
var url
function getProdFinder(classID, techID, catID, keyword) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    url = "/getProdFinder.aspx?";
    if (classID != null) {
        url = url + "&classID=" + classID;
    }
   
    if (techID != null) {
        url = url + "&techID=" + techID;
    }
    if (catID != null) {
        url = url + "&catID=" + catID;
    }

    if (keyword != "0") {
        url = url + "&keyword=" + keyword;
    }
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}


function stateChanged() {
    if (xmlHttp.readyState == 4) {
        document.getElementById("searchProdFinder").innerHTML = xmlHttp.responseText;
    }
    else {
        document.getElementById("searchProdFinder").innerHTML =
        "<h2>Search Products</h2><div id=\"locate\"></div>"
        + "<form action=\"./\" method=\"post\" class=\"panelContent\">"
        + "<fieldset><div class=\"row clear\"><select name=\"classID\" id=\"classID\" onchange=\"getProdFinderDDL(document.getElementById(&#39;classID&#39;).value,&#39;0&#39;);getProdFinderRes(document.getElementById(&#39;classID&#39;).value,&#39;0&#39;,&#39;0&#39;,document.getElementById(&#39;keyText&#39;).value);getProdFinderLink(document.getElementById(&#39;classID&#39;).value,&#39;0&#39;,&#39;0&#39;,document.getElementById(&#39;keyText&#39;).value)\" style=\"width:190px;\">"
        + "<option selected=\"selected\" value=\"0\">All fields</option>"
	    + "<option value=\"3\" title=\"Life Sciences\">Life Sciences</option>"
	    + "<option value=\"8\" title=\"Clinical Diagnostics\">Clinical Diagnostics</option>"
	    + "<option value=\"1\" title=\"Separations\">Separations</option>"
	    + "<option value=\"20\" title=\"Environmental\">Environmental</option>"
	    + "<option value=\"7\" title=\"Drug Discovery\">Drug Discovery</option>"
	    + "<option value=\"19\" title=\"Forensics\">Forensics</option>"
	    + "<option value=\"2\" title=\"Spectroscopy\">Spectroscopy</option>"
	    + "<option value=\"21\" title=\"Food and Beverage\">Food and Beverage</option>"
	    + "<option value=\"5\" title=\"Lab Automation\">Lab Automation</option>"
	    + "<option value=\"4\" title=\"General Lab\">General Lab</option>"
	    + "<option value=\"18\" title=\"Materials\">Materials</option>"
	    + "<option value=\"6\" title=\"Lab Informatics\">Lab Informatics</option>"
        + "</select></div><div class=\"row clear\" id=\"techDD\"><select name=\"techID\" id=\"techID\" onchange=\"getProdFinderDDL(document.getElementById(&#39;classID&#39;).value,document.getElementById(&#39;techID&#39;).value);getProdFinderRes(document.getElementById(&#39;classID&#39;).value,document.getElementById(&#39;techID&#39;).value,&#39;0&#39;,document.getElementById(&#39;keyText&#39;).value);getProdFinderLink(document.getElementById(&#39;classID&#39;).value,document.getElementById(&#39;techID&#39;).value,&#39;0&#39;,document.getElementById(&#39;keyText&#39;).value)\" style=\"width:190px;\">"
        + "	<option selected=\"selected\" value=\"0\">All techniques</option>"
        + "</select></div><div class=\"row clear\" id=\"catDD\"><select onchange=\"getProdFinderRes(document.getElementById(&#39;classID&#39;).value,document.getElementById(&#39;techID&#39;).value,document.getElementById(&#39;catID&#39;).value,document.getElementById(&#39;keyText&#39;).value);getProdFinderLink(document.getElementById(&#39;classID&#39;).value,document.getElementById(&#39;techID&#39;).value,document.getElementById(&#39;catID&#39;).value,document.getElementById(&#39;keyText&#39;).value)\" style=\"width:190px;\">"
        + "	<option selected=\"selected\" value=\"0\">All product categories</option>"
        + "</select></div>"
        + "<div class=\"row clear\"><font style=\"color:#95c063;\">Containing</font> <input type=text style=\"margin-left:5px;width:130px; height:20px; border: 1px solid #95c063;\" id=\"keyText\" onkeyup=\"getProdFinderLink(document.getElementById('classID').value,document.getElementById('techID').value,document.getElementById('catID').value,document.getElementById('keyText').value)\" name=keyText></div>"
        + "</fieldset><fieldset class=\"clear\"><p style=\"valign: center;float:left;\" class=\"button\"><a id=\"link\" href=\"/all-fields/product-directory/?key=&fromSearch=1\"/>Show results</a></p>   <p class=\"result\" id=\"result\"> </p></fieldset></form>";
        
    }
}
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function getProdFinderLink(classID, techID, catID, keyword) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    url = "/getProdFinderUtil.aspx?type=link";
    if (techID != null) {
        url = url + "&techID=" + techID;
    }
    if (classID != null) {
        url = url + "&classID=" + classID;
    }
    if (catID != null) {
        url = url + "&catID=" + catID;
    }

    if (keyword != "0") {
        url = url + "&keyword=" + keyword;
    }
    xmlHttp.onreadystatechange = stateChangedUtilLink;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function stateChangedUtilLink() {
    if (xmlHttp.readyState == 4) {
        document.getElementById("link").href = xmlHttp.responseText;
    }
}



var xmlHttpResult
function getProdFinderRes(classID, techID, catID, keyword) {
    xmlHttpResult = GetXmlHttpObjectResult();
    if (xmlHttpResult == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    url = "/getProdFinderUtil.aspx?type=result";
    if (techID != null) {
        url = url + "&techID=" + techID;
    }
    if (classID != null) {
        url = url + "&classID=" + classID;
    }
    if (catID != null) {
        url = url + "&catID=" + catID;
    }

    if (keyword != "0") {
        url = url + "&keyword=" + keyword;
    }
    xmlHttpResult.onreadystatechange = stateChangedUtilResult;
    xmlHttpResult.open("GET", url, true);
    xmlHttpResult.send(null);
}
function GetXmlHttpObjectResult() {
    var xmlHttpResult = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttpResult = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttpResult = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttpResult = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttpResult;
}


function stateChangedUtilResult() {
    if (xmlHttpResult.readyState == 4) {
        document.getElementById("result").innerHTML = xmlHttpResult.responseText;
    }
    else {
        document.getElementById("result").innerHTML = "<img src=/a/img/global/ajax-loader.gif />"
    }
}

 

//----------------------------product finder dropdown----------------------------

var xmlHttpDD

function getProdFinderDDL(classID, techID) {
    xmlHttpDD = GetXmlHttpObjectDD();
    if (xmlHttpDD == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    url = "/getProdFinderUtil.aspx?type=";

    if (techID != null && techID != "0") {
        url = url + "catDD&techID=" + techID;
        xmlHttpDD.onreadystatechange = stateChangedUtilCatDD;
    }
    else 
    if (classID != null) {
            url = url + "techDD&classID=" + classID;
            xmlHttpDD.onreadystatechange = stateChangedUtilTechDD;
    }
    
    xmlHttpDD.open("GET", url, true);
    xmlHttpDD.send(null);
}
function stateChangedUtilCatDD() {
    if (xmlHttpDD.readyState == 4) {
        document.getElementById("catDD").innerHTML = xmlHttpDD.responseText;
    }
    else {
        document.getElementById("catDD").innerHTML = "<img src=/a/img/global/ajax-loader.gif />"
    }
}

function stateChangedUtilTechDD() {

    if (xmlHttpDD.readyState == 4) {
        document.getElementById("catDD").innerHTML = "<select style=\"width:190;\" id=\"catID\" name=\"catID\"><option selected=\"selected\" value=\"0\">All product categories</option></select>";
        document.getElementById("techDD").innerHTML = xmlHttpDD.responseText;       
    }
    else {
        document.getElementById("techDD").innerHTML = "<img src=/a/img/global/ajax-loader.gif />"
        document.getElementById("catDD").innerHTML = "<img src=/a/img/global/ajax-loader.gif />"
    }
}

function GetXmlHttpObjectDD() {
    var xmlHttpDD = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttpDD = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttpDD = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttpDD = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttpDD;
}

