/*
	Processing Library
	@Author: EVA Software Ltd.
*/

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
var isIE = (browser=="Microsoft Internet Explorer");
var isIE6 = (isIE && version >= 6 && version < 7);
var images = Array();
var imgW = Array();
var imgH = Array();
var oldOnLoad = window.onload;
window.onload = pageOnLoad;

function pageOnLoad() {
	onLoadImages();

	onLoadOpacitySlider();

        onLoadSliderOpacity();

        onLoadSliderButtons();

	onLoadDateBanner();
	
	if (oldOnLoad != null) {
	    var xOnLoad = oldOnLoad;
	    oldOnLoad = null;
	    xOnLoad();
	}
}

function onLoadSliderButtons(){
    var btnleft = document.getElementById('btnleft');
    var btnright = document.getElementById('btnright');
    if (btnleft !== undefined && btnleft !== null) {
        btnleft.onclick = btnMoveLeft;
    }
    if (btnright !== undefined && btnright !== null) {
        btnright.onclick = btnMoveRight;
    }
    
}


var currentLeft = -100;

function btnMoveLeft(){
    moveDiv('bannerList', 'left');
}

function btnMoveRight(){
    moveDiv('bannerList', 'right');
}

function moveDiv(divId, direct) {
    var min = -150;
    var max = 30;
    var step = 50;
    switch (direct) {
        case "left":
            //  alert(currentLeft);
            if (currentLeft + step >= min)
                currentLeft = currentLeft - step;
            break;
        case "right":
            //    alert(currentLeft);
            if (currentLeft + step <= max)
                currentLeft = currentLeft + step;
            break;
    }
  //  alert(currentLeft.toString());
    document.getElementById(divId).style.left = currentLeft + "px";
}

function onLoadSliderOpacity(){
    var soImages = document.getElementsByTagName('img');
    if(soImages !== undefined && soImages !== null && soImages.length > 0) {
        fadeOpacity.addRule('SliderOpacity', 1, 0.3 , 30);
        for (var i=0; i < soImages.length; i++) {
            var cName = soImages[i].className.split(" ");
            if (cName.length > 0) {
                for (var j=0; j<cName.length;j++){
                    if (cName[j] == 'jsopacity') {
                        soImages[i].onmouseover = doOpacity;
                        soImages[i].onmouseout = backOpacity;
                    }
                }
            }
        }
    }
}

function doOpacity() {
    var soImages = document.getElementsByTagName('img');
    if(soImages !== undefined && soImages !== null && soImages.length > 0) {
        for (var i=0; i < soImages.length; i++) {
            var cName = soImages[i].className.split(" ");
            if (cName.length > 0) {
                for (var j=0; j<cName.length;j++){
                    if (cName[j] == 'jsopacity') {
                        if (soImages[i].id != this.id) {
                            fadeOpacity(soImages[i].id, 'SliderOpacity');
                        }
                    }
                }
            }
        }
    }
}

function backOpacity() {
    var soImages = document.getElementsByTagName('img');
    if(soImages !== undefined && soImages !== null && soImages.length > 0) {
        for (var i=0; i < soImages.length; i++) {
            var cName = soImages[i].className.split(" ");
            if (cName.length > 0) {
                for (var j=0; j<cName.length;j++){
                    if (cName[j] == 'jsopacity') {
                        if (soImages[i].id != this.id) {
                            fadeOpacity.back(soImages[i].id);
                        }
                    }
                }
            }
        }
    }
}


function onLoadDateBanner() {
	var smart_banner = document.getElementById('smart_banner');
	if (smart_banner !== undefined && smart_banner !== null ) {
		smart_banner.src = 'http://date.styler.co.il/iframe.php?rows=1&cols=1&lang=heb&region=100&genre=1&look_genre=2&size=1&age_from=18&age_to=36&show_info=1&new_window=1&text_color=000000&width=100&width_px=0&link_color=000000&link_no_follow=1&bg_color=ffffff&bord_color=000000&rol_color=000000&photo_bord_color=000000&photo_bord_width=0&table_bord_width=0&win_encoding=&modest=0&hot=1&adult=0&static_online=&no_alt=&tid=124';
	}
}

var hpImages = new Array();
var hpCurr = 0;
var hpStatus = false;
var hpOnload = false;
var hpImgNew = null;
var hpImgOld = null;
var hpDivText = null;
var hpTimeOut = null;

function onLoadOpacitySlider() {
    if (hpTimeOut == null) {
        hpDivText = (isIE)? getElementsByName_iefix('div', 'divText') : document.getElementsByName('divText');
        if(hpDivText !== undefined && hpDivText !== null && hpDivText.length > 0) {
            for (var i=0; i < hpDivText.length; i++) {
                hpImages[i] = hpDivText[i].getElementsByTagName('input')[0].value;
                if (hpDivText[i].className == 'divHomePageTopArticle') {
                    hpCurr = i;
                }
            }

            fadeOpacity.addRule('homepageSlideShow', 0, 1, 50);

            hpImgNew = document.getElementById('bigImageHidden');
            hpImgOld = document.getElementById('bigImage');
            hpImgOld.src = '';
            hpImgOld.src = hpImages[0];

            hpImgNew.onload = showImageEffect;
            var lCurr = ( (hpCurr+1) >= hpImages.length ) ? 0 : (hpCurr+1);
            hpTimeOut = setTimeout('switchImage(hpImages['+lCurr+']);', 9000);
            //setTimeout('switchImage(hpImages['+hpCurr+']);', 7000);
        }
    }
}

