/*
 * jQuery plugin: fieldSelection - v0.1.0 - last change: 2006-12-16
 * (c) 2006 Alex Brem <alex@0xab.cd> - http://blog.0xab.cd
 */

(function() {

	var fieldSelection = {

		getSelection: function() {

			var e = this.jquery ? this[0] : this;

			return (

				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					var l = e.selectionEnd - e.selectionStart;
					return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
				}) ||

				/* exploder */
				(document.selection && function() {

					e.focus();

					var r = document.selection.createRange();
					if (r == null) {
						return { start: 0, end: e.value.length, length: 0 }
					}

					var re = e.createTextRange();
					var rc = re.duplicate();
					re.moveToBookmark(r.getBookmark());
					rc.setEndPoint('EndToStart', re);

					return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
				}) ||

				/* browser not supported */
				function() {
					return { start: 0, end: e.value.length, length: 0 };
				}

			)();

		},

		replaceSelection: function() {

			var e = this.jquery ? this[0] : this;
			var text = arguments[0] || '';

			return (

				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
					return this;
				}) ||

				/* exploder */
				(document.selection && function() {
					e.focus();
					document.selection.createRange().text = text;
					return this;
				}) ||

				/* browser not supported */
				function() {
					e.value += text;
					return this;
				}

			)();

		}

	};

	jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });

})();
// mouseover for detailed offers
$(document).ready(function(){

	// blog
	if ($('#blogroll').length > 0) { 
		$('#blogroll').addClass('threeCol last');
		$('.alternateArchive_meta').show();
		$('.alternateArchive_comments').show();
	}
	
	if (location.href == HTTP_ROOT) {
		content = $('#content').html();
		$('#content').html('');
		$('#teaser').before('<div id="content">'+content+'</div>');
	}
	
	
	// 
	$('.hidden').css('display','none').css('padding-left','20px');
	$('.toggleNext').each(function(){
		if ($(this).hasClass('closed')) {
			
		} else {
			$(this).addClass('open');
		}
	});
	$('.toggleNext').css('cursor','pointer').click(function(){
		if ($(this).hasClass('open')) {
			$(this).removeClass('open');
			$(this).addClass('closed');
		} else {
			$(this).removeClass('closed');
			$(this).addClass('open');
		}
		$(this).next().slideToggle();
	});

	
	// lightbox
	$('#content img:not(.editsymbol)').addClass('lb');
	if($("img.lb").length) {
		$(".lb").mouseover(function() {
			$(this).animate({opacity: 0.7 }, 300 );
			$(this).attr("title",$(this).attr("alt"));	
		});
		$(".lb").mouseout(function() {
			$(this).animate({opacity: 1 }, 300 );
		});
		$(".lb").lightBox();
			
	}
	
	// INPUTS LEEREN
	$("input.clean").focus(function(){
		$(this).val('');
	});
	$("input.clean").blur(function(){
		var newValue = $(this).val();
		if (newValue == '') {
			$(this).val($(this).attr('defaultValue'));
		}
	});
	
});


var timer = 8000;
var changeTeaserRight = window.setInterval("newTeaserRight('.ajaxMoverWrapRight .ajaxMoverInner');",timer);
var timeouter = null;

function resume() {
	window.clearTimeout(timeouter);
	window.clearInterval(changeTeaserRight);
	changeTeaserRight = window.setInterval("newTeaserRight('.ajaxMoverWrapRight .ajaxMoverInner');",timer);
}

function newTeaserRight (el) {
	curr_id = $('.ajaxMoverWrapRight .ajaxMoverInner').attr('id');
	$('#'+curr_id).animate({'left':'+=250','opacity': '0'},'slow',function(){
		$(this).remove();
		$('.ajaxMoverWrapRight').load(
				HTTP_ROOT+'ajax/movingTeaser',
				{'current':curr_id,'pref': 'mt_','style':'position: relative; left: -250px; opacity: 0','prev':curr_id},
				function(){
					$('.ajaxMoverWrapRight').find('.ajaxMoverInner').animate({'left':'+=250','opacity': '1'});
				});
	});
}

function lastTeaserRight (el) {
	curr_id = $('.ajaxMoverWrapRight .ajaxMoverInner').attr('id');
	$('#'+curr_id).animate({'left':'-=250','opacity': '0'},'slow',function(){
		$(this).remove();
		$('.ajaxMoverWrapRight').load(
				HTTP_ROOT+'ajax/movingTeaser',
				{'current':curr_id,'pref': 'mt_','style':'position: relative; left: +250px; opacity: 0','last': '1','prev':curr_id},
				function(){
					$('.ajaxMoverWrapRight').find('.ajaxMoverInner').animate({'left':'-=250','opacity': '1'});
				});
	});
}


$(document).ready(function(){
	
	if ($('#teaser').html().length > 0) {
		$('#teaser').show();
	}
	
	//newTeaserRight('.ajaxMoverWrapRight .ajaxMoverInner');
	
	$('#teaser').hover(
		function(){
			window.clearInterval(changeTeaserRight);
		},
		function(){
			timeouter = window.setTimeout("resume()", 10000);
	});
	
	if(location.pathname == '/') {
		$('#teaser').append('<a id="mt_last">&lang;</a> <a id="mt_next">&rang;</a>');
	}
	
	$('#mt_next').click(function(){
		newTeaserRight('.ajaxMoverWrapRight .ajaxMoverInner');
		window.clearInterval(changeTeaserRight);
		window.clearTimeout(timeouter);
		timeouter = window.setTimeout("resume()", 20000);
	});
	$('#mt_last').click(function(){
		lastTeaserRight('.ajaxMoverWrapRight .ajaxMoverInner');
		window.clearInterval(changeTeaserRight);
		window.clearTimeout(timeouter);
		timeouter = window.setTimeout("resume()", 20000);
	});
	
});