// פונקציית מציאת גודל החלון
function windowSize(getWidth) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  if (getWidth == 1) {
    return myWidth;
  } else {
    return myHeight;
  }
}
// פונקציית שינוי גודל חלון האג'אקס
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('blackBox').style.top = winTop + "px";
    document.getElementById('blackBox').style.width = (windowSize(1)-20)+"px";
    document.getElementById('blackBox').style.height = (windowSize(0)-20)+"px";
    document.getElementById('blackBox').style.paddingTop = "70px";
    document.getElementById('whiteBox').style.top = winTop + "px";
    document.getElementById('whiteBox').style.width = (windowSize(1)-20)+"px";
    document.getElementById('whiteBox').style.height = (windowSize(0)-30)+"px";
    document.getElementById('whiteBox').style.paddingTop = "80px";
    document.getElementById('container').style.top = winTop + "px";
    document.getElementById('container').style.width = (windowSize(1)-20)+"px";
    document.getElementById('container').style.height = (windowSize(0)-30)+"px";
    document.getElementById('container').style.paddingTop = "80px";
    document.getElementById('loadScreen').style.top = winTop + 150 + "px";
    document.getElementById('loadScreen').style.width = windowSize(1)+"px";
}
// פונקציית עמעום חלון
function opacityChange(opacStart, opacEnd, di, db) {
    var speed = 10;
    var timer = 0;
    
    if (!di) {
        di = 'whiteBox';
    }
    
    if (!db) {
        db = 0;
    }
    
    if (opacStart > opacEnd) {   
        if (db == 0) {
            document.getElementById('container').style.display = 'none';
        }
        for (i = opacStart; i >= opacEnd; i-=2) {
            //if (timer == 0) { document.getElementById('whiteBox').style.display = "block"; }
            setTimeout("setOpacity("+i+",'"+di+"',"+db+")", timer * speed);
            
            timer++;
        }
        
        setTimeout("document.getElementById('"+di+"').style.display = 'none'", 510);
        if (db == 0) {
            setTimeout("document.getElementById('blackBox').style.display = 'none'", 510);
        }
    } else {
        setOpacity(1,di,1);
        document.getElementById(di).style.display = 'block';
        
        for (i = opacStart; i <= opacEnd; i+=2) {
            //if (timer == 0) { document.getElementById('whiteBox').style.display = "block"; }
            setTimeout("setOpacity("+i+",'"+di+"',"+db+")", timer * speed);
            timer++;
        }
    }
}
function setOpacity(ol,di,db) {
     document.getElementById(di).style.opacity = ol/100; 
     document.getElementById(di).style.MozOpacity = ol/100; 
     document.getElementById(di).style.filter = 'alpha(opacity='+ol+')';
     if (db == 0) {
        document.getElementById('blackBox').style.opacity = ol/100; 
        document.getElementById('blackBox').style.MozOpacity = ol/100; 
        document.getElementById('blackBox').style.filter = 'alpha(opacity='+ol+')';
     }
}

function autoResize(id) {
    var speed = 5;
    var timer = 0;
    var bottomHeight = 100;
    var newImg = new Image();
    newImg.src = document.getElementById(id).src;
    winHeight = parseInt(document.getElementById('aw').style.height);
    picHeight = parseInt(newImg.height);
    if (picHeight < (winHeight-bottomHeight)) {
        distance = Math.round((winHeight-(picHeight+bottomHeight))/25);
        for (i = winHeight; i >= (picHeight+bottomHeight); i-=distance) {
            if (i == ((picHeight+bottomHeight)+1)) {
                i = picHeight+bottomHeight;
            }
            setTimeout("doResize("+i+","+picHeight+","+winHeight+",'"+id+"')", timer * speed)
            
            distance = Math.round((i-(picHeight+bottomHeight))/25);
            if (distance < 2) {
                distance = 2;   
            }
            timer++;
        }
    } else {
        distance = Math.round(((picHeight+bottomHeight)-winHeight)/2);
        for (i = winHeight; i <= (picHeight+bottomHeight); i+=distance) {
            if (i == ((picHeight+bottomHeight)-1)) {
                i = picHeight+bottomHeight;
            }
            setTimeout("doResize("+i+","+picHeight+","+winHeight+",'"+id+"')", timer * speed)
            
            distance = Math.round(((picHeight+bottomHeight)-i)/25);
            if (distance < 2) {
                distance = 2;   
            }
            timer++;
        }
    }
}

