﻿// JScript File
function getParam(strParamName) {
    var strReturn = "";
    var strHref = window.location.href;
    if (strHref.indexOf("?") > -1) {
        var strQueryString = strHref.substr(strHref.indexOf("?") + 1).toLowerCase();
        var aQueryString = strQueryString.split("&");
        var lastArray;
        for (var iParam = 0; iParam < aQueryString.length; iParam++) {
            lastArray = aQueryString[iParam].split("=");
            if (lastArray[0] == strParamName) {
                strReturn = lastArray[1];
                break;
            }
        }
    }
    return strReturn;
}
function preloadImage(path) {    // preload image by adding a hidden image to the document
    var img = new Image();
    img.src = path;
    /*
    var img = document.createElement('img');
    alert(path);
    img.src = path;
    img.style.display = 'none';
    document.body.appendChild(img);*/
}
function ShowProduct(img) {
    //alert(img);
    var strImg = "<img src='"+img+"' />";
    document.getElementById("ctl00_ContentPlaceHolder1_tdProductImg").innerHTML = strImg;
}
function AddNewQuestion() {
    document.getElementById("ctl00_ContentPlaceHolder1_divNewQuestion").style.display = "";
}
function closeQuestion() {
    document.getElementById("ctl00_ContentPlaceHolder1_divNewQuestion").style.display = "none";
}
function FillSelectedArticle(articleId) {
    document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedArticle").value = articleId;
    document.getElementById("ctl00_ContentPlaceHolder1_btnAjax").click();
}
function FillSelectedRealtor(realtorC) {
    document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedRealtor").value = realtorC;
    document.getElementById("ctl00_ContentPlaceHolder1_btnAjax").click();
}
function FillSelectedReferSubject(referSubjectC) {
    document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedReferSubject").value = referSubjectC;
    document.getElementById("ctl00_ContentPlaceHolder1_btnAjax").click();
}
function RefreshReferSubjectsList() {
    document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aReferTypes").click();
}
function RefreshCategoriesList()
{
    document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aCategories").click();
}
function RefreshLinksList()
{
    document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aLinks").click();
}
function RefreshQuestionsList() {
    document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aQuestions").click();
}
function RefreshArticlesList() {
    var type = getParam("type");
    if (type == "1")   /* Articles */
    {
        document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aArticles").click();
    }
    if (type == "2")    /* News */
        document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aNews").click();
}
function RefreshRealtorsList() {
    document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aRealtors").click();
}
function RefreshCareersList() {
    document.getElementById("ctl00_ContentPlaceHolder1_UcMenuManager1_aCareers").click();

}
function FillSelectedCareer(careerId) {
    document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedCareer").value = careerId;
    document.getElementById("ctl00_ContentPlaceHolder1_btnAjax").click();
}
function FillSelectedLink(linkId) {
    document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedLink").value = linkId;
    document.getElementById("ctl00_ContentPlaceHolder1_btnAjax").click();
}
function FillSelectedCategory(categoryId) {
    document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedCategory").value = categoryId;
    document.getElementById("ctl00_ContentPlaceHolder1_btnAjax").click();
}
function FillSelectedQuestion(questionId) {
    document.getElementById("ctl00_ContentPlaceHolder1_hidSelectedQuestion").value = questionId;
    document.getElementById("ctl00_ContentPlaceHolder1_btnAjax").click();
}
/***********************************************
* Pausing up-down scroller- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
function pausescroller(content, divId, divClass, delay) {
    if (content.length == 0)
        return;
    this.content = content //message array content
    this.tickerid = divId //ID of ticker div to display information
    this.delay = delay //Delay between msg change, in miliseconds.
    this.mouseoverBol = 0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
    this.hiddendivpointer = 1 //index of message array for hidden div
    document.write('<div id="' + divId + '" class="' + divClass + '" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="' + divId + '1">' + content[0] + '</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="' + divId + '2">' + content[1] + '</div></div>')
    var scrollerinstance = this
    if (window.addEventListener) //run onload in DOM2 browsers
        window.addEventListener("load", function() { scrollerinstance.initialize() }, false)
    else if (window.attachEvent) //run onload in IE5.5+
        window.attachEvent("onload", function() { scrollerinstance.initialize() })
    else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
        setTimeout(function() { scrollerinstance.initialize() }, 500)
}
// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------
pausescroller.prototype.initialize = function() {
    this.tickerdiv = document.getElementById(this.tickerid)
    this.visiblediv = document.getElementById(this.tickerid + "1")
    this.hiddendiv = document.getElementById(this.tickerid + "2")
    this.visibledivtop = parseInt(pausescroller.getCSSpadding(this.tickerdiv))
    //set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
    this.visiblediv.style.width = this.hiddendiv.style.width = this.tickerdiv.offsetWidth - (this.visibledivtop * 2) + "px"
    this.getinline(this.visiblediv, this.hiddendiv)
    this.hiddendiv.style.visibility = "visible"
    var scrollerinstance = this
    document.getElementById(this.tickerid).onmouseover = function() { scrollerinstance.mouseoverBol = 1 }
    document.getElementById(this.tickerid).onmouseout = function() { scrollerinstance.mouseoverBol = 0 }
    if (window.attachEvent) //Clean up loose references in IE
        window.attachEvent("onunload", function() { scrollerinstance.tickerdiv.onmouseover = scrollerinstance.tickerdiv.onmouseout = null })
    setTimeout(function() { scrollerinstance.animateup() }, this.delay)
}
// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------
pausescroller.prototype.animateup = function() {
    var scrollerinstance = this
    if (parseInt(this.hiddendiv.style.top) > (this.visibledivtop + 1)) {
        this.visiblediv.style.top = parseInt(this.visiblediv.style.top) - 1 + "px"
        this.hiddendiv.style.top = parseInt(this.hiddendiv.style.top) - 1 + "px"
        setTimeout(function() { scrollerinstance.animateup() }, 50)
    }
    else {
        this.getinline(this.hiddendiv, this.visiblediv)
        this.swapdivs()
        setTimeout(function() { scrollerinstance.setmessage() }, this.delay)
    }
}
// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------
pausescroller.prototype.swapdivs = function() {
    var tempcontainer = this.visiblediv
    this.visiblediv = this.hiddendiv
    this.hiddendiv = tempcontainer
}
pausescroller.prototype.getinline = function(div1, div2) {
    div1.style.top = this.visibledivtop + "px"
    div2.style.top = Math.max(div1.parentNode.offsetHeight, div1.offsetHeight) + "px"
}
// -------------------------------------------------------------------
// setmessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

pausescroller.prototype.setmessage = function() {
    var scrollerinstance = this
    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        setTimeout(function() { scrollerinstance.setmessage() }, 100)
    else {
        var i = this.hiddendivpointer
        var ceiling = this.content.length
        this.hiddendivpointer = (i + 1 > ceiling - 1) ? 0 : i + 1
        this.hiddendiv.innerHTML = this.content[this.hiddendivpointer]
        this.animateup()
    }
}
pausescroller.getCSSpadding = function(tickerobj) { //get CSS padding value, if any
    if (tickerobj.currentStyle)
        return tickerobj.currentStyle["paddingTop"]
    else if (window.getComputedStyle) //if DOM2
        return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
    else
        return 0
}
function GetScrolling() {


    var pausecontent = new Array();
    var newsArr = document.getElementById("ctl00_hidNewsDiv").innerHTML.split("|");
    var ln = getParam("ln");
    if (ln == "")
        ln = "he";
    var j = 0;
    for (i = 0; i < newsArr.length; i++) {
        if (newsArr[i]) {
            pausecontent[j] = '<a href="' + newsArr[i+1] + '&ln=' + ln + '" ><font color="#524e4a">' + newsArr[i] + '</font></a>';
        }
        i++;
        j++;
    }
    
    if (pausecontent.length == 0) {
        pausecontent[0] = "";
        pausecontent[1] = "";
    }
    if (pausecontent.length == 1) {
        pausecontent[1]="";
    }
    
    return new pausescroller(pausecontent, "pscroller2", "someclass", 4000);
}

function GetProductsIcons(category, language) 
{   /* Get products icons according to the chosen category */
    LoadImages.LoadProductsForCategory(category, language, PutProductsForCategory);
}
function PutProductsForCategory(result) 
{   /* Loading the products icons that were recieved from the web service */
    var data = result.evalJSON();
    var strHTML = "";
    var flag = false;
    
    strHTML += "<table>";

    for (var i = 0; i < data.length; i++) 
    {
        var product = data[i];
        var categoryId = product.categoryId;
        var productId = product.productId;
        var iconUrl = product.iconUrl;
        
        var language = "he";
        if (!flag) {
            GetProductSmallImages(categoryId,productId,language);
            flag = true;
        }
        strHTML += "    <tr>";
        strHTML += "        <td>";
        strHTML += "            <a href='#' onclick='GetProductSmallImages(\"" + categoryId + "\",\"" + productId + "\",\"" + language + "\");'><img border='0' src='" + iconUrl + "'/></a>";
        strHTML += "        </td>";
        strHTML += "    </tr>";
    }
    strHTML += "</table>";
    document.getElementById("ctl00_ContentPlaceHolder1_tdProductsForCategory").innerHTML = strHTML;
}
function GetProductSmallImages(categoryId, productId, language) {
    LoadImages.LoadProductSmallImages(categoryId, productId, language, PutProductSmallImages);
}

