/**
 * @author jth
 */

function wopen(left, top, wi, he, link, toolbar) {
	wincom = window.open(link, 
						 "fenster",
						 "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1"
						 + ", resizable=1"
						 + ", left=" + left 
						 + ", top=" + top 
						 + ", width=" + wi
						 + ", height=" + he);
	wincom.focus();
}

function wopenAL(left, top, width, height, tb, lc, dr, st, mb, sb, rz, link) {
	wincom = window.open(link, 
						 "fenster",
						 "toolbar=" + tb
						 + ", location=" + lc
						 + ", directories=" + dr
						 + ", status=" + st
						 + ", menubar=" + mb
						 + ", scrollbars=" + sb
						 + ", resizable=" + rz
						 + ", left=" + left
						 + ", top=" + top
						 + ", width=" + width
						 + ", height=" + height);
	wincom.focus();
}

function showLoadtime() {
	endLoadtime = new Date();
	diffLoadtime = endLoadtime.getTime() - startLoadtime.getTime();
	statusLoadtime = "Reisewelten - Ladezeit: " + diffLoadtime / 1000 + " Sekunden";
	window.defaultStatus = statusLoadtime;
}



startLoadtime = new Date();



jQuery.noConflict();
 
jQuery(document).ready(function($) {
    $('#sidebar h5').each(function(index) {
		$('<img src="./static/images/tmp/link_pfeil_grau_unten.png">')
		.click(function(event) {
			// event.target.blur();
			var $ul = $(this).parent('h5').next('ul'); 
			$(this).attr({ src: './static/images/tmp/link_pfeil_grau' + 
				($ul.is(':hidden') ? '_unten' : '') + '.png' });
			$ul.slideToggle('normal');
		})
		.appendTo(this);
	});

	$('<select id="siteselect" size="1"><option value="">L&auml;nderauswahl...</option></select>')
	  .appendTo('#globalnav').change(function() {
		if (this.value != '') {
			window.location = this.value;
			// alert ('goto ' + this.value);
		}
	});
	var $siteselect = $('#siteselect');
	$('#globalfooter a').each(function(index) {
		$siteselect.append('<option value="' + this.href + '">' + $(this).text() + '</option>');
	});
		
	$('a.wOpen, a.wOpenAL, a.windowOpen').click(function(event) {
		event.target.blur();

		var urlParts = this.href.match(/(.+)(#((.+)\/)?(.+))/);
		// alert('goto "' + urlParts[1] + '", "' + urlParts[4] + '", "' + urlParts[5] + '"');
		var newWindow = window.open(urlParts[1], urlParts[4], urlParts[5]);
		newWindow.focus();
		
		return false;
	});
});



jQuery(document).ready(function($) {
	var $search = $('#search').addClass('overlabel');
    var $searchInput = $search.find('input');
    var $searchLabel = $search.find('label');
    
    if ($searchInput.val()) {
        $searchLabel.hide();
    }
    
    $searchInput
    .focus(function() { 
        $searchLabel.hide(); 
    })
    .blur(function() { 
        if (this.value == '') { 
            $searchLabel.show(); 
        } 
    });
    
    $searchLabel.click(function() {
        $searchInput.trigger('focus');
    });
});



/*
jQuery(document).ready(function($) {
	$('.btn').each(function(){
		var b = $(this);
		var tt = b.text() || b.val();
		if ($(':submit,:button',this)) {
			b = $('<a>').insertAfter(this). addClass(this.className).attr('id',this.id);
			$(this).remove();
		}
		b.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span>').
		text(tt).append('<i></i><span></span>'));
	});
});
*/



jQuery(document).ready(function($) {
	$('input.btns[type=submit]').each(function(){
		var $thisSummit = $(this);
		$thisSummit.replaceWith('<a class="btns"><span>' + $(this).val() + '</span></a>');
		$('a.btns').click(function() {
			$(this).parents('form').trigger('submit');
			return false;
		});
	});
});



jQuery(document).ready(function($) {
	$('.error-message.wrong').removeClass('wrong');
	
	$('input.wrong, textarea.wrong, select.wrong').each(function(){
		$(this).siblings('.error-message').not('[rel]').addClass('wrong');
		$('[rel=' + this.name + ']').addClass('wrong');
	}).eq(0).focus();
});



jQuery(document).ready(function($) {
	if ($("a[rel^='prettyPhoto']").length > 0) {
		var link = $('<link>');
		link.attr({ rel: 'stylesheet', type: 'text/css', 
					href: 'http://static.reisewelten.org/styles/prettyPhoto-facebook.min.css',
			        media: 'screen', charset: 'utf-8'});
		$('head').append(link);
		
		$.getScript('/static/scripts/jquery.prettyPhoto.min.js', function() {
			$("a[rel^='prettyPhoto']").prettyPhoto();
			// $("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'fast', theme:'facebook', slideshow:3000});
			// $(".gallery:first a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'light_square',slideshow:2000});
			// $(".gallery:gt(0) a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'fast',slideshow:10000});		
		});
	}

	if ($('.weather.animate div.view').length > 0) {
		$.getScript('/static/scripts/jWeather.js');		
	}
	
	if ($('.teaser.animate div.view').length > 0) {
		$.getScript('/static/scripts/jObjects.js');
	}
});




function showLoading() {   
    jQuery('ul.hotels li, ul.hotels div.img, ul.hotels h4').css({ opacity: 0 });
    jQuery('<li id="loadingPanel" class="loading first">Lade Ergebnisse</li>').prependTo('ul.hotels');
    jQuery('html,body').animate({ scrollTop: 0 }, 'fast');
}



jQuery(document).ready(function($) {
	$('.pager a').each(function() {
		$(this).click(function() { showLoading(); });
	});
	
	$('#hotel-select select').change(function() {
	    (this.id == 'idRegion') && $('#idStadt').attr('selectedIndex', 0);
	    $(this).blur().parents('form').submit();
	});
	
	$('form').submit(function() {
	    showLoading();
	});
	
    var waitAnimation = new Image();
    waitAnimation.src = 'http://static.reisewelten.org/images/waitanimation.gif';
});

	

jQuery(document).ready(function($) {
	$('.alpha-shadow a img').each(function() {
		var i = $(this);
		
		/*
		i.css({ 
		'-moz-border-radius': '5px',
		'-moz-box-shadow': '1px 1px 2px #999',
		'-webkit-border-radius': '5px',
		'-webkit-box-shadow': '1px 1px 2px #999',
		'border-radius': '5px',
		'box-shadow': '1px 1px 2px #999'
		});
		*/
		
		/*
		var w = i.width();
		var h = i.height();
		var nw, nh;

		if (w > h) {			
			nw = w + 12;
			nh = Math.round(h * (nw / w));
		} else {
			nh = h + 12;
			nw = Math.round(w * (nh / h));
		}
		
		var d = i.parents('div.alpha-shadow'); //.css('position', 'absolute');
		var fpos = $('<div>').addClass('img')
                 .css({'position': 'relative', width: d.width(), height: d.height()})
                 .insertBefore(d).position();
                
		d.css({position: 'absolute', top: fpos.top, left: fpos.left });
		i.hover(
			function() {
				i.stop().css({ 'z-index': 100, 'width': w, 'height': h, 'border-color': '#ff4005' })
				    .animate({ width: nw, height: nh }, 'fast');
				d.stop().css({ top: fpos.top, left: fpos.left })
					.animate({ top: fpos.top - Math.round((nh - h) / 2), left: fpos.left - Math.round((nw - w) / 2) }, 'fast');
			},
			function() {
				i.stop().animate({ width: w , height: h }, 'fast', function() { i.css({'z-index': 1, 'border-color': '#03c' }); });
                d.stop().animate({ top: fpos.top, left: fpos.left }, 'fast');
            }
		);
		*/
	});
});
