function ToggleBoxes( id ){

    var Element = document.getElementById( id );
    var Text = document.getElementById( id + 'Text' );

    if(Element.style.display == 'none'){
        Element.style.display = 'block';
        Text.innerHTML = 'Ukryj';
    }else{
        Element.style.display = 'none';
        Text.innerHTML = 'Pokaż';
    }
}

function ajaxLW(obj){

    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

    x.onreadystatechange = function() {

        if (x.readyState == 4 && x.status == 200) {

            obj.success(x.responseText);
            var ForScriptRun = x.responseText.split( '<script>' ) ;
            
            if( ForScriptRun.length > 1 )
            for( i in ForScriptRun ) {
                
                if( ForScriptRun[i].split( '</script>' ).length > 1 )
                    eval( ForScriptRun[i].split( '</script>' )[0] ) ;
            }
        }
    }
    if(obj.data == undefined)
        obj.data = "";

    if(obj.type == undefined)
        obj.type = "GET";

    var param = obj.data;

    if(obj.async == undefined)
        obj.async = true;

    x.open(obj.type, obj.url, obj.async);

    if(obj.type == undefined)
        obj.data = "application/x-www-form-urlencoded";

    x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") ;
    x.setRequestHeader("Content-length", param.length) ;
    x.setRequestHeader("Connection", "close") ;
    if(x.overrideMimeType) x.overrideMimeType('text/html; charset=ISO-8859-2') ;
    x.send(param) ;
}


function ChangeOtherBuyProduct( obj, side, id, IdsArray, Url ) {

    var RightArrow = document.getElementById('OtherClientsBuyArrowRight_' + id) ;
    var LeftArrow = document.getElementById('OtherClientsBuyArrowLeft_' + id) ;

    if( LeftArrow.getAttribute('value') == 1 && side == 'left' ) return false;
    if( RightArrow.getAttribute('value') == parseInt( Math.ceil(IdsArray.length / 5) ) && side == 'right' ) return false;


    if( RightArrow.getAttribute('value') < parseInt( Math.ceil(IdsArray.length / 5) ) && side == 'right' ){

        RightArrow.setAttribute('value', parseInt( RightArrow.getAttribute('value') ) + 1 );
        LeftArrow.setAttribute('value', parseInt( LeftArrow.getAttribute('value') ) + 1 );
    }

    if( LeftArrow.getAttribute('value') > 1 && side == 'left' ){

        LeftArrow.setAttribute('value', parseInt( LeftArrow.getAttribute('value') ) - 1 );
        RightArrow.setAttribute('value', parseInt( RightArrow.getAttribute('value') ) - 1 );
    }

    var Value = obj.getAttribute('value');

    var Move = 0;

    if( IdsArray[ (Value - 1) * 5 + 4 ] == undefined ) Move++;
    if( IdsArray[ (Value - 1) * 5 + 3 ] == undefined ) Move++;
    if( IdsArray[ (Value - 1) * 5 + 2 ] == undefined ) Move++;
    if( IdsArray[ (Value - 1) * 5 + 1 ] == undefined ) Move++;
    if( IdsArray[ (Value - 1) * 5 ] == undefined ) Move++;

    var Box1 = document.getElementById("OtherClientBuyBox_1_" + id);
    var Box2 = document.getElementById("OtherClientBuyBox_2_" + id);
    var Box3 = document.getElementById("OtherClientBuyBox_3_" + id);
    var Box4 = document.getElementById("OtherClientBuyBox_4_" + id);
    var Box5 = document.getElementById("OtherClientBuyBox_5_" + id);

    Box1.innerHTML = "<img src='gfx/loader.gif'>";
    Box2.innerHTML = "<img src='gfx/loader.gif'>";
    Box3.innerHTML = "<img src='gfx/loader.gif'>";
    Box4.innerHTML = "<img src='gfx/loader.gif'>";
    Box5.innerHTML = "<img src='gfx/loader.gif'>";
       
    if(!Url) Url = 'lw/Modules/PromoSetsProductsAjax';
       
    ajaxLW({
        url: Url + ".php?id=" + IdsArray[ ( (Value - 1) * 5 ) - Move ],
        success: function(res){ //alert(res) ;
            if( res ) Box1.innerHTML = res ;
            else Box1.innerHTML = "<div style='height:10px;width:10px;'>&nbsp;</div>";
        }
    }) ;
    ajaxLW({
        url: Url + ".php?id=" + IdsArray[ ( (Value -1 ) * 5 + 1 ) - Move ],
        success: function(res){ //alert(res) ;
            if( res ) Box2.innerHTML = res ;
            else Box2.innerHTML = "<div style='height:10px;width:10px;'>&nbsp;</div>";
        }
    }) ;
    ajaxLW({
        url: Url + ".php?id=" + IdsArray[ ( (Value - 1) * 5 + 2 ) - Move ],
        success: function(res){ //alert(res) ;
            if( res ) Box3.innerHTML = res ;
            else Box3.innerHTML = "<div style='height:10px;width:10px;'>&nbsp;</div>";
        }
    }) ;
    ajaxLW({
        url: Url + ".php?id=" + IdsArray[ ( (Value - 1) * 5 + 3 ) - Move ],
        success: function(res){ //alert(res) ;
            if( res ) Box4.innerHTML = res ;
            else Box4.innerHTML = "<div style='height:10px;width:10px;'>&nbsp;</div>";
        }
    }) ;
    ajaxLW({
        url: Url + ".php?id=" + IdsArray[ ( (Value - 1) * 5 + 4 ) - Move ],
        success: function(res){ //alert(res) ;

            if( res ) Box5.innerHTML = res ;
            else Box5.innerHTML = "<div style='height:10px;width:10px;'> &nbsp; </div>";
        }
    }) ;

    if( parseInt( LeftArrow.getAttribute('value') ) == 1 ){
        LeftArrow.style.background = 'white';
        LeftArrow.style.color = 'white';
        LeftArrow.style.cursor = 'default';
    }else{
        LeftArrow.style.background = OtherClientsBg;
        LeftArrow.style.color = OtherClientsColor;
        LeftArrow.style.cursor = 'pointer';
    }

    if( parseInt( RightArrow.getAttribute('value') ) == parseInt( Math.ceil(IdsArray.length / 5) ) ){
        RightArrow.style.background = 'white';
        RightArrow.style.color = 'white';
        RightArrow.style.cursor = 'default';
    }else{
        RightArrow.style.background = OtherClientsBg;
        RightArrow.style.color = OtherClientsColor;
        RightArrow.style.cursor = 'pointer';
    }
}

