/* --- Tabs --- */
Tabs = {
    // Changes to the tab with the specified ID.
    GoTo: function (contentId, skipReplace) {
        // This variable will be true if a tab for the specified
        // content ID was found.
        var foundTab = false;

        // Get the TOC element.
        var toc = $("tab_toc");
        
        if (toc) {
        	var lis = $$("#tab_toc li");
        	lis.each(function(li) {
        		var anc = li.getElementsByTagName("a")[0];
                if (anc && anc.hash == "#" + contentId) {
                	li.addClass("active");
                    foundTab = true;
                        //break;
                } else {
                	li.removeClass("active");
                }
        	});
        	
        }

        // Show the content with the specified ID.
        var divsToHide = [];
        var divs = $$('.tab_panel');
        for (var i = 0; i < divs.length; i++) {
            var div = divs[i];

            if (div.id == "tab_" + contentId)
	            div.removeClass('tab_hide');
            else
                divsToHide.push(div);
        }

        // Hide the other content boxes.
        for (var i = 0; i < divsToHide.length; i++)
            divsToHide[i].addClass('tab_hide');

        // Change the address bar.
        if (!skipReplace) window.location.replace("#" + contentId);
        
        // change form submit
        try {
			fact = document.forms['zappa'];
			if ((i = fact.action.indexOf('#')) >= 0) {
				fact.action = fact.action.substring(0,i+1)+contentId;
			} else {
				fact.action += '#'+contentId;
			}
		} catch(e) {}
		
		// modify edit link
		try {
			el = $('cms_admin_link');
			h = el.getProperty('href');
			if ((i = h.indexOf('#')) >= 0) {
				h = h.substring(0,i+1)+contentId;
			} else {
				h += '#'+contentId;
			}
			el.setProperty('href',h);
		} catch(e) {}
    },

    OnClickHandler: function (e) {
        // Stop the event (to stop it from scrolling or
        // making an entry in the history).
        var found = false;
        var tab = this.hash.substring(1);
        var toc = document.getElementById("tab_toc");
        if (toc) {
        	$$('#tab_toc li a').each(function(anc) {
        		if (anc.hash == '#'+tab) {
        			found=true;
        		}
        	});
        }
        
        if (!found) {
        	return true;
        }
        
        if (!e) e = window.event;
        if (e.preventDefault) e.preventDefault(); else e.returnValue = false;

        // Get the name of the anchor of the link that was clicked.
        Tabs.GoTo(tab);
    },

    Init: function () {
        var toc = $('tab_toc');
        if (!toc || toc.hasClass('not_hash')) return;

        // Attach an onclick event to all the anchor links on the page.
        var anchors = $$("#tab_toc a");
        var h1 = window.location.href;
        var idx = window.location.href.indexOf('#');
        if (idx > 0) {
	    	h1 = h1.substring(0,idx);
	    }
        //console.log('href='+h1);
        for (var i = 0; i < anchors.length; i++) {
            var a = anchors[i];
            if (a.hash.length > 1) {
            	h2 = a.href.substring(0,a.href.indexOf('#'));
            	if (h1 != h2) {
            		continue;
            	}
            	a.onclick = Tabs.OnClickHandler;
            }
        }

        var contentId;
        if (window.location.hash) contentId = window.location.hash.substring(1);

        if (!contentId) {
        	// open first panel
        	divs = $$('#tab_toc a');
			if (divs.length) {
				contentId = divs[0].hash.substring(1);
			}
        }
        Tabs.GoTo(contentId, true);
    }
};

var contentId = window.location.hash;

/* forms helpers */

function tznCombo0(ff,tt) {
	$(tt+'Select').setStyle('display','none');
	$(tt+'Text').setStyle('display','block');
	ff.elements[tt+'Sel'].selectedIndex=0;
}

function tznCombo1(ff,tt) {
	box = ff.elements[tt+'Sel'];
	val = box.options[box.selectedIndex].value;
	if (val == '-') {
		$(tt+'Select').setStyle('display','none');
		$(tt+'Text').setStyle('display','block');
	}
	box.selectedIndex=0;
}

function tznCombo2(ff,tt) {
	box = ff.elements[tt+'Sel'];
	box.selectedIndex=0;
	$(tt+'Text').setStyle('display','none');
	$(tt+'Select').setStyle('display','block');
	ff.elements[tt+'Txt'].value = '';
}

