var roar = false;
var mysite = false;

var pageCtrl = new Class({
	Implements: [Options,Events],
  options: {
  },
  initialize: function(options){
  	this.setOptions(options);
  	this.adtarget = '';

 		this.catnavitems = $$('.mycat');
 		this.catnavheadlink = $('catnavheadlink');
  	this.catHash = new Hash.Cookie('rk_region', {path: '/', duration: 0});
  	
  	this.adHash = new Hash.Cookie('rk_ads', {path: '/', duration: 0});
  	
  	this.watchHash = new Hash.Cookie('rk_watchlist', {path: '/', duration: 21});
  	if($('catnav')) { 	
			//this.updateCatCount();
		}
	},
	toggleCategory: function(catid) {
		
		if(this.catHash.get(catid) == catid) {
			this.disableCategory(catid);
			} else {
			this.enableCategory(catid);
			}
		this.updateCategoryDisplay(catid);
		this.updateCatCount();
	},
	disableCategory: function(catid) {
		this.catHash.erase(catid);
	},
	enableCategory: function(catid,info) {
		this.disableAllCategories();
		this.catHash.include(catid, info);
	},
	updateCategoryDisplay: function(catid) {
		if(this.catHash.get(catid)) {
			// its active
			$('catselect-'+catid).addClass('is-selected');
			} else {
			$('catselect-'+catid).removeClass('is-selected');
			}
	},
	updateCatCount: function() {
		if(this.catHash.getLength() > 0) {
			var myKey; this.catHash.every( function(value, key){ myKey = key; return false;}); // get first hash key/value
			this.catnavheadlink.set('html','Region: '+this.catHash.get(myKey));
			} else {
			this.catnavheadlink.set('html','Region wählen');
			}
	},
	toggleAllCategories: function() {
		this.catnavitems.each(function(e) {
			this.toggleCategory(e.rel);
			},this);
	},
	disableAllCategories: function() {
		this.catHash.empty();
		this.updateCatCount();
		this.catnavitems.each(function(e) {
			this.updateCategoryDisplay(e.rel);
			},this);
	},
	addToWatchList: function(postid, hl) {
		this.watchHash.include(postid, postid);
		roar.alert(hl,"wurde auf die Merkliste gesetzt.");
	},
	removeFromWatchList: function(postid, hl, postid) {
		this.watchHash.erase(postid);
		roar.alert(hl, "wurde von der Merkliste entfernt.");
		
		var findpost = 'postid-' + postid;
		$(findpost).destroy();
	},
	loadGoogleMaps: function(url) {
		//this.ajax.send({ 'data': 'ajax_map='+url });
		SqueezeBox.open('/?ajax_map='+url, {handler: 'ajax' ,size: {x: 650, y: 500} });
	},
	loadGoogleMapsIFrame: function(url) {
		//this.ajax.send({ 'data': 'ajax_map='+url });
		SqueezeBox.open(url, {handler: 'iframe' ,size: {x: 650, y: 500} });
	},
	startAdProgress: function() {
		adbar = $('adprogress');		
		$('sbox-window').setStyle('background-color','#000000');
		$('sbox-content').setStyle('background-color','#000000');
		$('sbox-btn-close').setStyle('display','none');
		if(adbar) {
			var adprogress = new Fx.Tween(adbar, {property: 'width', duration:3000});
			adprogress.start(600).chain( function() { document.location.href = mysite.adtarget; });
			}
	},
	hideWeather: function() {
		var d = $('detailnav');
		var w = d.getElementById('wp_addweather');
		w.setStyle('background-color','transparent');
		w.setStyle('opacity',0);
	
	}

});

