/*  Copyright (c) 2001-2007 FOX Software Sp. z o.o. author: marcin.lisok@fox.com.pl */
var op = /opera/i.test(navigator.userAgent);
var ie = !op && /msie/i.test(navigator.userAgent);	     
var mz = !op && /mozilla\/5/i.test(navigator.userAgent); 

// funkcja przełącza TabSheet
function changeTabSheet() {
  if ((!this) || (this.className == 'front')) return false;
  var ul = this.parentNode;
  var li = ul.getElementsByTagName('LI');
  for (var i=0; i<li.length; i++ ) {
    if (li[i].id == 'googlemaptrigg') {
        googlemapsiframe(document.getElementById('page'+i));
    }
    li[i].className = (li[i]==this) ? li[i].className.replace('back','front') : li[i].className.replace('front','back');
    var pg = document.getElementById('page'+i);
    if (pg) {
      pg.className = (li[i]==this)? pg.className.replace('back','front') : pg.className.replace('front','back');
      // for (var k = 0, l=pg.childNodes.length; k<l; k++) {
        // if (pg.childNodes[k].id == 'googlemapholder') {
            
            // break;
        // }
      // }
    }
  }
  return false;
}

function googlemapsiframe(obj) {
    var oIframe = obj.getElementsByTagName('iframe')[0];
    if (oIframe && oIframe.nowyiframe) {
        return;
    } else if (oIframe) {
        var newIframe = document.createElement('iframe');
        //for (var x in oIframe) newIframe[x] = oIframe[x];
        newIframe.src = oIframe.src;
        if (parseInt(oIframe.width) > 570) {
            newIframe.width = 570;
        } else {
            newIframe.width = oIframe.width;        
        }
        newIframe.height = oIframe.height;        
        newIframe.nowyiframe = 1;
        oIframe.parentNode.replaceChild(newIframe, oIframe);
    }
}

function activateTabSheet() {
    //alert(1);
  var ul = document.getElementById('tabs2');
  if (!ul) { return; }
    var li = ul.getElementsByTagName('LI');
    for( j=0; j<li.length; j++ )
          li[j].onclick = changeTabSheet;
}

function setTabSheet (aTab) {
    var ul = document.getElementsByTagName('UL');
    for( i=0; i<ul.length; i++ ) {
        if (ul[i].className != 'tabs') continue;
        var li = ul[i].getElementsByTagName('LI');
        for( j=0; j<li.length; j++ ) {
            if (li[j].className.indexOf("front") != -1) {
                li[j].className = li[j].className.replace('front','back');
                var pg = document.getElementById('page'+j);
                if (pg) {
                    pg.className = pg.className.replace('front','back');                
                }
                li[aTab].className = li[aTab].className.replace('back','front');
                var pg2 = document.getElementById('page'+aTab);
                if (pg2) {
                    pg2.className = pg2.className.replace('back','front');                
                }
            }
        }
    }
}

function setTabSheet2 (aTab) {
     var infop = document.getElementById('info-panel');
     var div = infop.getElementsByTagName('DIV');
     pages = new Array();
     for( i=0; i<div.length; i++ ) {
        pages[i] = document.getElementById('page' + i);
     }     
     for( j=0; j<pages.length; j++ ) {
        if ( pages[j] != null ) {
            if (pages[j].className.indexOf("front") != -1) {
                    pages[j].className = pages[j].className.replace('front','back');                    
            }
        }
     }
     pages[aTab].className = pages[aTab].className.replace('back','front');
}

function findParentElement( node, tag ) {
  var res = null;
  tag = tag.toUpperCase();
  while (node && node.tagName != tag) {
    node = node.parentNode;
  } 
  if (node && node.tagName == tag) res = node;
  return res;
}
function activateDetailView() {
  var arr = document.getElementsByTagName('A');
  for (var i=0; i<arr.length; i++) {
    if (arr[i].className.indexOf('detail') == -1) continue;
    arr[i].onclick = function () {
      var tr = findParentElement( this, 'TR' );
      if (tr) {
        tr = tr.nextSibling;
        while (tr.nodeType != 1) tr = tr.nextSibling;
        if (!tr) return true;
        var td = tr.cells[0];
        if (td.style.display != 'none' && td.style.display != '' ) {
          td.style.display = 'none';
          if ( ie ) tr.style.display = 'none';
        } else {
          if ( ie ) {
            td.style.display = 'block';
            tr.style.display = '';
          } else {
            td.style.display = 'table-cell';
          }
        }
      }
      return false;
    }
  }
}