function RotationBox( obj, side, id, IdsArray, Url, CountProds ) {

    var RightArrow = document.getElementById('RotationBoxArrowRight_' + id) ;
    var LeftArrow = document.getElementById('RotationBoxArrowLeft_' + id) ;

    if( LeftArrow.getAttribute('value') == 1 && side == 'left' ) return false;
    if( RightArrow.getAttribute('value') == parseInt( Math.ceil(IdsArray.length / CountProds) ) && side == 'right' ) return false;

    if( RightArrow.getAttribute('value') < parseInt( Math.ceil(IdsArray.length / CountProds) ) && side == 'right' ){

        RightArrow.setAttribute('value', parseInt( RightArrow.getAttribute('value') ) + 1 );
        LeftArrow.setAttribute('value', parseInt( LeftArrow.getAttribute('value') ) + 1 );
    }

    if( LeftArrow.getAttribute('value') > 1 && side == 'left' ){

        LeftArrow.setAttribute('value', parseInt( LeftArrow.getAttribute('value') ) - 1 );
        RightArrow.setAttribute('value', parseInt( RightArrow.getAttribute('value') ) - 1 );
    }

    var Value = obj.getAttribute('value');
    var Move = 0;

    for( var i = CountProds; i > 0; i-- ){
        
        if( IdsArray[ (Value - 1) * CountProds + ( i - 1 ) ] == undefined ) Move++;
        document.getElementById("RotationBox_" + i + "_" + id).innerHTML = "<img src='gfx/loader.gif'>"; ;
    }


    if(!Url) Url = 'lw/Modules/OtherClientsBuyAjax';
    var Ids = "" ;
    
    for( var j = CountProds; j > 0; j-- ) {
        
        Ids += IdsArray[ ( (Value - 1) * CountProds + ( j - 1 )) - Move ] + ",";
    }
    ajaxLW({
        url: Url + ".php?ids=" + Ids.substr(0, ( Ids.length - 1 ) ),
        async: true,
        success: function(res){ //alert(res) ;
            var Prods = res.split( '^^^^' ) ;
                
            for( var i = CountProds; i > 0; i--  ) {
                    
                if( res ) document.getElementById("RotationBox_" + i + "_" + id).innerHTML = Prods[i - 1] ;
                else document.getElementById("RotationBox_" + i + "_" + id).innerHTML = "<div style='height:10px;width:10px;'>&nbsp;</div>";
            }
        }
    }) ;

    if( parseInt( LeftArrow.getAttribute('value') ) == 1 ){
        LeftArrow.style.background = 'white';
        LeftArrow.style.color = 'white';
        LeftArrow.style.cursor = 'default';
    }else{
        LeftArrow.style.background = RotationBoxBg;
        LeftArrow.style.color = RotationBoxColor;
        LeftArrow.style.cursor = 'pointer';
    }

    if( parseInt( RightArrow.getAttribute('value') ) == parseInt( Math.ceil(IdsArray.length / 6) ) ){
        RightArrow.style.background = 'white';
        RightArrow.style.color = 'white';
        RightArrow.style.cursor = 'default';
    }else{
        RightArrow.style.background = RotationBoxBg;
        RightArrow.style.color = RotationBoxColor;
        RightArrow.style.cursor = 'pointer';
    }
}