function switchImage(newImage) {
	if (!hpOnload) {
		hpOnload = true;
		if (hpStatus) {
			hpImgOld.src = newImage;
		} else {
		    hpImgOld.onload = showImageEffect;
			hpImgNew.src = newImage;
		}
	}
}

function showImageEffect() {
	if (hpStatus) {
		fadeOpacity.back(hpImgNew.id);
	} else {
		fadeOpacity(hpImgNew.id, 'homepageSlideShow');
	}
	hpStatus = !hpStatus;
	var oldCurr = hpCurr;
	hpCurr++;
	hpCurr = ( hpCurr >= hpImages.length ) ? 0 : hpCurr ;

	hpDivText[oldCurr].className='divHomePageTopArticleVisited' ;
	hpDivText[hpCurr].className='divHomePageTopArticle';
	var hrefs = hpDivText[hpCurr].getElementsByTagName('a');
	if (hrefs.length>0) {
		for (var key=0; key < hrefs.length; key++) {
			if (hrefs[key].className == 'HomPageLinkBigTop') {
				document.getElementById('uu').getElementsByTagName('a')[0].href = ( isIE6 ? escape(hrefs[key].href.replace(/http:\/\/styler.co.il/gi, '')) : hrefs[key].href );
				document.getElementById('uu').getElementsByTagName('a')[0].title = hrefs[key].title;
				hpImgNew.alt = hpImgNew.title = hrefs[key].title;
				break;
			}
		}
	}

	var lCurr = ( (hpCurr+1) >= hpImages.length ) ? 0 : (hpCurr+1);
	setTimeout('switchImage(hpImages['+lCurr+']);', 8000);
	//setTimeout('switchImage(hpImages['+hpCurr+']);', 7000);
	hpOnload = false;
}

function onLoadImages(){
    var count = images.length;
    if (count>0) {
        for (var i=0; i<count; i++){
		    CalcImageResize(images[i], imgW[i], imgH[i]);
	    }
	}
}

function collectImages(imgObj, maxWidth, maxHeight){
    var index = images.length;
    images[index] = imgObj;
    imgW[index] = maxWidth;
    imgH[index] = maxHeight;
    imgObj.style.visibility = "hidden";
}

function showImages(imgObj, maxWidth, maxHeight) {
	if (isIE) {
		var imgNew = document.createElement('img');
		imgNew.onload = function() {
			CalcImageResize(this, maxWidth, maxHeight);
			imgObj.style.height = this.height;
			imgObj.style.width = this.width;
			imgObj.style.visibility = 'visible';
//			imgObj.style.vertical-align = 'middle';
//			imgObj.style.text-align = 'center';
		}
		imgNew.src = imgObj.src;
		
	} else {
		CalcImageResize(imgObj, maxWidth, maxHeight);
	}
}

function CalcImageResize(imgObj, maxWidth, maxHeight){
	var ret = false;
	var t = false;
	var height = (imgObj.height == 0) ? maxHeight : imgObj.height;
	var width = (imgObj.width == 0) ? maxWidth : imgObj.width;
	var sHeight = height;
	var sWidth = width;
	var orgHeight = height;
	if(width > maxWidth && height > maxHeight) {
		var t1 = maxWidth > 0 ? width / maxWidth : 0;
	    var t2 = maxHeight > 0 ? height / maxHeight : 0;
	    t = Math.max(t1, t2);
	} else if(width > maxWidth) {
	    t = width / maxWidth;
	} else if(height > maxHeight) {
	    t = height / maxHeight;
	}
	if(t) {
	    sWidth = Math.floor(width / t);
	    if (height == orgHeight)
	        sHeight = Math.floor(height / t);
	    ret = true;
	}
	imgObj.width = sWidth;
	imgObj.height = sHeight;
	imgObj.style.width = sWidth;
	imgObj.style.height = sHeight;
	imgObj.style.visibility = "visible";
	return ret;
}

function getXmlNodeByName(xmlObj, nodeName) {
	return xmlObj.getElementsByTagName(nodeName);
}

function getElementsByName_iefix(tag, name)
{
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     if(elem !== undefined && elem !== null && elem.length > 0) {
         var iarr = 0;
         for(var i=0; i<elem.length; i++) {
              att = elem[i].getAttribute("name");
              if(att == name) {
                   arr[iarr] = elem[i];
                   iarr++;
              }
         }
     }
     return arr;
};