function setSize() {
	var sb1 = document.getElementById('sidebar1');
	var sb2 = document.getElementById('sidebar2');
	var mn  = document.getElementById('main');
	var cnt  = document.getElementById('content');
	var shr  = document.getElementById('subheader');
	if (mn && sb2) {
		var biggerSize = Math.max(sb1.offsetHeight,sb2.offsetHeight);
		var biggest = Math.max(biggerSize,mn.offsetHeight);
		document.getElementById('sidebar1').style.height = biggest + 'px';
		document.getElementById('sidebar2').style.height = biggest + 'px';
		document.getElementById('main').style.height = biggest + 'px';
	}
	if (cnt && shr) {
		var left = cnt.offsetHeight+shr.offsetHeight+4;
		var biggest = Math.max(sb1.offsetHeight,left);
		document.getElementById('sidebar1').style.height = biggest + 10 + 'px';
		document.getElementById('content').style.height = biggest - shr.offsetHeight - 4 + 'px';		
	}
	else {
		return false;
	}
}



function SelectNavigation(navId) {
    this.navId = navId;
    this.targets = new Array;
    this.defaultActive = new String;
}
SelectNavigation.prototype.init = function () {
    var nav = document.getElementById(this.navId);
    if (!nav) return;
    var obj = this;
    var anchors = nav.getElementsByTagName("option");
    var targets = new Array;
    this.defaultActive = anchors[0].value.substring(1);
    
    // dodanie eventu do linkow i dodanie do tablicy id'kow
    for (var i = 0; i < anchors.length; i++) {
        if (anchors[i].value) {
            
            this.targets.push(anchors[i].value);

            nav.onchange = function(e) {
                obj._run(this.value);
            }
        }
    }
}
SelectNavigation.prototype._run = function (hash) {
    // pokazywanie i ukrywanie tresci
    for (var i=0; i<this.targets.length; i++) {
        // jezeli hash klikniety lub podany w url'u jest rowny jednemu z id'kow
        if (hash == this.targets[i]) {
            // jezeli istniej docelowy element - to go pokazuje
            if (document.getElementById(this.targets[i]))
                document.getElementById(this.targets[i]).style.display = 'block';
            // jezeli nie ma docelowego elementu - pokazuje domyslny
            else
                document.getElementById(this.defaultActive).style.display = 'block';
        }
        // ukrywa wszystkie pozostale
        else {
            if (document.getElementById(this.targets[i])) 
                document.getElementById(this.targets[i]).style.display = 'none';
        }
    }
}
/* end class */


function TabNavigation_old(sNavId, sActiveClassName, sDisplayNoneClassName, bAllowAdressNav, aTargets) {
    this.sNavId = sNavId;
    this.sActiveClassName = sActiveClassName;
    this.sDisplayNoneClassName = sDisplayNoneClassName;
    this.bAllowAdressNav = bAllowAdressNav || false;
    this.aTargets = aTargets || new Array();
    this.sDefaultTarget = new String;
}

TabNavigation_old.prototype.init = function () {
    var nav = document.getElementById(this.sNavId); if (!nav) return;
    var aAnchors = nav.getElementsByTagName("a");
    var obj = this;
    var tmp = this.aTargets.length;
    var aAnchorsTmp = new Array();

    for (var i = 0; i < aAnchors.length; i++) {
        if (aAnchors[i].hash && aAnchors[i].hash != '#') {
            // ustawia sDefaultTarget sTarget, gdy jakis link ma nadana klase sActiveClassName
            if (this.sActiveClassName && this._isClass(aAnchors[i], this.sActiveClassName) == true)
                this.sDefaultTarget = aAnchors[i].hash.substring(1);
            
            // tworzona jest tablica sTargetow na podstawie hasha w linkach, jezeli nie zrobiono tego recznie
            if (tmp == 0)
                this.aTargets.push(aAnchors[i].hash.substring(1));
            
            // tworzony jest sTarget, gdy jest hash w url'u
            if (window.location.hash && window.location.hash == aAnchors[i].hash) 
                var sTarget = window.location.hash.substring(1);
            
            aAnchors[i].onclick = function(e) {
                obj._run(this.hash.substring(1), aAnchors, true, e);
            }
            aAnchorsTmp.push(aAnchors[i].hash.substring(1)); 
        }
    }
    
    if (this.sDefaultTarget == '') 
        this.sDefaultTarget = aAnchorsTmp[0];

    //jezeli sTarget dostajemy z url'a
    if (sTarget && this.bAllowAdressNav == true)
        this._run(sTarget);
    // jezeli sTarget dostajemy na podstawie domyslnego linka
    else if (this.sDefaultTarget != '')
        this._run(this.sDefaultTarget, aAnchors);
};