/* ajax */
var pce_wait;

function ajax_start() {
	$('mb_close_link').addClass('loading');
}

function ajax_stop() {
	try {
		$('mb_close_link').removeClass('loading');
	} catch(e){}
}

function ajax_load(url) {
	ajax_start();
	pce_ajax = new Ajax(url, {
		method: 'get',
		evalScripts: true,
		update: $('mb_contents'),
		onComplete: function() {
			ajax_stop();
		}
	}).request();
	return false;
}

function ajax_send_pop(id) {
	var elf = elu = $(id);
	elf.send({
		update: elu, 
		evalScripts: true,
		onRequest: function() {
			ajax_start();
		},
		onComplete: function() {
			ajax_stop();
			pceTips = new Tips($$('#'+id+' .pce_tip'), {
				maxTitleChars: 50
			});
		}
	}); 
	return false;
}

function ajax_send(id) {
	var pce_wait = new Element('div',
		{'class':'pce_wait'}
	);
	var tips = '#'+id;
	var elf = elu = $(id);
	if (arguments.length > 1) {
		tips = '#'+arguments[1];
		elu = $(arguments[1]);
	}
	elf.send({
		update: elu, 
		evalScripts: true,
		onRequest: function() {
			pce_wait.setOpacity(0.8);
			pce_wait.injectBefore('global');
			/*
			pce_wait.addEvent('click',function() {
				pce_ajax.cancel();
				this.remove();
				cms_message('Request cancelled!',true);
			});
			*/
		},
		onComplete: function() {
			pce_wait.remove();
			//if (!$('condal')) {
				try {
					MOOdalBox.init();
				} catch(e) {}
			//}
			pceTips = new Tips($$(tips+' .pce_tip'), {
				maxTitleChars: 50
			});
		}
	}); 
	return false;
}

/* instant alert / info */

var cms_alert;
var pce_ajax;

function cms_message(str,err) {

	if (str) {
		cms_alert = new Element('div');
		cms_alert.setProperty('id','tzn_message');
		if (err) {
			cms_alert.addClass('error');
		}
		cms_alert.setHTML(str);
		cms_alert.setOpacity(0);
		cms_alert.injectBefore('global');
		// new Fx.Slide('tzn_message').slideIn().start();
		new Fx.Style(cms_alert, 'opacity').start(0,1);
		window.setTimeout('cms_message()',2500);
	} else {
		// new Fx.Slide('tzn_message').slideOut().start();
		new Fx.Style(cms_alert, 'opacity').start(1,0);
		//e.removeClass('error');
		window.setTimeout('cms_alert.remove()',500);
	}
}

/* search and currency */
function pce_change_currency(fo) {
	fd = document.forms['zappa'];
	fd.userCurrency.value=fo.userCurrency.value;
	try {
		if (fd.elements['userRateId']) {
			fd.userRateId.value = fo.userRateId.value;
			fd.userRateSpec.value = fo.userRateSpec.value;
			fd.userMarketId.value = fo.userMarketId.value;
		}
	} catch(e) {}
	fd.submit()
}

/* misc */
function cms_trans_anchor(tag) {
	if (window.location.hash) {
		if (i = tag.href.indexOf('#')) {
			tag.href = tag.href.substr(0,i);
		}
		tag.href += '&tab='+window.location.hash.substring(1);
	}
}

function cms_pop(name, x, y) {
   return window.open('',name,'toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=0,status=0,alwaysRaised=0,width='+x+',height='+y)
}