function getElementsByClassName(classname, node)  {
    
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);

    return a;
}

function OnReady( Function ){ 

    var body = document.getElementsByTagName('body')[0];
    
    if( body && body.readyState == 'loaded' ){
        setTimeout( Function + "()", 1 );
    } else if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", Function, false);
    }else {  
        window.onload = Function;
    }
}

var RunOnLoad = new Object() ;
function EvalOnReady(  ){

    for( i in RunOnLoad ) {
        if( RunOnLoad[i].substr( (RunOnLoad[i].length - 1 ), 1) != ')' )
            eval( RunOnLoad[i] + '()' ) ;
        else
            eval( RunOnLoad[i] ) ;
    }
}

OnReady( EvalOnReady ) ;

function IsIe(){
    var IE;
    var v = false;
    var n;
    try{
        n = navigator.appVersion;
        IE = ( n.indexOf('MSIE') != -1 )
    }catch(e){}

    if( IE ) v = n.substr( n.indexOf('MSIE') + 5, 1 ) ;

    return parseInt(v);
}


function SetOpacity( Obj, Opacity ){ 
    if( IsIe() ) Obj.style.filter = 'alpha(opacity=' + (Opacity * 100) + ')';
    else Obj.style.opacity = Opacity;
}

function RoundPrice( DoubleNumber ){

    var num = "" + ( ( Math.ceil( DoubleNumber * 100 ) ) / 100 ) ;
    var i=num.indexOf(".");

    if ( i<0 ) num+=".00";
    else {
    num=num.substring(0,i) + "." + num.substring(i + 1);
    i=(num.length - i) - 1;
    if ( i==0 ) num+="00";
    else if ( i==1 ) num+="0";
    else if ( i>2 ) num=num.substring(0,i + 3);
    }
    return num ;
}

function Trim( text ){

    return text.replace( /^\s+|\s+$/gi, '' ) ;
}

var SubMenuHover = 0 ;

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
        
    return curtop;
}

function AdjustSubMenuPos(e) {

    e.SubMenuHover = 1 ;
    var SubMenu = e.getElementsByTagName("ul");
    if( SubMenu.length < 1 ) {
        return false ;
    }
    var SumMenuTopMargin = SubMenu[0].style.marginTop.split('px')[0]*1;

    if( SumMenuTopMargin >= 0 ) {

        SumMenuTopMargin = e.clientHeight*-1 ;
    }
    else if( SumMenuTopMargin < e.clientHeight*-1 ) {

        return false ;
    }

    var MenuOverflow = findPosY(SubMenu[0])+SubMenu[0].clientHeight-document.body.scrollTop-document.body.clientHeight ;

    if( MenuOverflow > 0 ){

        SubMenu[0].style.marginTop = SumMenuTopMargin-MenuOverflow;
    // document.getElementById("TestLog").innerHTML+=SumMenuTopMargin+'-'+MenuOverflow+'Adj ';
    }
}