TabNavigation_old.prototype._run = function (sTarget, aAnchors, click, e) {
    // return false dla klikniecia
    if (click) {
        if (!e) e = window.event;
        if (e.preventDefault) 
            e.preventDefault(); 
        else 
            e.returnValue = false;         
    }
    
    // nadawanie linkom klas
    for (var i = 0; i < aAnchors.length; i++) {
        if (aAnchors[i].hash == "#" + sTarget) {
            this._addClass(aAnchors[i], this.sActiveClassName);
        } 
        else {
            this._removeClass(aAnchors[i], this.sActiveClassName);
        }
    }
    
    // pokazywanie i ukrywanie tresci
    for (var i=0; i<this.aTargets.length; i++) {
        // jezeli hash klikniety lub podany w url'u jest rowny jednemu z id'kow
        if (sTarget == this.aTargets[i]) {
            // jezeli istniej docelowy element - to go pokazuje
            if (document.getElementById(this.aTargets[i]))
                this._show(document.getElementById(this.aTargets[i]), this.sDisplayNoneClassName);
            // jezeli nie ma docelowego elementu - pokazuje domyslny
            else if (this.sDefaultTarget)
                this._show(document.getElementById(this.sDefaultTarget), this.sDisplayNoneClassName);
        }
        // ukrywa wszystkie pozostale
        else {
            if (document.getElementById(this.aTargets[i])) 
                this._hide(document.getElementById(this.aTargets[i]), this.sDisplayNoneClassName);
        }
    }
    
    // zmienianie adresu
    if (this.bAllowAdressNav == true)
        window.location.replace("#" + sTarget);
};


TabNavigation_old.prototype._show = function (el, cl) {
    if (!cl) {
        el.style.display = "block";
    } else {
	    this._removeClass(el, cl);
    }       
};

TabNavigation_old.prototype._hide = function (el, cl) {
    if (!cl) {
        el.style.display = "none";
    } else {
	    this._addClass(el, cl);
    }       
};

TabNavigation_old.prototype._addClass = function (el, cl) {
    if (!el.className.match(new RegExp("" + cl + "", "i")))
        el.className += (el.className ? " " : "") + cl;
};

TabNavigation_old.prototype._isClass = function (el, cl) {
    if (el.className.match(new RegExp("" + cl + "", "i")))
        return true;
    else
        return false;
};

TabNavigation_old.prototype._removeClass = function (el, cl) {
    el.className = el.className.replace(new RegExp(" \\b" + cl + "\\b|\\b" + cl + "\\b ?", "gi"), "");
};




/* funckcje ze starego pliku ajax.js */


/*
* dodaje do koszyka oferte
*/

function addToBasket( offer, nodeCheckbox ) {

	if ( nodeCheckbox.checked )
	{
		strOperation = "add";
		showWindow( 'BasketNote', nodeCheckbox, 'Dodano do schowka', 800  );
	}
	else
	{
		strOperation = "delete";
		showWindow( 'BasketNote', nodeCheckbox, 'Usunięto ze schowka', 800  );
	}
    //console.info("http://" + location.hostname + "basket.php");
	advAJAX.get({
    	  url : "http://" + location.hostname + "/basket.php",
    		//onSuccess : changeBasketCount, // dynamiczna zmiana liczby w koszyku
    	  parameters : {
    	    "offer" : offer,
    	  	"oper" : strOperation  
    	  },
          onSuccess : function(obj) { return false; },
          onError : function(obj) { 
            if(console != undefined) {
                console.info("Error: " + obj.status); 
            }
            else {
                return false;
            }
        }
	}
    
	);
}

function changeBasketCount( obj ) 
{
	intValue = parseInt(obj.responseText);
	
	objBasketCount = document.getElementById('BasketCount');
	objBasketContener = document.getElementById('BasketContener');
	if ( intValue > 0 )
	{
		objBasketContener.className = 'menu-koszyk';
		objBasketCount.replaceChild( document.createTextNode( intValue ), objBasketCount.firstChild );
	}
	else
	{
		objBasketContener.className = 'menu-koszyk-brak';
		objBasketCount.replaceChild( document.createTextNode( 'brak' ), objBasketCount.firstChild );
	}
};

objWindowTime = new Array();
function showWindow( strWindowID, nodeCheckbox, strNote, intTime )
{
	nodeWindow = document.getElementById( strWindowID );
	if ( objWindowTime[strWindowID] )
	{
		clearTimeout( objWindowTime[strWindowID] ); 
	} 	
	nodeCheckbox.parentNode.appendChild( nodeWindow );
	nodeWindow.style.display = 'block';
	nodeWindow.replaceChild( document.createTextNode( strNote ), nodeWindow.firstChild );
	objWindowTime[strWindowID] = setTimeout( 'hideWindow( "' + strWindowID + '" )', intTime );
}

function hideWindow( strWindowID )
{
	nodeWindow = document.getElementById( strWindowID );	
	nodeWindow = document.getElementById( 'BasketNote' );
	nodeWindow.style.display = 'none';
}

/* end funkcje ze starego pliku ajax.js */





var initialize = new Object;
initialize.init = function() {
    
    // stworzenie obiektu nawigacji dla poradnika
    var placeSelect = 'place-select';
    if (document.getElementById(placeSelect) != 'undefined') {
        var selectNavigation = new SelectNavigation(placeSelect);
        selectNavigation.init();
    }
    // stworzenie obiektu nawigacji dla poradnika
    var tripTips = 'tripTips';
    if (document.getElementById(tripTips) != 'undefined') {
        var tabNavigation_old = new TabNavigation_old(tripTips, 'active', 'back');
        tabNavigation_old.init();
    }
    
}


window.onload = function() {
  activateTabSheet();
  activateDetailView();
  makeHover.init();
  initialize.init();
}
