var $j = jQuery.noConflict();

$j(document).ready(function () {
    var links = $j("a");
    $j.each(links, function (i, val) {
        url = val.toString().toLowerCase();
        if (url.indexOf("/contact/") > -1) {
            this.href = "#";
            $j(this).fancybox({
                'padding': 0,
                'centerOnScroll': true,
                'autoScale': true,
                'scrolling': 'none',
                titleShow: false,
                'href': url
            });
        }
        if (url.indexOf(".ashx") > -1 || url.indexOf("documentviewer") > -1) {
            //Adding tracking for media library and publications
            $j(this).attr("onclick", "javascript:pageTracker._trackPageview('" + url + "')");
        }
    });
});

var flashVersion = "10.0.0";
//TestFlash();

function TestFlash()
{
    arrVersion = flashVersion.split(".");
    
    if (arrVersion[0] > swfobject.getFlashPlayerVersion().major)
    {
        location.replace("/noflash.htm");
    }
}

function GetHostName()
{
    var hostname = window.location.hostname;
    if (window.location.port != "")
    hostname += ":" + window.location.port;
    return hostname;
}

function LoadHtml(url) {
    $j.ajax({
        type: "GET",    
        url: url,
        success: function (response) {
            $j('#div_Content').html(response);
        }
    });
}

function LoadPushFlash(url, tabfield, link, target, header)
{
    var flashvars = {}
    flashvars.link = link;
    flashvars.target = target;
    flashvars.header = header;
    var params = {};
    params.menu = "false";
    params.wmode = "transparent";
    var attributes = {};
    swfobject.embedSWF(url, tabfield, "326", "216", flashVersion,"/~/media/Files/Flash/swfobject/expressInstall.ashx", flashvars, params, attributes);
}

function changeImage(element, source) {
    document.getElementById(element).src = source;
}

function ClearTextBox() {
    document.getElementById("TextBoxSearch").value = "";
}

function SubmitSearch(e, id, language,view) {
    if (IsEnter(e)) {
        Search(id, language, view);
        return false;
    }
    return true;
}

function SubmitSearchPublication(e, language) {
    if (IsEnter(e)) {
        SearchPublication(language);
        return false;
    }
    return true;
}


function IsEnter(e)
{
    var keynum

    if(window.event) // IE
    {
        keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }

    if(keynum==13)
    {
        return true;
    }
    return false;
}

function Search(id, language, view) {

    query = document.getElementById(id);
    url = "/" + language + "/Search?page=1";

    if (query != null && query.value != "")
    {
        url += "&query=" + Url.encode(query.value);
    }
    if (view != null && view != "") {
        url += "&view=" + view;
    }

    window.location.href= url;
}

function SearchPublication(language) {
    query = document.getElementById('query');
    queryProduct = document.getElementById('ChooseProduct');
    queryType = document.getElementById('ChooseType');
    queryLanguage = document.getElementById('ChooseLanguage');
    url = "/" + language + "/Publications?page=1";

    if (query != null && query.value != "") {
        url += "&query=" + Url.encode(query.value);
    }

    url += "&queryProduct=" + queryProduct.value;
    url += "&queryType=" + queryType.value;
    url += "&queryLanguage=" + queryLanguage.value;

    window.location.href = url;
}

var Url = {   
  
    // public method for url encoding   
    encode : function (string) {   
        return escape(this._utf8_encode(string));   
    },   
  
    // public method for url decoding   
    decode : function (string) {   
        return this._utf8_decode(unescape(string));   
    },   
  
    // private method for UTF-8 encoding   
    _utf8_encode : function (string) {   
        string = string.replace(/\r\n/g,"\n");   
        var utftext = "";   
  
        for (var n = 0; n < string.length; n++) {   
  
            var c = string.charCodeAt(n);   
  
            if (c < 128) {   
                utftext += String.fromCharCode(c);   
            }   
            else if((c > 127) && (c < 2048)) {   
                utftext += String.fromCharCode((c >> 6) | 192);   
                utftext += String.fromCharCode((c & 63) | 128);   
            }   
            else {   
                utftext += String.fromCharCode((c >> 12) | 224);   
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);   
                utftext += String.fromCharCode((c & 63) | 128);   
            }   
  
        }   
  
        return utftext;   
    },   
  
    // private method for UTF-8 decoding   
    _utf8_decode : function (utftext) {   
        var string = "";   
        var i = 0;   
        var c = c1 = c2 = 0;   
  
        while ( i < utftext.length ) {   
  
            c = utftext.charCodeAt(i);   
  
            if (c < 128) {   
                string += String.fromCharCode(c);   
                i++;   
            }   
            else if((c > 191) && (c < 224)) {   
                c2 = utftext.charCodeAt(i+1);   
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));   
                i += 2;   
            }   
            else {   
                c2 = utftext.charCodeAt(i+1);   
                c3 = utftext.charCodeAt(i+2);   
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));   
                i += 3;   
            }   
  
        }   
  
        return string;   
    }   
  
}