function ResetSubMenuPos(e) {

    //alert(1);
    if( e.SubMenuHover == 1 ) {
        return false ;
    }
    //alert(2);

    var SubMenu = e.getElementsByTagName("ul");

    if( SubMenu.length > 0 ) {

        SubMenu[0].style.marginTop = e.clientHeight*-1 ;
    }
}

function sfHoverdsa() {

    var sfEls = document.getElementById("nav").getElementsByTagName("LI");



    for (var i=0; i<sfEls.length; i++) {

        sfEls[i].onmouseover=function() {

            var Id = this.id.split('_')[1] ;
            var Li = this;

            if( !this.getAttribute('isfull') ){

                this.setAttribute('isfull', true) ;



            }else{

                Li.className += " sfhover" ;
                AdjustSubMenuPos( Li );
            }

        }

        sfEls[i].onmouseout=function() {

            this.className = this.className.split( " " )[0] ;
            e = this ;
            e.SubMenuHover = 0 ;
            setTimeout(function(){
                ResetSubMenuPos(e)
            },500);
        }
    }
}

function sfHover( Cats ) {

    var sfEls = document.getElementById("nav").getElementsByTagName("LI");

    for (var i=0; i<sfEls.length; i++) {

        var Id = sfEls[i].id.split('_')[1] ;

        if( Cats[0][Id] != undefined ){

            var SubCat = "<ul style='z-index:2;'>" ;

            for( j in Cats[0][Id] ){

                SubCat += '<li class="cat_lev_1"><a class="link_lev_1" href="' + Cats[0][Id][j].Url + '">' + Cats[0][Id][j].Name + '</a>' ;

                if( Cats[0][j] != undefined ){

                    SubCat += "<ul>" ;
                    for( k in Cats[0][j] ){

                        SubCat += '<li class="cat_lev_2"><a class="link_lev_2" href="' + Cats[0][j][k].Url + '">' + Cats[0][j][k].Name + '</a>' ;
                    }
                    SubCat += "</ul>" ;
                }

                SubCat += '</li>' ;
            }

            SubCat += "</ul>" ;
            sfEls[i].innerHTML += SubCat ;
        }

        sfEls[i].onmouseover=function() {

            this.className+=" sfhover" ;
            AdjustSubMenuPos(this);
        }

        sfEls[i].onmouseout=function() {

            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
            e = this ;
            e.SubMenuHover = 0 ;
            setTimeout(function(){
                ResetSubMenuPos(e)
            },500);
        }
    }
}

function GetCats(){

    if( document.getElementById("nav") == null ) return false;

    ajaxLW({
        url: "lw/Modules/GetCats.php",
        success: function( res ){
            if( res ) ;
            sfHover( eval( res ) ) ;
        }
    }) ;
}

if( window.location.href.split('/')[3].substr(0,14) != 'product_bought' ) {
    
    RunOnLoad['GetCats'] = 'GetCats' ;
}

RunOnLoad['TrackCoordinates'] = 'TrackCoordinates' ;

function TrackCoordinates(){

    if( document.getElementsByTagName('body')[0].addEventListener ) {

        document.getElementsByTagName('body')[0].addEventListener( 'click',
            function( event ) {

                SetCookie( 'TrackCoordinates', GetScrollXY( 1, event )[2] + ":" + GetScrollXY( 1, event )[3] + ";", ( 180 ), '/' ) ;
            }
            , true
            ) ;
    } else {

        document.getElementsByTagName('body')[0].attachEvent( 'click',
            function( event ) {

                SetCookie( 'TrackCoordinates', GetScrollXY( 1, event )[2] + ":" + GetScrollXY( 1, event )[3] + ";", ( 180 ), '/' ) ;
            }
            ) ;
    }

}

function ModerateProductCat( Moderator, ProductId ){

    ajaxLW({
        type: "POST",
        data: "ProductId="+ProductId+"&Moderator="+Moderator,
        url: "/lw/Functions/ModerateProductCat.php",
        success: function(text) {

            alert( text ) ;
        }
    });
}