window.addEvent('domready', function() {

roar = new Roar();

mysite = new pageCtrl();

// fixed position footer on iOS
if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
{ $("footer").setStyle("position", "static"); }


// searchform behaviour
if($('s')) {
	$('s').addEvent('click', function() {
		this.setStyle('color','#333');
		if(this.get('color') != '#333') this.set('value','');
		});
	}


// init catnav
if($('catnav')) {
	
	$('catnavheadreset').setStyle('opacity',0);
	
	var cns = $('catnavselect');
	var cn = $('catnav');
	var rolltimeout = false;



	cns.setStyle('opacity',0);
	cn.addEvent('mouseenter', function() {
		rolltimeout = $clear(rolltimeout);	
		this.tween('height',cns.getStyle('height').toInt() + 14);
		$('catnavheadreset').fade('in');
		cns.tween('opacity',1);
		});
	cn.addEvent('mouseleave', function() {
			rolltimeout = function() { 
				cn.tween('height','14px');
				$('catnavheadreset').fade('out');
				cns.tween('opacity',0);
				}.delay(2000);
		});	
		
} // end catnav

	// navigation fade
	var fadenavelements = $$('#footer a, .options a, div#header img.channelnav');	
	fadenavelements.each(function(e) {
		e.setStyle('color','#ffffff');
		if(!e.hasClass("active") && !e.hasClass("nofade")) {
			e.setStyle('opacity',0.5);
			e.addEvent('mouseenter', function() { e.tween('opacity',1); });
			e.addEvent('mouseleave', function() { e.tween('opacity',0.5); });
		}
	});


if('splashpage') {
	$$('.interstitial').each(function(el) {
		el.addEvent('click', function(e) {
			e.stop();
			mysite.adtarget = el.get('href');
			SqueezeBox.open('/?displayad=absolut.jpg', { handler: 'ajax', size: {x: 650, y: 650}, closeBtn: false, closable: false, parse: true, overlayOpacity:1 });
			//startlink.delay(8000, el);
		});
	});
} // splashpage

/* ads */

var h_ad = $('hotel-ad');
if(h_ad  && !mysite.adHash.get('hotel-ad')) {
		h_ad.addEvent('click', function(e) {
			e.stop();
			mysite.adHash.include('hotel-ad',1);
			mysite.adtarget = h_ad.get('href');
			SqueezeBox.open('/?displayad=voeslauer2.jpg', { handler: 'ajax', size: {x: 602, y: 850}, closeBtn: false, closable: false, classWindow: 'adbox', parse: true, overlayOpacity:1 });//ruefa.jpg
		});
} // ad


var g_ad = $('green-ad');
if(g_ad  && !mysite.adHash.get('green-ad')) {
		g_ad.addEvent('click', function(e) {
			e.stop();
			mysite.adHash.include('green-ad',1);
			mysite.adtarget = g_ad.get('href');
			SqueezeBox.open('/?displayad=voeslauer2.jpg', { handler: 'ajax', size: {x: 602, y: 850}, closeBtn: false, closable: false, classWindow: 'adbox', parse: true, overlayOpacity:1 });//hertz.jpg
		});
} // ad



var spa_ad = $('spa-ad');
if(spa_ad  && !mysite.adHash.get('spa-ad')) {
		spa_ad.addEvent('click', function(e) {
			e.stop();
			mysite.adHash.include('spa-ad',1);
			mysite.adtarget = spa_ad.get('href');
			SqueezeBox.open('/?displayad=voeslauer2.jpg', { handler: 'ajax', size: {x: 602, y: 850}, closeBtn: false, closable: false, classWindow: 'adbox', parse: true, overlayOpacity:1 });//lancome.jpg
		});
} // ad



var r_ad = $('restaurant-ad');
if(r_ad  && !mysite.adHash.get('restaurant-ad')) {
		r_ad.addEvent('click', function(e) {
			e.stop();
			mysite.adHash.include('restaurant-ad',1);
			mysite.adtarget = r_ad.get('href');
			SqueezeBox.open('/?displayad=voeslauer2.jpg', { handler: 'ajax', size: {x: 602, y: 850}, closeBtn: false, closable: false, classWindow: 'adbox', parse: true, overlayOpacity:1 });//voeslauer.jpg
		});
} 
// ad


var s_ad = $('shopping-ad');
if(s_ad && !mysite.adHash.get('shopping-ad')) {
		s_ad.addEvent('click', function(e) {
			e.stop();
			mysite.adHash.include('shopping-ad',1);
			mysite.adtarget = s_ad.get('href');
			SqueezeBox.open('/?displayad=voeslauer2.jpg', { handler: 'ajax', size: {x: 602, y: 850}, closeBtn: false, closable: false, classWindow: 'adbox', parse: true, overlayOpacity:1 });//shopping.jpg
		});
}
// ad




otherposts = $('other-posts');
if(otherposts) {
	otherpostitems = otherposts.getElements('.postnav-item');
	otherpostitems.each(function(el) {
		el.setStyle('opacity',0);
	});
} // otherposts

}); // end DOMREADY


window.addEvent('load', function() {

var fadeindelay = 0;
var fadeinfn = function() { this.fade('in'); }

otherposts = $('other-posts');
if(otherposts) {
	otherpostitems = otherposts.getElements('.postnav-item');
	otherpostitems.each(function(el) {
			fadeinfn.bind(el).delay(fadeindelay);
			fadeindelay += 300;
		});
}	
	
}); // end onload


function imgMouseOverEvents(outString, overString, selector) {
	$$(selector).each(function(image) {
		image = $(image);
		if (image.hasClass('active'))
			{
			// set active
			if ($type(image.src)) image.src = image.src.replace(outString, overString);
			}
			
		if ($type(image.src)) {
			if (image.src.indexOf(outString) > 0) {
				image.addEvent('mouseenter',function(){
					image.src = image.src.replace(outString, overString);
				}).addEvent('mouseleave', function(){
					image.src = image.src.replace(overString, outString);
				});
			}
		}
	});
};
window.addEvent('domready', function(){imgMouseOverEvents('_off', '_on', 'div#footer img.rollover');});

function displaySlideshowParam( images ) 
{ 
	Slimbox.open( eval( '[' +  images + ']' ), 0, { counterText: "Bild {x} / {y}", loop: true, captionAnimationDuration: 200 }); 
}
