var AllHeaders = new Array();
var CurrentHeader;
var Timer;
var TimeInterval = 7 * 1000;

// functions

var config = {    
     over: onHover, // function = onMouseOver callback (REQUIRED)    
     timeout: 0, // number = milliseconds delay before onMouseOut    
     out: StartTimer // function = onMouseOut callback (REQUIRED)    
};

$(document).ready(function () {
    SetAllHeadersArray();
    SetDefaultHeader();
    $("#LeftHeaders li").delay(700).hoverIntent(config);  

});

function onHover() {
        StopTimer();
        $("#LeftHeaders li").each(function () {
            SetUnactiveHeader($(this).attr("id"));
        });
        var _target = $(this);
        CurrentHeader = $(_target).attr("id");
        SetActiveHeader(CurrentHeader);
}

function SetActiveHeader(target) {
    var _t = $("#" + target);
    $(_t).css({ 'background-image': 'url(http://www.datapolis.com/Portals/_default/Skins/MinimalExtropy/images/Datapolis/dnban/menu_header_on.png)',
        'background-repeat': 'no-repeat'
    });
    var _content = $(_t).find("div").text();
    var _hId;
    var _hData ="";
    for (var i = 0; i < AllHeaders.length; i++) {
        if (CurrentHeader == AllHeaders[i]) {
            _hId = i;
        };
    }; 

    switch (_hId) {
    case 0:
     _hData = Header1; 
    break;
    case 1:
    _hData = Header2;
    break;
    case 2:
    _hData = Header3;
    break;
    case 3:
    _hData = Header4;
    break;
    };
       
    $("#RightPanelDisplayArea").hide().html(_hData).fadeIn(200);
}

function SetUnactiveHeader(target) {
    var _t = $("#" + target);
    $(_t).css({ 'color': 'white',
        'background-image': 'url(http://www.datapolis.com/Portals/_default/Skins/MinimalExtropy/images/Datapolis/dnban/menu_header_off.png)',
        'background-repeat': 'no-repeat',
    });
}

function SetDefaultHeader() {
    var first = $("#first");
    CurrentHeader = $(first).attr("id");
    SetActiveHeader(CurrentHeader);
    StartTimer();
}

// TIMER FUNCTIONS

function StartTimer() {
    Timer = setTimeout("ChangeHeader()", TimeInterval);
}

function StopTimer() {
    clearTimeout(Timer);
}

function ChangeHeader() {
    StopTimer();
    $("#LeftHeaders li").each(function () {
        SetUnactiveHeader($(this).attr("id"));
    });
    var next;
    for (var i = 0; i < AllHeaders.length; i++) {
        if (CurrentHeader == AllHeaders[i]) {
            next = i;
        }
    }

    if (next == 3) {
        next = 0;
    } else {
        next++;
    }
    CurrentHeader = AllHeaders[next];
    SetActiveHeader(AllHeaders[next]);
    StartTimer();
}

// DATA FUNCTIONS

function SetAllHeadersArray() {
    $("#LeftHeaders li").each(function () {
        AllHeaders.push($(this).attr("id"));
    });
}

// sample data

       var title = new Array(5);
       var tresc = new Array(5); 
       var img = new Array(5);    
       var buttony = new Array(5);    
         $.ajax({
             type: "GET",
             url: "/xml/baner_pl.xml",
             dataType: "xml",
			 async:false,
             success: function(data) {
				var id = 0;
            	$(data).find("baner").each(function () {                                     
                   title[id] = $(this).find("tytul").text(); 
                   
                   tresc[id] = $(this).find("tresc").text();
                    
                   img[id] = $(this).find("obrazek-glowny").text(); 
                    
                   buttony[id] = $(this).find("buttony").text();
                            id = id + 1;          

                 }); 
                                
           	}
        });

 

var Header1 = "<div style='height: 225px; margin-top: -40px;'><div style='width:280px; float: left; text-align: left;'><p style='font-family:verdana; font-size:21px;' class='xtitle'>" + title[0] +"</p><br />" + tresc[0] +"</div><div style='float:right'>" + img[0] + "</div></div><div id='ctas' style='position:relative; height:28px; float:right; left: 130px; top: -17px; width: 660px;'>" + buttony[0] + "</div>";

var Header2 = "<div style='height: 225px; margin-top: -40px;''><div style='width:300px; float: left; text-align: left;'><p style='font-family:verdana; font-size:21px;'>"+ title[1] +"</p><br />" + tresc[1] + "</div><div style='float:right'>" + img[1] + "</div></div><div id='ctas' style='position:relative; height:28px; float:right; left: 130px; top: -17px; width: 660px;'>" + buttony[1] + "</div>";

var Header3 = "<div style='height: 225px; margin-top: -40px;''><div style='width:300px; float: left; text-align: left;'><p style='font-family:verdana; font-size:21px;'>"+ title[2] +"</p><br />" + tresc[2] +"</div><div style='float:right'>" + img[2] + "</div></div><div style='clear:both;'></div><div id='ctas' style='position:relative; height:28px; float:right; left: 24px; top: -17px; width: 300px;'>" + buttony[2] + "</div>";

var Header4 = "<div style='height: 225px; margin-top: -40px;'><div style='width:300px; float: left; text-align: left;'><p style='font-family:verdana; font-size:21px;'>"+ title[3] +"</p><br /><div style='line-height:150%; font-size: 14px; margin-left: 0px;'>" + tresc[3] + "</div></div><div style='float:right;position:relative;'>"+ img[3] +"</div></div></div><div id='ctas' style='position:relative; height:28px; float:right; left: 382px; top: 17px; width: 150px;'>"+ buttony[3] +"</div>"; 


   
  /**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

