var tabIDs = ['overview','map'];
window.onload = function () {
	$('mapswf').style.visibility='hidden';
	initGMap();
	hidable.each( function (name) {
		if ($('ln_'+name)) {
			$('ln_'+name).hide();
		}
		if ($('seeall_'+name)) {
			$('seeall_'+name).hide();
		}
	});
	sc.each(function (parent) {
		if (parent) {
			parent.each(function (child) {
				if ($('ln_'+child)) {
					$('ln_'+child).hide();
				}
				if ($('seeall_'+child)) {
					$('seeall_'+child).hide();
				}
			});
		}
	});
	startSlideshow();
	//loadSwfs();
	//togleTab('overview');
}

/**
 * This function is opening or closeing the left nav thing
 * 
 * @param {Object} leftNavAnchor
 */

function leftNavOpener(linksDiv) {
	$('mapswf').style.visibility='hidden';
	if ($('ln_'+linksDiv).parentNode.className == 'open') { // if it was open, just close
		closeSeeAllWindow(linksDiv);
		new Effect.BlindUp('ln_'+linksDiv,{
			duration: 0.3
		});
		new PeriodicalExecuter(function (pe) {
			$('ln_'+linksDiv).parentNode.className = '';
			updateSeeAllWindowPosition();
			pe.stop();
		}, 0.280);
	} else { // if it was closed then 
		hidable.each( function (name) {  // check all other is open
			if (name == linksDiv) { // if the actal is the clicked just open
				new Effect.BlindDown('ln_'+linksDiv,{
					duration: 0.3
				});
				new PeriodicalExecuter(function (pe) {
					$('ln_'+linksDiv).parentNode.className = 'open';
					updateSeeAllWindowPosition();
					if (linksDiv == '11' && $('ln_11_region').parentNode.className == 'open') {
						$('mapswf').style.visibility='visible';
					}
					pe.stop();
				}, 0.280);
			} else {
				//alert(sc[name]+' | '+linksDiv+' | '+((sc[name])?sc[name].indexOf(linksDiv):''));
				if (sc[name] && sc[name].indexOf(linksDiv)>=0) { // if it is the parent of the clicked
					//alert('now!');
					sc[name].each( function (scid) {
						if (scid == linksDiv) { // if the actal is the clicked just open
							new Effect.BlindDown('ln_' + linksDiv, {
								duration: 0.3
							});
							new PeriodicalExecuter(function (pe) {
								$('ln_'+linksDiv).parentNode.className = 'open';
								updateSeeAllWindowPosition();
								if (linksDiv == '11_region') {
									$('mapswf').style.visibility='visible';
								}
								pe.stop();
							}, 0.280);
						} else {
							closeSeeAllWindow(scid);
							if ($('ln_' + scid)) {
								new Effect.BlindUp('ln_' + scid, {
									duration: 0.3
								});
								new PeriodicalExecuter(function (pe) {
									$('ln_'+scid).parentNode.className = '';
									updateSeeAllWindowPosition();
									pe.stop();
								}, 0.280);
							}
						}
					});
				} else { // else just close
					closeSeeAllWindow(name);
					if ($('ln_' + name)) {
						new Effect.BlindUp('ln_' + name, {
							duration: 0.3
						});
						new PeriodicalExecuter(function (pe) {
							$('ln_'+name).parentNode.className = '';
							updateSeeAllWindowPosition();
							pe.stop();
						}, 0.280);
					}
				}
			}
		});
	}
}

/**
 * This function is closing the SeeAll windows
 * 
 * @param {Object} windowID
 */
function closeSeeAllWindow(windowID) {
	if ($('seeall_' + windowID) && $('seeall_' + windowID).visible()) {
		$(windowID + '_options').style.overflow = 'hidden';
		new Effect.BlindLeft('seeall_' + windowID, {
			duration: 0.5
		});
	}
	return false;
}

/**
 * This function is opening the SeeAll windows
 * 
 * @param {Object} windowID
 */
