/// <reference path="jquery-1.4.1-vsdoc.js" />

function ToggleLoadingOverlay(containerSelector, fadeBackground) {
    if (containerSelector != null) {
        $container = $(containerSelector);
        $("#loadingOverlay").css({
            top: $container.offset().top,
            left: $container.offset().left,
            width: $container.outerWidth(),
            height: $container.outerHeight(),
            background: "none"
        });
        $("#loadingAnim").css({
            top: ($container.height() / 2),
            left: ($container.width() / 2)
        });

        if (fadeBackground) {
            $("#loadingOverlay").css({
                opacity: 0.5,
                background: "#ffffff"
            });
        }

        $("#loadingOverlay").fadeIn(200);
    }
    else {
        $("#loadingOverlay").fadeOut(200);
    }
}

var currentInfoPopup = "";
function ToggleInfoPopup(showBySelector, contentSelector) {
    
    if (showBySelector) {
        if (currentInfoPopup == showBySelector) {
            $("#InfoPopup").fadeOut(130, ClearInfoPopup());
        }
        else {
            ClearInfoPopup();
            $("#InfoContent").append($(contentSelector).contents().clone());
            var pos = $(showBySelector).offset();
            $("#InfoPopup").css({ left: pos.left + 35 + "px", top: pos.top - 70 + "px" });
            $("#InfoPopup").fadeIn(170);
            currentInfoPopup = showBySelector;
        }
    }
    else {
        $("#InfoPopup").fadeOut(130, ClearInfoPopup());
        
    }
}
function ClearInfoPopup() {
    $("#InfoPopup").hide();
    $("#InfoContent").empty();
    currentInfoPopup = "";
}


function buttonEndings() {
//    if (!document.getElementsByTagName) {
//	    return false
//    }
//    var buttons = getElementsByClass("button");
//    /* loop through all buttons and attach a div */
//    for (i=0; i < buttons.length; i++) {
//	    var div = document.createElement("div");
//	    div.className = "roundedButtonEnding";
//	    insertAfter(div, buttons[i]);
//    }
//    var buttonsOff = getElementsByClass("buttonOff");
//    /* loop through all buttons and attach a div */
//    for (i=0; i < buttonsOff.length; i++) {
//	    var div = document.createElement("div");
//	    div.className = "roundedButtonEndingOff";
//	    insertAfter(div, buttonsOff[i]);
//    }
}




///* http://www.dustindiaz.com/getelementsbyclass/ */
//function getElementsByClass(searchClass,node,tag) {
//    var classElements = new Array();
//    if ( node == null )
//	    node = document;
//    if ( tag == null )
//	    tag = '*';
//    var els = node.getElementsByTagName(tag);
//    var elsLen = els.length;
//    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
//    for (i = 0, j = 0; i < elsLen; i++) {
//	    if ( pattern.test(els[i].className) ) {
//		    classElements[j] = els[i];
//		    j++;
//	    }
//    }
//    return classElements;
//}

///* by Jeremy Keith */
//function insertAfter(newElement, targetElement) {
//    var parent = targetElement.parentNode;
//    if (parent.lastChild == targetElement) {
//	    parent.appendChild(newElement);
//    }
//    else {
//	    parent.insertBefore(newElement, targetElement.nextSibling);
//    }
//}
//    
//function ShowArea(divId) 
//{
//	document.getElementById(divId).style.visibility = "visible";
//}

//function HideArea(divId) 
//{
//	document.getElementById(divId).style.visibility = "hidden";
//}	
//function SetMainImage(targetId, mainImage)
//{
//    document.getElementById(targetId).src = mainImage;
//}
function ShowProductsResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'block';
        document.all['AccessoryResult'].style.display = 'none';
        document.all['SiteSearchResult'].style.display = 'none';
        // document.all['DocumentSearchResult'].style.display = 'block';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'block';
        document.getElementById('AccessoryResult').style.display = 'none';
        document.getElementById('SiteSearchResult').style.display = 'none';
        // document.getElementById('DocumentSearchResult').style.display = 'none';
    }
}

function ShowAccessoryResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'none';
        document.all['AccessoryResult'].style.display = 'block';
        document.all['SiteSearchResult'].style.display = 'none';
        // document.all['DocumentSearchResult'].style.display = 'none';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'none';
        document.getElementById('AccessoryResult').style.display = 'block';
        document.getElementById('SiteSearchResult').style.display = 'none';
        // document.getElementById('DocumentSearchResult').style.display = 'none';
    } 
}

function ShowSiteSearchResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'none';
        document.all['AccessoryResult'].style.display = 'none';
        document.all['SiteSearchResult'].style.display = 'block';
        // document.all['DocumentSearchResult'].style.display = 'none';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'none';
        document.getElementById('AccessoryResult').style.display = 'none';
        document.getElementById('SiteSearchResult').style.display = 'block';
        // document.getElementById('DocumentSearchResult').style.display = 'none';
    } 
}
function ShowDocumentSearchResults() 
{
    if(document.all) 
    {
        document.all['ProductsResult'].style.display = 'none';
        document.all['AccessoryResult'].style.display = 'none';
        document.all['SiteSearchResult'].style.display = 'none';
        // document.all['DocumentSearchResult'].style.display = 'block';
    } 
    else 
    {
        document.getElementById('ProductsResult').style.display = 'none';
        document.getElementById('AccessoryResult').style.display = 'none';
        document.getElementById('SiteSearchResult').style.display = 'none';
        // document.getElementById('DocumentSearchResult').style.display = 'block';
    } 
}

function SaveAccessory(pageid, lang) {

    $.get("/Ajax/LastViewedItems2.aspx?floorId=&pageid=" + pageid + "&languageCode="+lang, function (response) {
        if (response == "EmptyResponse") {
            $('#messageBox' + pageid).html("Accessory already added").fadeIn(1000).fadeOut(1000);
        } else {
            $('#messageBox' + pageid).html("Accessory saved").fadeIn(1000).fadeOut(1000);
            $("#lastViewedItemContainer").show();
            $("#lastViewedItemsHolder").html(response).show();
            $("#lastViewedItemsFooter").show();
        }
    });
}

////////////////////////////////////////////////////////////
// AJAX code
////////////////////////////////////////////////////////////

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;
	}
 
}
