/*

Javascript מחודש - Yalla

*/

// פונקציית מציאת רוחב המסך

function windowSize(getWidth) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  if (getWidth == 1) {
    return myWidth;
  } else {
    return myHeight;
  }
}

// פונקציית שינוי גודל חלון ה-Ajax

function resizeAjaxDiv() {
    var winTop = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        winTop = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        winTop = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        winTop = document.documentElement.scrollTop;
    }
    document.getElementById('blackScreen').style.top = winTop + "px";
    document.getElementById('blackScreen').style.width = windowSize(1)+"px";
    document.getElementById('blackScreen').style.height = windowSize(0)+"px";
    document.getElementById('whiteBox').style.top = winTop + 100 + "px";
    document.getElementById('whiteBox').style.width = (windowSize(1)/10)*8+"px";
    document.getElementById('container').style.top = winTop + 100 + "px";
    document.getElementById('container').style.width = (windowSize(1)/10)*8+"px";
    document.getElementById('loadScreen').style.top = winTop + 150 + "px";
    document.getElementById('loadScreen').style.width = windowSize(1)+"px";
}

// פונקציית עמעום מסך

function opacityChange(opacStart, opacEnd, wobj) {
    var speed = 10;
    var timer = 0;
    
    if (!wobj) {
        var wobj = 'whiteBox';
    }
    
    if (opacStart > opacEnd) {   
        for (i = opacStart; i >= opacEnd; i-=2) {
            if (timer == 0) { 
                if (wobj == 'whiteBox') {
                    document.getElementById(wobj).style.display = "block"; 
                } else {
                    document.getElementById(wobj).style.visibility = "visible";
                }
            }
            setTimeout("document.getElementById('"+wobj+"').style.opacity = "+i/100+"; document.getElementById('"+wobj+"').style.MozOpacity = "+i/100+"; document.getElementById('"+wobj+"').style.filter = 'alpha(opacity="+i+")';", (timer * speed));
            
            timer++;
        }
        
        setTimeout("document.getElementById('"+wobj+"').style.display = 'none'", 600);
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i+=2) {
            if (wobj == 'whiteBox') {
                document.getElementById(wobj).style.display = "block"; 
            } else {
                document.getElementById(wobj).style.visibility = "visible";
            }
            setTimeout("document.getElementById('"+wobj+"').style.opacity = "+i/100+"; document.getElementById('"+wobj+"').style.MozOpacity = "+i/100+"; document.getElementById('"+wobj+"').style.filter = 'alpha(opacity="+i+")';", (timer * speed));
            
            timer++;
        }
    }
}

// פונקציית הפיכת אובייקט לשקוף

function makeTransparent(i) {
    document.getElementById(i).style.opacity = "0"; 
    document.getElementById(i).style.MozOpacity = "0"; 
    document.getElementById(i).style.filter = 'alpha(opacity="0")';
}

// פונקציית שינוי גודל חלון
function doResize(t,s,w,h) {
    if (t == 0) {
        document.getElementById('igwt').style.width = s+"px";
    } else {
        document.getElementById('igwt').style.height = s+"px";
    }
    
    if (parseInt(document.getElementById('igwt').style.width) == w && parseInt(document.getElementById('igwt').style.height) == h) {
        document.getElementById('containertd').style.width = w+"px";
        document.getElementById('containertd').style.height = h+"px";
        document.getElementById('igt').style.width = (w-6)+"px";
        document.getElementById('igwb').style.width = (w-6)+"px";
        makeTransparent('igt');
        document.getElementById('igt').style.display = '';
        opacityChange(0,100,'igt');
        makeTransparent('igp');
        document.getElementById('imd').style.display = ''; 
        setTimeout("opacityChange(0,100,'igp')", 600);
        makeTransparent('igwb');
        document.getElementById('igwb').style.display = '';
        setTimeout("opacityChange(0,100,'igwb')", 1200);
    }
}

function autoResize(w,h) {
    var speed = 5;
    var timer = 0;
    var w = parseInt(w)+10; // להוסיף 8 פיקסלים לטובת הגבולות
    var h = parseInt(h)+106; // להוסיף 20 פיקסלים לטובת הבאר התחתון
    
    currentWidth = parseInt(document.getElementById('igwt').style.width);
    currentHeight = parseInt(document.getElementById('igwt').style.height);

    if (w > currentWidth) {
        for (i = currentWidth; i <= w; i+=2) {
            setTimeout("doResize(0,"+i+","+w+","+h+")", (speed * timer));
            
            timer++;
        }
    } else if (w < currentWidth) {
        for (i = currentWidth; i >= w; i-=2) {
            if (i == (w+1)) {
                i-=1;
            }
            setTimeout("doResize(0,"+i+","+w+","+h+")", (speed * timer));
            
            timer++;
        }
    }
    
    if (h > currentHeight) {
        for (i = currentHeight; i <= h; i+=2) {
            if (i == (h-1)) {
                i+=1;
            }
            setTimeout("doResize(1,"+i+","+w+","+h+")", (speed * timer));
            
            timer++;
        }
    } else if (h < currentHeight) {
        for (i = currentHeight; i >= h; i-=2) {
            if (i == (h+1)) {
                i-=1;
            }
            setTimeout("doResize(1,"+i+","+w+","+h+")", (speed * timer));
            
            timer++;
        }
    }
}

// פקודת Ajax

