﻿function GoPage(iPageNumber, iSort)
{
    if (iSort == 0)
    {
        if (document.getElementById("ctl00_ContentPlaceHolder1_hdnSort"))
            iSort = document.getElementById("ctl00_ContentPlaceHolder1_hdnSort").value;
    }
    
    var sUrl = window.location.href.split("?");
    if (sUrl.length > 1)
    {
        //with parameters
        var ArrParams = sUrl[1].split("&");
        var sNewParams = "?";
        var bPageParamExist = false;
        var bSortParamExist = false;
        
        for (i = 0; i < ArrParams.length; i++)
        {
            if (ArrParams[i].indexOf("PAGE") >= 0)
            {
                bPageParamExist = true;
                sNewParams = sNewParams + "PAGE=" + iPageNumber + "&";
            }
            else if (ArrParams[i].indexOf("SORT") >= 0)
            {
                bSortParamExist = true;
                sNewParams = sNewParams + "SORT=" + iSort + "&";
            }
            else
                sNewParams = sNewParams + ArrParams[i] + "&";
        }
        
        if (!bPageParamExist)
            sNewParams = sNewParams + "PAGE=" + iPageNumber + "&";
            
        if (!bSortParamExist)
            sNewParams = sNewParams + "SORT=" + iSort + "&";
            
        window.location.href = sUrl[0] + sNewParams.substring(0, sNewParams.length - 1);
    }
    else
    {
        // no parameters
        window.location.href = location.href + "?PAGE=" + iPageNumber + "&SORT=" + iSort;
    }
}

function GoSort()
{
    GoPage(1, document.getElementById("cmbSort").options[document.getElementById("cmbSort").selectedIndex].value);
}


function EncodeUTF8(sString)
{
    sString = sString.replace(/\r\n/g, "\n");
    var sUTFString = "";

    for (var i = 0; i < sString.length; i++)
    {
        var c = sString.charCodeAt(i);
        if (c < 128)
            sUTFString += String.fromCharCode(c);
        else if ((c > 127) && (c < 2048))
        {
            sUTFString += String.fromCharCode((c >> 6) | 192);
            sUTFString += String.fromCharCode((c & 63) | 128);
        }
        else
        {
            sUTFString += String.fromCharCode((c >> 12) | 224);
            sUTFString += String.fromCharCode(((c >> 6) & 63) | 128);
            sUTFString += String.fromCharCode((c & 63) | 128);
        }
    }
    return escape(sUTFString);
}

function DecodeUTF8(sUTFString)
{
    sUTFString = unescape(sUTFString);

    var sString = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while (i < sUTFString.length)
    {
        c = sUTFString.charCodeAt(i);
        if (c < 128)
        {
            sString += String.fromCharCode(c);
            i++;
        }
        else if ((c > 191) && (c < 224))
        {
            c2 = sUTFString.charCodeAt(i + 1);
            sString += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else
        {
            c2 = sUTFString.charCodeAt(i + 1);
            c3 = sUTFString.charCodeAt(i + 2);
            sString += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    return sString;
}


this.imagePreview = function()
{		
		xOffset = 10;
		yOffset = 30;

	$("img.preview").hover(function(e){
		this.t = this.title;
		this.title = "";
		var tempTitle = this.t.split("|");
		var sUrl = tempTitle[0];
		var sTitle = "";		
		if (tempTitle.length == 2)
		    sTitle = tempTitle[1];
		var c = (sTitle != "") ? "<br/>" + sTitle : "";
		$("body").append("<p id='preview'><img src='"+ sUrl +"' width='300px' height='300px' />"+ c +"</p>");
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#preview").remove();
    });	
	$("img.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};


this.screenshotPreview = function(){	
		
		xOffset = 10;
		yOffset = 30;

	$("a.linkTopTen").hover(function(e){
		this.t = this.title;
		this.title = "";
		var tempTitle = this.t.split("|");
		var sUrl = tempTitle[0];
		var sTitle = "";		
		if (tempTitle.length == 2)
		    sTitle = tempTitle[1];		
		var c = (sTitle != "") ? "<br/>" + sTitle : "";
		$("body").append("<p id='screenshot'><img src='"+ sUrl +"' width='300px' height='300px' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.linkTopTen").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

/*
// init on master page
// starting the script on page load
$(document).ready(function(){
	imagePreview();
	screenshotPreview();
});
*/
