// when the DOM is ready...
$(document).ready(function () {
    $.ajaxSetup ({
		cache: false
	});
	
	$("#debug").ajaxError(function(event, request, settings){
		$(this).append("<li>Error requesting page " + settings.url + "</li>");
	});
	
	$("div#cPerfectFit a").fancybox();
	$("div#cQPorter a").fancybox();
	$("div#cRailcity a").fancybox();
	$("div#cSportpalast a,div#cTonhalle a,div#cStraightahead a,div#cAllnightlong a,div#cBcone a").fancybox({
		'frameWidth': 360,
		'frameHeight': 288,
		'hideOnContentClick': true
	});

	$("a.fancy").fancybox();
	
	var form_ = $('div#frmContactContainer');
	
	$('button',form_).click(function() {
		var name_ = $('#name').attr('value');
		var email_ = $('#email').attr('value'); 
		var message_ = $('#message').attr('value'); 
		var i = Math.round(10000*Math.random());
		$.ajax({
			type: "GET",
			url: "system/sendmail.php",
			dataType: "text",
			data: 'name='+name_+'&email='+email_+'&message='+message_+'&i='+i,
			success: function(msg){
				//alert('hallo');
				handleCallback(msg,name_);
			}
		});
		return false;
	});
	
	createFlashModal('Promo',400,320);
	
	$('#Promo').click(function() {
		$(this).hide();
	});
	
	function handleCallback(m,p){
		if(m=='ok'){
			$('form input, form textarea',form_).each(function() {
				$(this).attr('value','');
			});
			var msg_ = '<h2>Hallo '+p+'</h2><p>Vielen Dank f&uuml;r deine Nachricht! Ich werde mich umgehend drum k&uuml;mmern.<br /><br />Viele Gr&uuml;sse<br />Sarna</p>';
			showInfo(300,180,msg_,'ok');
		} else {
			showInfo(300,180,m,'error');
		}
	}
	
	function showInfo(w,h,m,s) {
		closeInfo();
		var w_ = w;
		var h_ = h;
		var state_ = s;
		form_.append('<div class="box-msg '+state_+'">'+m+'<a href="#" id="returnToForm">schliessen</a></div>');
		info_ = $('div.box-msg',form_);
		info_.css({
				'height' : '180px',
				'width' : '300px',
				'left' : (form_.outerWidth() - w_)/2+'px',
				'top' : (form_.outerHeight() - h_)/2+'px'
			}).hide().fadeIn();
		$('a#returnToForm, div.box-msg').click(function(){
			closeInfo();
		});
	}
	
	function closeInfo() {
		$('div.box-msg').fadeOut('fast',function(){
			$(this).remove();
		});
	}
	
	function createFlashModal(el,w,h,s){
		var w_ = w;
		var h_ = h;
		var state_ = s;
		var window_ = $('body').append('<div id="'+el+'">einen Moment bitte...</div>');
		window_ = $('div#'+el).css({
				'height': h_+'px',
				'width': w_+'px',
				'padding': '10px',
				'margin-left': '-'+w_/2+'px',
				'margin-top': '-'+h_/2+'px'
			})
		.fadeIn(2000,function() {
			var sPromo = new SWFObject("_media/flash/S3D_Intro.swf","ply",w_,h_,"9","#FFFFFF");
			sPromo.addParam("allowfullscreen","true");
			sPromo.addParam("allowscriptaccess","always");
			sPromo.write("Promo");
			window_.append('<a href="#" id="closeModal">schliessen</a></div>');
		})
		//alert($(window_).height());
	}
							
	$('div.scroll').css('overflow','hidden');
	//$('html, body').css('overflow-y','hidden');
	
	$('a.external').each(function(){
		$(this).attr('target','_new');
	 });	$(document).ready(function () {
		setNav($('ul.navContent a.screen01'));
		$('li.screen02').hide();
		$('ul.navContent a').click(function() {
			unsetNav();
			setNav($(this));
			showPage($(this));
			return false;
		});
	});
	
	function showPage(el) {
		var class_ = $(el).attr('class');
		var start_ = class_.indexOf('screen',0);
		var end_ = class_.indexOf(' ',start_);
		class_ = class_.substr(start_,end_);
		$('li.screen').not('li.'+class_).fadeOut('fast');
		$('li.'+class_).fadeIn();
	}
	
	function setNav(el) {
		el.addClass('selected');
	}
	
	function unsetNav() {
		$('ul.navContent a').each(function() {
			$(this).removeClass('selected');
		});
	}
	
// SLIDER //////////////////////////////////////////////////////////////////////////////////////////////
	
	var $panels = $('#slider .scrollContainer > div');
	var $container = $('#slider .scrollContainer');
	
	// if false, we'll float all the panels left and fix the width 
	// of the container
	var horizontal = true;
	
	// float the panels left if we're going horizontal
	if (horizontal) {
	  $panels.css({
		'float' : 'left',
		'position' : 'relative' // IE fix to ensure overflow is hidden
	  });
	  
	  // calculate a new width for the container (so it holds all panels)
	  $container.css('width', $panels[0].offsetWidth * $panels.length);
	}
	
	// collect the scroll object, at the same time apply the hidden overflow
	// to remove the default scrollbars that will appear
	var $scroll = $('#slider .scroll').css('overflow', 'hidden');
	
	// apply our left + right buttons
	$scroll
	  .before('<img class="scrollButtons left" src="_media/img/framework/slider-left.png" />')
	  .after('<img class="scrollButtons right" src="_media/img/framework/slider-right.png" />');
	
	// handle nav selection
	function selectNav() {
	  $(this)
		.parents('ul:first')
		  .find('a')
			.removeClass('selected')
		  .end()
		.end()
		.addClass('selected');
	}
	
	$('#slider .navigation').find('a').click(selectNav);
	
	// go find the navigation link that has this target and select the nav
	function trigger(data) {
	  var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
	  selectNav.call(el);
	}
	
	if (window.location.hash) {
	  trigger({ id : window.location.hash.substr(1) });
	} else {
	  $('ul.navigation a:first').click();
	}
	
	// offset is used to move to *exactly* the right place, since I'm using
	// padding on my example, I need to subtract the amount of padding to
	// the offset.  Try removing this to get a good idea of the effect
	var offset = parseInt((horizontal ? 
	  $container.css('paddingTop') : 
	  $container.css('paddingLeft')) 
	  || 0) * -1;
	
	
	var scrollOptions = {
	  target: $scroll, // the element that has the overflow
	  
	  // can be a selector which will be relative to the target
	  items: $panels,
	  
	  navigation: '.navigation a',
	  
	  // selectors are NOT relative to document, i.e. make sure they're unique
	  prev: 'img.left', 
	  next: 'img.right',
	  
	  // allow the scroll effect to run both directions
	  axis: 'xy',
	  
	  onAfter: trigger, // our final callback
	  
	  offset: offset,
	  
	  // duration of the sliding effect
	  duration: 500,
	  
	  // easing - can be used with the easing plugin: 
	  // http://gsgd.co.uk/sandbox/jquery/easing/
	  easing: 'swing'
	};
	
	// apply serialScroll to the slider - we chose this plugin because it 
	// supports// the indexed next and previous scroll along with hooking 
	// in to our navigation.
	$('#slider').serialScroll(scrollOptions);
	
	// now apply localScroll to hook any other arbitrary links to trigger 
	// the effect
	$.localScroll(scrollOptions);
	
	// finally, if the URL has a hash, move the slider in to position, 
	// setting the duration to 1 because I don't want it to scroll in the
	// very first page load.  We don't always need this, but it ensures
	// the positioning is absolutely spot on when the pages loads.
	scrollOptions.duration = 1;
	$.localScroll.hash(scrollOptions);
});
