var setRotateInterval;
String.prototype.replaceAll = function(regexpression, replaceString)
{
    var value = this;
    var newValue = "";
    while(value != newValue)
    {
        newValue = value;
        value = value.replace(regexpression, replaceString);
    }
    return value;
}

function PosElement(obj)
{
    var width = obj.offsetWidth;
    var height = obj.offsetHeight;
    var left = 0;
    var top = 0;

    while(obj)
    {
        left += obj.offsetLeft - obj.scrollLeft;
        top += obj.offsetTop - obj.scrollTop;

        if(obj.style.borderTopWidth != '')
        {
            top += parseInt(obj.style.borderTopWidth);
        }
        if(obj.style.borderLeftWidth != '')
        {
            left += parseInt(obj.style.borderLeftWidth);
        }
        obj = obj.offsetParent;
    }
    top += document.body.scrollTop;
    left += document.body.scrollLeft;
    return {"left": left, "top": top, "width": width, "height": height};
}

//***** search bar ****//
var _TextBox;
var _DivBox;
var _RetFunc;
var _Title;

function txtClick(t)
{
    _TextBox = t;
    _DivBox = t.parentNode.getElementsByTagName('DIV')[0];
    CloseAllDivs();
    
    _DivBox.style.display = "";
    if(_DivBox.innerHTML != "")    SetScroll(_DivBox);
}

function txtClick2(t)
{
    t = document.forms[0].elements[t];
    _TextBox = t;
    _DivBox = t.parentNode.getElementsByTagName('DIV')[0];
    CloseAllDivs();
    
    _DivBox.style.display = "";
    if(_DivBox.innerHTML != "")    SetScroll(_DivBox);
}

function startFunction(name,title,type)
{
   
    var t = document.getElementsByName(name)[0];
    _TextBox = t;
    _DivBox = t.parentNode.getElementsByTagName('DIV')[0];
    if (type == 'mcb')EachBox(t, false,title);
    else EachInput(t,title);
}

function chkClick(t,title)
{
    EachBox(t, false,title);
}
function UpdatePrice(t,title)
{
    EachInput(t,title);
}
 
function SelectAll(t,title)
{
    EachBox(t, true,title);
}


function ReturnFunction(fn)
{
    _RetFunc = fn;
}

function SetScroll(dvb)
{
    var dv = dvb.getElementsByTagName('DIV')[0];
    
    dv.style.height = "";
    dv.style.overflowY = "auto";
    var pos = PosElement(dv);
    //   
    //if(pos.height + pos.top > document.body.clientHeight)
    //{
    //    var hgt = document.body.clientHeight - pos.top;
    //    hgt = hgt - 5;
    //    hgt = hgt < 50 ? 50 : hgt;
    //    alert(hgt);
    //    dv.style.height =  hgt + "px";
    //    dv.style.overflowY = "scroll";
        
    //    with(dv.style)
    //    {
    //        scrollbarArrowColor="0084ff";
    //        scrollbarBaseColor="99BBDD";
    //        scrollbarFaceColor="99BBDD";
    //        scrollbarTrackColor="BBDDFF";
    //    }
   // }
    
}

 function EachInput(t,title)
{
    var inText = "";
    var inID = "";
    try
    {
        var inp = _DivBox.getElementsByTagName('TABLE')[0].getElementsByTagName('INPUT');
    }
    catch(e)
    {
        return;
    }
     
    inText = inp[0].value + ' - ' + inp[1].value;
    inText = inText.replaceAll(/(<([^>]+)>)/gi, '');
    
    if(inText == ' - ')inText = title ;

    _TextBox.value = decodeURIComponent(inText);
    _TextBox.title = decodeURIComponent(inText); 
}

function EachBox(t, all,title)
{
    var inText = "";
    var inID = "";
    try
    {
        var cb = _DivBox.getElementsByTagName('TABLE')[0].getElementsByTagName('TABLE')[1].getElementsByTagName('INPUT');
    }
    catch(e)
    {
        return;
    }
    //alert(cb.length);
    
    
    for(var i = 0; i < cb.length; i ++)
    {
        if(all) cb[i].checked = t.checked;
        if(cb[i].checked)
        {
            inText += ((inText != "" ? ", " : "") + cb[i].parentNode.parentNode.getElementsByTagName('LABEL')[0].innerHTML);
            inID += ((inID != "" ? ", " : "") + cb[i].value);
        }
    }
    //alert(inText);
    inText = inText.replaceAll(/(<([^>]+)>)/gi, '');
    
    if(inText == "")inText = title ;

    _TextBox.value = decodeURIComponent(inText);
    _TextBox.title = decodeURIComponent(inText);
    if(_RetFunc != undefined) eval(_RetFunc + "('" + inID + "')");

}
            