function openSeeAllWindow(windowID,parentID) {
	closeAnySeeAllWindow();
	//alert('leftnav.left:'+$('leftnav').cumulativeOffset()[0]+' leftnav.widht:'+$('leftnav').getWidth());
	if ($('seeall_' + windowID)) {
		$('seeall_' + windowID).style.left = ($('leftnav').getWidth()-20) + 'px';
		$('seeall_' + windowID).style.top = ($(parentID ? 'li_' + parentID : 'li_' + windowID).cumulativeOffset()[1]) + 'px';
		//alert('top:'+$('li_'+windowID).getStyle('top')+' left:'+$('seeall_'+windowID).getStyle('left'));
		$(windowID + '_options').style.overflow = 'hidden';
		new Effect.BlindRight('seeall_' + windowID, {
			duration: 0.5
		});
		window.setTimeout("$('" + windowID + "_options').style.overflow = 'auto';", 700);
	}
	return false;
}

/**
 * This function is switching the open/close state of the SeeAll windows
 * 
 * @param {Object} windowID
 */
function togleSeeAllWindow(windowID,parentID){
	if ($('seeall_'+windowID) && $('seeall_'+windowID).visible()) {
		closeSeeAllWindow(windowID);
	} else {
		openSeeAllWindow(windowID,parentID);
	}
}

function updateSeeAllWindowPosition() {
	/*hidable.each( function (name) {
		//if ($('seeall_'+name) && $('seeall_'+name).visible()) {
		//	$('seeall_'+name).style.top = $('ln_' + name).parentNode.cumulativeOffset()[1]+'px';
		//}
	});*/
}

function closeAnySeeAllWindow() {
	hidable.each( function (name) {
		if ($('seeall_'+name)) {
			closeSeeAllWindow(name);
		}
	});
}

function togleTab(tab) {
	tabIDs.each( function (name) {
		if ($(name)) {
			//if ($(name).visible()) {
				if (name != tab) {
					$(name).hide();
					$('opt_'+name).removeClassName('selected');
			//	}
			} else {
			//	if (name == tab) {
					$(name).show();
					$('opt_'+name).addClassName('selected');
				}
			//}
			//alert(name+': ' + $('opt_'+name).classNames());
		}
	});
	return false;
}

/**
 * 
 * @param {Object} event
 */
function updateSearchField(sf,event,val) {
	//alert(sf+' e:'+event);
	switch (event) {
		case 'focus':
			$(sf).value = $(sf).value==val?'':$(sf).value;
		break;
		case 'blur':
			$(sf).value = $(sf).value==''?val:$(sf).value;
		break;
	}
}

/**
 * keywords max checking tools
 */
function checkTagLimit(chbox,ulid) {
	if (!chbox.checked) { // just uncheck
		return true;
	} else { // check the limit
		limit = $(ulid).readAttribute('limit');
		cnt = 0;
		$(ulid).childElements().each( function(li) {
			//alert(li.childElements()[0].checked);
			if (li.childElements()[0].checked) {
				cnt++;
			}
		});
		if (!(limit==0||limit>=cnt)) {
			alert('Sorry, in this category you can choose only '+limit+' option.')
		}
		return limit==0||limit>=cnt;
	}
}

/**
 * keywords clear all
 */
function clearAllTags(ulid) {
	$(ulid).childElements().each( function(li) {
		li.childElements()[0].checked = false;
	});
}

/**
 * Slideshow
 */
var pe;
var imgnum=0;
function changeSlide(num) {
	if (pe) pe.stop();
	imgnum_next = num;
	//alert(imgnum_next);
	Effect.Fade('bigimg_'+imgnum, { duration:1, from:1.0, to:0.0 }); 
	Effect.Appear('bigimg_'+imgnum_next, { duration:1, from:0.0, to:1.0 });
	imgnum=imgnum_next;
	pe = new PeriodicalExecuter(function (ppe) {
		//pe = ppe;
		//alert('hello');
		imgnum_next = imgnum==7?0:imgnum+1;
		while (!$('bigimg_'+imgnum_next)) {
			imgnum_next = imgnum_next==7?0:imgnum_next+1;
		}
		Effect.Fade('bigimg_'+imgnum, { duration:1, from:1.0, to:0.0 }); 
		Effect.Appear('bigimg_'+imgnum_next, { duration:1, from:0.0, to:1.0 });
		imgnum=imgnum_next;
	},5);
}