function cms_newsletter_send(msg, test, init) {
	document.ahmet.test.value=test;
	document.ahmet.init.value=init;
	if (msg) {
		if(confirm(msg)) {
			window.open('', 'pce_newsletter', 'height=100,width=200,toolbar=0,menubar=0,scrollbars=0,resizable=1,copyhistory=0,directories=0,status=0');
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

function contact_email() {
	document.write('<a href="mailto:info@pcetravel.com">info@pcetravel.com</a>');
}
function cms_toggle(id) {
	e = $(id);
	sh = (e.getStyle('display') == 'none')?true:false;
	if (arguments.length > 1) {
		sh = arguments[1];
	}
	if (sh) {
		e.setStyle('display','');
		/*
		if (e.getTag() == 'table') {
			e.setStyle('display','table');
		} else if (e.getTag() == 'tr') {
			e.setStyle('display','');
		} else {
			e.setStyle('display','block');
		}
		*/
	} else {
		e.setStyle('display','none');
	}
}
function cms_popup(id) {
	e = $(id);
	sh = (e.getStyle('display') == 'none')?true:false;
	if (sh) {
		pce_wait = new Element('div',
			{'class':'pce_blur'}
		);
		pce_wait.setOpacity(0.8);
		pce_wait.addEvent('click',function() {
			e.setStyle('display','none');
			this.remove();
		});
		e.setStyle('display','block');
		pce_wait.injectBefore(id);
		
	} else {
		e.setStyle('display','none');
		pce_wait.remove();
	}
}
function cms_user_toggle(m) {
	try {
		cms_toggle('sentity',m);
		cms_toggle('sperson1',1-m);
		cms_toggle('sperson2',1-m);
		cms_toggle('sperson3',1-m);
		cms_toggle('sperson4',1-m);
	} catch(e) {}
}
function cms_slide(id) {
	cms_toggle(id);
}
function slidechange() {
	$('illustration').setStyles({
		'background-image': 'none'
	});
}
function slideshow(i) {
	if (i > 9) {
		i = 0;
	}
	j = i+1;
	j = (j<10)?'_0'+j:'_'+j;
	$('illustration').setStyle('background-image','url(/template/images/slideshow'+j+'.jpg)');
	pos = i * 569;
	var fx = carousel.effects({duration: 500});
 	fx.start({
		'opacity': 0
	}).chain(function(){
		this.start({
			'left': -pos
		});
	}).chain(function() {
		this.start({
			'opacity': 1
		});
	})
	window.setTimeout('slideshow('+(i+1)+')',2500);
}
function staying_alive() {
	$('keepalive').setStyle('background-position','right');
	//$('keepalive').setHTML('<img src="/images/connection.gif" />');
	$('keepalive').setStyle('background-image','url(/images/connection.gif)');
	new Ajax('/ajax_staying_alive.php', {
		method: 'get',
		evalScripts: true,
		update: $('keepalive'),
		onComplete: function() {
			$('keepalive').setStyle('background-image','url(/images/connection.png)');
		}
	}).request();
	pceSlideShow = window.setTimeout('staying_alive()',600000);
}

var pceTips;
var pceSlideShow;
var carwrap;
var carousel;
	
window.addEvent('domready', function() {
	Tabs.Init();
	pceTips = new Tips($$('.pce_tip'), {
		maxTitleChars: 50
	});
	$$('.tznSearch').some(function(el,idx){
		el.focus();
		return true;
	});
	$$('.fclk').each(function(el) {
		//console.log('found one: '+el);
		el.getElements('tbody tr').each(function(el) {
			el.addEvent('click',function(e){
				try {
					if (e.target) {
						targ=e.target;
  					} else if (e.srcElement) {
						targ=e.srcElement;
					}
					if (targ.nodeType==3) // defeat Safari bug
					{
						targ = targ.parentNode;
					}
					if (targ.tagName == 'TD') {
						// e.preventDefault();
						if (e3 = this.getElement('a')) {
							window.location.href = e3;
						}
					}
				} catch(err) {
				}
			});
		});
	});
	$$('.sortclk').each(function(el) {
		el.addEvent('click',function(e) {
			window.location.href=this.getElement('a').getProperty('href');
		});
	});
	if ($('keepalive')) {
		window.setTimeout('staying_alive()',600000);
	}
	try {
		jsCalInit();
	} catch (e) {}
	if (carwrap = $('illustration')) {
		carousel = new Element('ul',{'id':'carousel'})
		for (i=1;i<=10;i++) {
			j = (i<10)?'_0'+i:'_'+i;
			carousel.adopt(new Element('li').adopt(new Element('img',{'src':'/template/images/slideshow'+j+'.jpg'})));
		}
		carwrap.empty();
		carousel.injectInside(carwrap);
		pceSlideShow = window.setTimeout('slideshow(1)',2500);
		window.setTimeout('slidechange()',1000);
	}
});