function PutProductSmallImages(result) {
    var data = result.evalJSON();
    var strHTML = "";
    var hasFirstImg = false;
    
    strHTML += "<table><tr>";
    var j = 0;
    for (var i = 0; i < data.length; i++) {
        if (j == 4) {
            strHTML += "</tr><tr>";
            j = 0;
        }
        var product = data[i];
        var imgUrl = product.imgUrl;
        var productId = product.productId;
        var iconUrl = product.iconUrl;
        
        var language = "he";
        if (!hasFirstImg) {
            ShowProduct(imgUrl);
            hasFirstImg = true;
        }
        strHTML += "<td>";
        strHTML += "    <a href='#' onmouseover='ShowProduct(\"" + imgUrl + "\");'><img border='0' src='" + iconUrl + "'/></a>&nbsp;";
        strHTML += "</td>";

        preloadImage(imgUrl);
        j++;
    }
    strHTML += "</tr></table>";
    document.getElementById("ctl00_ContentPlaceHolder1_tdProductsSmallImages").innerHTML = strHTML;
}

function ClearCategory() {
    document.getElementById("ctl00_ContentPlaceHolder1_tdProductsSmallImages").innerHTML = "";
    document.getElementById("ctl00_ContentPlaceHolder1_tdProductImg").innerHTML = "";
}
/******************* TEST - JSON **********************
function GetSynchronousJSONResponse(url, postData) {
    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    //false means synchronous
    xmlhttp.open("POST", url, false);
    xmlhttp.setRequestHeader("Content-Type","application/json; charset=utf-8");
    xmlhttp.send(postData);
    var responseText = xmlhttp.responseText;
    return responseText;
}

function GetData() {
    var url = "http://localhost/Bfocus/LoadImages.asmx/LoadProductsForCategory('1','he')";
    var result = GetSynchronousJSONResponse(url, null);
    //alert(result);//{d:{1:moshi,2:cohen}}
    var data = eval('(' + result + ')'); //.evalJSON();
    alert(data); // [object Object]
    for (var i = 0; i < result.d.length; i++) {
        var product = result.d[i];
        var categoryId = product.categoryId;
        var productId = product.productId;
        var iconUrl = product.iconUrl;
    }
}

***/