function GetScrollXY( MousePos, e ) {

    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }

    if( MousePos ) {

        var posx = 0;
        var posy = 0;
        e = e || window.event ;

        if (e.pageX || e.pageY) {

            posx = e.pageX;
            posy = e.pageY;
        } else if (e.clientX || e.clientY) 	{

            posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft ;
            posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop ;
        }

        posx = posx - document.getElementsByTagName('table')[0].offsetLeft ;

        return [ scrOfX, scrOfY, posx, posy ] ;
    }

    return [ scrOfX, scrOfY ] ;
}

function SetCookie( name, value, expires, path, domain, secure ) {

    var today = new Date();
    today.setTime( today.getTime() );

    if( getCookie( name ) ) value = getCookie( name ) + value ;
    //    alert(getCookie( name ) ) ;
    if ( expires ) expires = expires * 1000 ;

    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" ) ;
}

function getCookie(c_name){

    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++) {

        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name) {

            return unescape(y);
        }
    }
}
function Cloud( Id, Obj, e ){
    
    var posx = 0;
    var posy = 0;
    e = e || window.event ;

    if (e.pageX || e.pageY) {

        posx = e.pageX;
        posy = e.pageY;
    } else if (e.clientX || e.clientY) 	{

        posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft ;
        posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop ;
    }
    document.getElementById( 'Cloud_' + Id ).style.display = 'block';
    
    posx = posx - parseInt( document.getElementById( 'Cloud_' + Id ).style.width ) / 2 ;
    posy = ( posy - parseInt( document.getElementById( 'Cloud_' + Id ).offsetHeight  ) ) - 12 ;
//    alert( posx + " , " + posy) ;
    document.getElementById( 'Cloud_' + Id ).style.left = posx + 'px' ;
    document.getElementById( 'Cloud_' + Id ).style.top = posy + 'px';
}

function CloudHide( Id ) {
    if(document.getElementById( 'Cloud_' + Id ) != undefined )
    document.getElementById( 'Cloud_' + Id ).style.display = 'none';
} 
function MenuHover( obj, active ){
        
    if( active ) {
        obj.style.backgroundColor='#ce5454';
        obj.style.backgroundImage='url(/gfx/menu_arrowa.png)';
    } else {
        obj.style.backgroundColor='';
        obj.style.backgroundImage='';
    }
}
var ToggleCat = 'show' ;
    
function ToggleCats( Lev ) {
        
    var Childs ;
        
    if( ToggleCat == 'show' ) {
            
        if( Lev == 1 ) {
                
            var Childs = getElementsByClassName('MenuSubLev', document.getElementById('ListMenu'));
            for( var i in Childs ) {

                Childs[i].style.display = 'none' ;
            }
        }
            
        Childs = getElementsByClassName('MenuSub2Lev', document.getElementById('ListMenu'));
        for( var i in Childs ) {
                
            Childs[i].style.display = 'none' ;
        }
            
        ToggleCat = 'hide' ;
    } else if( ToggleCat == 'hide' ) {
            
        if( Lev == 1 ) {
                
            var Childs = getElementsByClassName('MenuSubLev', document.getElementById('ListMenu'));
            for( var i in Childs ) {

                Childs[i].style.display = 'block' ;
            }
        }
            
        Childs = getElementsByClassName('MenuSub2Lev', document.getElementById('ListMenu'));
        for( var i in Childs ) {
                
            Childs[i].style.display = 'block' ;
        }
            
        ToggleCat = 'show' ;
    }
}

var CloudArray = new Array() ;
var CloudInterval = false ;

function MoveCloudsToRightPlace(){
    
    if( CloudInterval ) clearInterval( CloudInterval ) ;
    CloudInterval = setInterval( 'MoveCloudsToRightPlace()', 2000 ) ;
    var CloudHTML = "" ;
    
    for( i in CloudArray ) {
        
        CloudHTML = document.getElementById( CloudArray[i] ).parentNode.innerHTML ;
        document.getElementById( CloudArray[i] ).parentNode.removeChild( document.getElementById( CloudArray[i] )) ;
        
        if( document.getElementById( CloudArray[i] ) == null )
            document.getElementById('CloudsKontener').innerHTML += CloudHTML ;
        
        CloudHTML = "" ;
    }
    CloudArray = new Array() ;
}

$(document).ready(function() {
    MoveCloudsToRightPlace() ;
});