/*********************/
/* SubMenu functions */
/*********************/
var subMenu = new Array();
var resetSubMenuTimeOut;

function GetSubMenu(shortID) {
    ClearSubMenuTimeOut()
    subMenuStr = "";
    if (subMenu[shortID] != null && subMenu[shortID].length > 0) {
        menu = subMenu[shortID].split("|")
        for (var i = 0; i < menu.length; i++) {
            if (menu[i].length > 0) {
                currentMenu = menu[i].split("_");
                strClass = "";
                if (currentMenu.length == 3) {
                    strClass = "class='SubMenuActive'";
                }
                subMenuStr += "<a href='" + currentMenu[0] + "' " + strClass + ">" + currentMenu[1] + "</a>";

                if (i < menu.length - 1)
                    subMenuStr += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
    }
    
    document.getElementById("submenu").innerHTML = subMenuStr;
}

function ManageSubMenu(shortID) {
    ClearSubMenuTimeOut();
    resetSubMenuTimeOut = setTimeout("GetSubMenu('" + shortID + "')", 500);
}

function ResetSubMenu(shortID) {
    ClearSubMenuTimeOut();
    resetSubMenuTimeOut = setTimeout("GetSubMenu('" + shortID + "')", 1000);
}

function ClearSubMenuTimeOut() {
    clearTimeout(resetSubMenuTimeOut);
}

/*****************
* TABS functions *
*****************/
function switchTabStyle(tabID, state) {
    if (state == "over") {
        document.getElementById("TabLeft" + tabID).src = "/~/media/WidexShared/design/tab_left_over.ashx";
        document.getElementById("TabTile" + tabID).className = "page_Tab_over";
        document.getElementById("TabRight" + tabID).src = "/~/media/WidexShared/design/tab_right_over.ashx";
    } else if (state == "out") {
        document.getElementById("TabLeft" + tabID).src = "/~/media/WidexShared/design/tab_left.ashx";
        document.getElementById("TabTile" + tabID).className = "page_Tab";
        document.getElementById("TabRight" + tabID).src = "/~/media/WidexShared/design/tab_right.ashx";
    }
}

$j(document).ready(function() {
$j('a#linkTextSize').click(function() {
        ChangeFontsize();
    });

    $j('img.changeFontSize').click(function() {
        ChangeFontsize();
    });

});

function ChangeFontsize() {
    fontSizeSmall = !fontSizeSmall;
    var url = "/Views/Ajax/FontSize.aspx?fontsize=true";
    if (!fontSizeSmall) {
        url = "/Views/Ajax/FontSize.aspx?fontsize=false";
    }
    SetFontSize();
    $j.ajax({
        type: "POST",
        url: "/Views/Ajax/FontSize.aspx",
        data: "fontsize=" + fontSizeSmall
    });
}

function SetFontSize() {
    var fontSizeText1 = 12;
    var fontSizeText2 = 14;
    var src = '/~/media/WidexShared/design/font_button_small.ashx';
    if (!fontSizeSmall) {
        fontSizeText1 = 14;
        fontSizeText2 = 16;
        src = '/~/media/WidexShared/design/font_button_large.ashx';
    }
    $j('.page_TextColumn_first, .page_TextColumn_second, h2').css('font-size', fontSizeText1);
    $j('.summary').css('font-size', fontSizeText2);
    $j('img.changeFontSize').attr('src', src);

}