function doResize(i,ph,wh,id) {
    windowHeight = windowSize(0);
    if ((((i+70+20) < windowHeight) && ph > wh) || ph <= wh) {
        document.getElementById('aw').style.height = i+"px"; 
        document.getElementById('containertd').style.height = i+"px";
        document.getElementById('blacktd').style.height = (i+20)+"px";
        document.getElementById('ajaxBottom').style.top = (i-70)+"px";
    }
    
    if ((ph+100) == i) {
        if ((ph+70+25+100) > windowHeight) {
            ajaxMainHeight = windowHeight-70-25-100;
        } else {
            ajaxMainHeight = ph;
        }
        document.getElementById('ajaxMain').style.height = ajaxMainHeight+"px";
        opacityChange(0,100,id,1);
        document.getElementById('curPic').value = parseInt(id.replace("pic_",""));
    }
}
// פונקציית האג'אקס הכללית
var xmlHttp;

function ajaxOp(page,getJS,params,ll,ig,w,h,a)
{
    if (!ig) {
        ig = 0;
    } else {
        h += 120;
    }
    if (!w) {
        w = "auto";
    }
    if (!h) {
        h = "auto";
    }
    
    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;
              }
        }
      }
    if (a == 1) {
        xmlHttp.abort();
    }
    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();
                if (ll != '' && ll != undefined) {
                    if (ll != 1) {
                        document.getElementById(ll).style.display = "none";
                    }
                } else {
                    document.getElementById('loadScreen').style.display = "none";
                }
                if (getJS == 1) {
                    eval (xmlHttp.responseText);
                } else {
                    document.getElementById('blacktd').style.width = w+20+"px";
                    document.getElementById('blacktd').style.height = h+20+"px";
                    document.getElementById('blackBox').style.display = "block";
                    document.getElementById('blackBox').style.opacity = "100"; 
                    document.getElementById('blackBox').style.MozOpacity = "100"; 
                    document.getElementById('blackBox').style.filter = 'alpha(opacity="100")';
                    document.getElementById('containertd').style.width = w+"px";
                    document.getElementById('containertd').style.height = h+"px";
                    document.getElementById('container').style.display = "block";
                    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: '+(w+2)+'px; height: '+(h+2)+'px; border: 1px solid #FFF" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" id="aw"><tr><td align="center" valign="top" style="overflow: hidden">'+xmlHttp.responseText+'</td></tr></table>';
                    
                    //ajaxText = '<table align="center" style="width: '+w+'; height: '+h+'; 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=\'yallacsprite ximgh\'" onmouseout="document.getElementById(\'cwt\').style.color=\'#C4C4C4\'; document.getElementById(\'cwi\').className=\'yallacsprite ximg\'"><div id="cwt" style="display: inline; font-size: 11px; font-family: arial; color: #C4C4C4; float: left">סגור חלון</div><img src="http://p1.yala.co.il/yf/pixel.gif" id="cwi" class="yallacsprite ximg" style="float: left; margin: 4px 0px 0px 2px"></div></td></tr>';
                    //ajaxText = ajaxText + '</table><div style="border: 1px solid #FFF">'+xmlHttp.responseText+'</div></td></tr></table>';
                    document.getElementById('whiteBox').innerHTML = ajaxText;
                                        
                    //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 {
            if (ll != '' && ll != undefined) {
                if (ll != 1) {
                    document.getElementById(ll).style.display = 'inline';
                    document.getElementById(ll).innerHTML = '<img src="http://p1.yala.co.il/m/lloader.gif">';
                }
            } else {
                document.getElementById('loadScreen').style.display = "block";
            }
        }
      }
      xmlHttp.send(params);
}

function changePic(gt) {
    if (document.getElementById('maxPic').value > 1) {
        document.getElementById("pic_"+document.getElementById('curPic').value).style.display = 'none';
        if (gt == 0) {
            if ((parseInt(document.getElementById('curPic').value)-1) <= 0) {
                nextPic = document.getElementById('maxPic').value;
            } else {
                nextPic = parseInt(document.getElementById('curPic').value)-1;
            }
        } else {
            if ((parseInt(document.getElementById('curPic').value)+1) > document.getElementById('maxPic').value) {
                nextPic = 1;
            } else {
                nextPic = parseInt(document.getElementById('curPic').value)+1;
            }
        }
        document.getElementById('picNum').innerHTML = nextPic;
        autoResize("pic_"+nextPic);
    }
}

function KeyCheck(e,f) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (!f) {
        f = 0;
    }

    switch(KeyID) {
        case 37:
            if (document.getElementById('curPic').value != '') {
                changePic(0);
            }
        break;
        case 39:
            if (document.getElementById('curPic').value != '') {
                changePic(1);
            }
        break;
        case 27:
            if (document.getElementById('loginDiv').style.display == 'none') {
                document.getElementById('loginDiv').style.display = 'block';
            }
            opacityChange(100,0);
        break;
    }
}

function showRecaptcha(element, themeName) {
  Recaptcha.create("6LddmwoAAAAAAIJ6KamieSb8DtHteLeIgP88ipga", element, {
        theme: themeName,
        tabindex: 2,
        callback: Recaptcha.focus_response_field
  });
}