function CloseDIV()
{
    try
    {
        _DivBox.style.display = "none";
    }
    catch(e){}
}


function CloseAllDivs()
{
    var divs = document.getElementsByTagName('DIV')
    for(var i = 0; i < divs.length; i ++)
    {
        if(divs[i].id == 'MCBDiv')
            divs[i].style.display = "none";
    }    
}


//***** END search bar ****//    



function favoris(url) {
url = "http://" + url;
if ( navigator.appName != 'Microsoft Internet Explorer' )
{ window.sidebar.addPanel("ANGLO SAXON",url,""); }
else { window.external.AddFavorite(url,"ANGLO SAXON"); } 
}

function openwindow(URL, WinName, height, width, external){

    if(height=='' || !height)
    height=screen.height/2
    height_factor = ((screen.height/100)*90)
    if(height>=height_factor)
    height=height_factor;

    if(width=='' || !width)
    width=screen.width/2
    width_factor = ((screen.width/100)*98)
    if(width>=width_factor)
    width=width_factor;

    var top=((screen.height-height)/2)-(screen.height/20);
    if(top<1) top=1;

    var left=(screen.width - width) / 2 ;
    if(left<1) left=1;

    win=window.open(URL, WinName, 'toolbar=no,location=no,directories=no,status=0,menubar=no,scrollbars=1,resizable=1,copyhistory=no,width='+width+',height='+height+',top='+top+',left='+left+'');
    if(!external)
    {
        win.resizeTo(width, height);
        win.moveTo(left, top);
    }

    win.focus();

    return win;

}

function changeImgSRC(srcImg,img,des)
{
    var desImg = 'desImg' + img;
    var img = 'big' + img;
    document.images[img].src = srcImg;
    document.images[img].width = "227";
    document.getElementById(desImg).innerHTML = des;
}

function SetLangageSite(lang){
    var url = window.location.href;
    pos = url.lastIndexOf('/');
    url = url.substring(pos);
    if (lang == 'he') var path = "http://www.anglo-saxon.co.il/";
    else var path = "http://www.anglo-saxon.com/";
    //url = "../"+lang+url;
    if (url.indexOf('index.php') && lang == 'he') url = path;
    else url = path+lang+url;
    window.location.href = url;
}

function AdsPrint(id, lang){
    var r = $.ajax({  
      type: 'POST',
      data: "id=" + id + "&lang=" + lang,    
      url: "../cutils/ajax_update_print.php",  
      async: false  
     }).responseText; 
     window.print()
     return r;  
    
}

//Rotate Image
function theRotator() {
    //Set the opacity of all images to 0
    $('div#rotator ul li').css({opacity: 0.0});
    
    //Get the first image and display it (gets set to full opacity)
    $('div#rotator ul li:first').css({opacity: 1.0});
        
    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    setRotateInterval = setInterval('rotate()',6000);
    
}


function rotate() {    
    //Get the first image
    var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));    
    
    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({opacity: 0.0})
    .addClass('show')
    .animate({opacity: 1.0}, 1000);

    //Hide the current image
    current.animate({opacity: 0.0}, 1000)
    .removeClass('show');
    
};

$(document).ready(function() {        
    //Load the slideshow
    theRotator();
});

function geturl(addr,id,lang,debug) {  
 var r = $.ajax({  
  type: 'POST',
  data: "id=" + id + "&lang=" + lang + "&debug=" + debug,  
  url: addr,  
  async: false  
 }).responseText; 
 return r;  
}  

function PrintOneAd(id, lang){ 
    $("#wrapper").html(geturl('../cutils/printads_functions.php',id,lang,0));
}

function openwindow(URL, WinName, height, width, external)
{

    if(height == '' || !height)
    {
        height = screen.height / 2;
    }
    height_factor = ((screen.height / 100) * 90);
    
    if(height == 'max')
    {
        height = screen.height;
    }
    else if(height >= height_factor)
    {
        height = height_factor;
    }
    
    if(width == '' || !width)
    {
        width = screen.width / 2;
    }
    
    width_factor = ((screen.width/100)*98);
    
    if(width == 'max')
    {
        width = screen.width;
    }
    else if(width >= width_factor)
    {
        width = width_factor;
    }
    
    var top = ((screen.height-height)/2)-(screen.height/20);
    if(top < 1)
    { 
        top = 1;
    }

    var left = (screen.width - width) / 2 ;
    if(left < 1)
    { 
        left = 1;
    }

    win=window.open(URL, WinName, 'toolbar=no,location=no,directories=no,status=0,menubar=no,scrollbars=1,resizable=1,copyhistory=no,width='+width+',height='+height+',top='+top+',left='+left+'');
    if(!external)
    {
        win.resizeTo(width, height);
        win.moveTo(left, top);
    }

    win.focus();

    return win;

}