function updateWhiteBox(page,getJS,params,ig,w,h)
{
    if (!ig) {
        ig = 0;
    }
    if (!w) {
        w = 0;
    }
    if (!h) {
        h = 0;
    }
    if (page.substr(0,7) == "http://") {
        resizeAjaxDiv();
        document.getElementById('whiteBox').style.display = "block";
        document.getElementById('whiteBox').style.opacity = "100"; 
        document.getElementById('whiteBox').style.MozOpacity = "100"; 
        document.getElementById('whiteBox').style.filter = 'alpha(opacity="100")';
        ajaxText = '<table align="center" style="width: auto; border: 3px solid #000000" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF"><tr><td align="center" valign="top">';
        ajaxText = ajaxText + '<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" style="bottom: 0px"><tr><td width="100%" align="left" style="padding: 2px 0px 4px 4px;" bgcolor="#575757" valign="top"><div onclick="javscript:opacityChange(100,0)" style="cursor: pointer" onmouseover="document.getElementById(\'cwt\').style.color=\'#FFF\'; document.getElementById(\'cwi\').className=\'yallasprite ximgh\'" onmouseout="document.getElementById(\'cwt\').style.color=\'#C4C4C4\'; document.getElementById(\'cwi\').className=\'yallasprite ximg\'"><div id="cwt" style="display: inline; font-size: 11px; font-family: arial; color: #C4C4C4; float: left">סגור חלון</div><img src="http://p1.yalla.co.il/yf/pixel.gif" id="cwi" class="yallasprite ximg" style="float: left; margin: 4px 0px 0px 2px"></div></td></tr>';
        ajaxText = ajaxText + '</table><div style="border: 1px solid #FFF"><iframe width="100%" height="350" scrolling="no" frameborder="0" src="'+page+'"></iframe></div></td></tr></table>';
        document.getElementById('whiteBox').innerHTML = ajaxText;
    } else {
        var xmlHttp;
        try
        {
          xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
          try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
          catch (e)
        {
            try
              {
                  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            catch (e)
              {
                  alert("הדפדפן שלך אינו תומת ב- AJAX!");
                  return false;
              }
        }
      }
    xmlHttp.open("POST", page, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                resizeAjaxDiv();
                document.getElementById('loadScreen').style.display = "none";
                document.getElementById('whiteBox').style.display = "block";
                document.getElementById('whiteBox').style.opacity = "1"; 
                document.getElementById('whiteBox').style.MozOpacity = "1"; 
                document.getElementById('whiteBox').style.filter = 'alpha(opacity="100")';
                if (getJS == 1) {
                    eval (xmlHttp.responseText);
                } else {
                    if (ig == 1) {
                        //makeTransparent('blackScreen');                                                
                        document.getElementById('blackScreen').style.display = "block";
                        //opacityChange(0,80,'blackScreen');
                        document.getElementById('blackScreen').style.opacity = "0.8"; 
                        document.getElementById('blackScreen').style.MozOpacity = "0.8"; 
                        document.getElementById('blackScreen').style.filter = 'alpha(opacity="80")';
                        document.getElementById('container').style.display = "block";
                        ajaxText = '<table align="center" style="width: 300px; height: 300px; border: 3px solid #FFFFFF; background-color: #FFF; z-index: 2" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" id="igwt"><tr><td style="width: 200px; height: 200px; background-color: #FFF !important" align="center" valign="top" id="igw">';
                        ajaxText = ajaxText + '<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" style="bottom: 0px; display: none" id="igt"><tr><td width="100%" align="left" style="padding: 2px 0px 4px 4px;" bgcolor="#575757" valign="top"><div onclick="javscript:opacityChange(100,0)" style="cursor: pointer" onmouseover="document.getElementById(\'cwt\').style.color=\'#FFF\'; document.getElementById(\'cwi\').className=\'yallasprite ximgh\'" onmouseout="document.getElementById(\'cwt\').style.color=\'#C4C4C4\'; document.getElementById(\'cwi\').className=\'yallasprite ximg\'"><div id="cwt" style="display: inline; font-size: 11px; font-family: arial; color: #C4C4C4; float: left">סגור חלון</div><img src="http://p1.yalla.co.il/yf/pixel.gif" id="cwi" class="yallasprite ximg" style="float: left; margin: 4px 0px 0px 2px"></div></td></tr>';
                        ajaxText = ajaxText + '</table><div style="border: 1px solid #FFF; display: none; background-color: #FFF" id="imd"></div><br/><div style="background-color: #FFF; display: none; float: left; bottom: 5px; text-align: center" class="a12bla" id="igwb"><div class="a12dgra">תמונה 1 מוך 1</div><img src="http://p1.yalla.co.il/yf/pixel.gif" class="yallasprite nexti" onmouseover="this.className=\'yallasprite nextih\'" onmouseout="this.className=\'yallasprite nexti\'"><img src="http://p1.yalla.co.il/yf/pixel.gif" class="yallasprite previ" onmouseover="this.className=\'yallasprite previh\'" onmouseout="this.className=\'yallasprite previ\'"></div></td></tr></table>';
                        document.getElementById('whiteBox').innerHTML = ajaxText;
                        document.getElementById('imd').innerHTML = xmlHttp.responseText;
                        setTimeout("autoResize("+w+","+h+")", 480);
                    } else {
                        document.getElementById('whiteBox').innerHTML = '<table align="center" style="width: auto; border: 3px solid #000000" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF"><tr><td width="100%" align="center">'+xmlHttp.responseText+'</td></tr></table>';
                    }   
                }
                 
        } else {
            document.getElementById('loadScreen').style.display = "block";
        }
      }
      xmlHttp.send(params);
    }
}
