var $j = jQuery.noConflict();

$j(function() {
	// comment next lines to disable features
	bouncer();		
	if ($j("#slides").length > 0) setInterval("slides()", 3000); // slide timer (5000 / 1000ms = 5 sec)
	if ($j("#slides2").length > 0) setInterval("slides2()", 3000); // slide timer (5000 / 1000ms = 5 sec)
	if ($j("form#ad_contact").length > 0) contactForm(); // initialize javascript validators for the contact form 
	if ($j(".gallery").length > 0) folio(500, 0.6); // image shade (timer, opacity)
	add(); //maps, contact form and reservation in ad content
});


// functions


function contactForm() {

	$j(".input", "form#ad_contact").blur(function() { validateInput($j(this)); }); // validate when unfocus

	$j("#submit", "form#ad_contact").click(function() { // validate on submit
		$j(".input", "form#ad_contact").each(function() { validateInput($j(this)); })
		if (!isFormValid())
			return false;
	});

}

//
function bouncer() {	
	$j('a.foto').hover (function() { //mouse in
				$j(this).stop().animate({ paddingLeft: 10 }, 400, 'easeOutBounce');
				}, function() { //mouse out
				$j(this).stop().animate({ paddingLeft: 0 }, 400, 'easeOutBounce');
				});	
	$j('a.mag').hover (function() { //mouse in
				$j(this).stop().animate({ paddingLeft: 20 }, 400, 'easeOutBounce');
				}, function() { //mouse out
				$j(this).stop().animate({ paddingLeft: 0 }, 400, 'easeOutBounce');
				});	
	$j('a.blog').hover (function() { //mouse in
				$j(this).stop().animate({ paddingLeft: 20 }, 400, 'easeOutBounce');
				}, function() { //mouse out
				$j(this).stop().animate({ paddingLeft: 0 }, 400, 'easeOutBounce');
				});	
	
	$j('a.nudge').hover (function() { //mouse in
				$j(this).stop().animate({ paddingRight: 20 }, 400, 'easeOutBounce');
				}, function() { //mouse out
				$j(this).stop().animate({ paddingRight: 0 }, 400, 'easeOutBounce');
				});	
	
				
	
}

//
function add() {	
	$j("a.foto").hover(function() { // show this on click, and hide others
		$j("#foto").addClass("foto2");
	},function() { // show this on click, and hide others
		$j("#foto").removeClass("foto2");
	} );
	
		$j("a.mag").hover(function() { // show this on click, and hide others
		$j("#mag").addClass("mag2");
	},function() { // show this on click, and hide others
		$j("#mag").removeClass("mag2");
	} );
	
		$j("a.blog").hover(function() { // show this on click, and hide others
		$j("#blog").addClass("blog2");
	},function() { // show this on click, and hide others
		$j("#blog").removeClass("blog2");
	} );
	
	
}






//

function isFormValid() {

	return $j(".input.incorrect", "form#ad_contact").length > 0 ? false : true;

}

function validateInput(obj) {

	var id = obj.attr("id");
	var correct = false;

	if (id == "email") { // email validator
		if (obj.val().match(/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/))
			correct = true;
	}
	else if (id == "message") { // message validator
		if (obj.val().replace(/(^\s+)|(\s+$)/g, "") != "")
			correct = true;
	}

	obj.removeClass("correct incorrect"); // clearing
	if (correct) {
		obj.addClass("correct");
	}
	else {
		obj.addClass("incorrect");
	}

}

//



//



//

function folio(time, opacity) {
	$j("img", "#folio")
		.css("opacity", 1)
		.hover(
			function() {
				$j(this).stop().animate({ opacity: opacity }, time);
			},
			function() {
				$j(this).stop().animate({ opacity: 1 }, time);
			}
		);
}

//

function slides() {

	if (ie6) {
		return; // ie6 fails to animate overall opacity of the transparent PNGs
	}

	var $active = $j("#slides img.active");

	if ($active.length == 0) {
		$active = $j("#slides img:last");
	}

	var $next = $active.next().length ?
			$active.next() :
			$j("#slides img:first");

	$active.addClass("pre-active");

	$next.
		css({ opacity: 0 }).
		addClass("active").
		animate({ opacity: 1 }, 1000, function() {
			$active.removeClass("active pre-active");
		});

}


function slides2() {

	if (ie6) {
		return; // ie6 fails to animate overall opacity of the transparent PNGs
	}

	var $active = $j("#slides2 img.active");

	if ($active.length == 0) {
		$active = $j("#slides2 img:last");
	}

	var $next = $active.next().length ?
			$active.next() :
			$j("#slides2 img:first");

	$active.addClass("pre-active");

	$next.
		css({ opacity: 0 }).
		addClass("active").
		animate({ opacity: 1 }, 1000, function() {
			$active.removeClass("active pre-active");
		});

}



//

function newsline() {
	var $ph = $j("#newsline a.ph");

	if ($ph.length == 0) { // prepare basement
		var $first = $j("#newsline a:first");
		$j("#newsline p").append("<br />").append($first.clone());
		$ph = $first.addClass("ph");
	}

	var $active = $j("#newsline a.active");

	if ($active.length == 0) {
		$active = $j("#newsline a:last");
	}

	var $next = $active.next().next().length ?
			$active.next().next() :
			$j("#newsline a:not(.ph):first");

	$next.addClass("active");
	$active.removeClass("active");

	if (ie6) { // bha! another ie6 opacity fail
		$ph.html($next.html());
		$ph.attr("href", $next.attr("href"));
	}
	else {
		$ph.animate({ opacity: 0 }, 500, function() { // hiding
			$ph.html($next.html());
			$ph.attr("href", $next.attr("href"));
			$ph.animate({ opacity: 1 }, 300); // revealing
		});
	}
}


function newsline2() {
	var $ph = $j("#newsline2 a.ph");

	if ($ph.length == 0) { // prepare basement
		var $first = $j("#newsline2 p:first");

		$ph = $first.addClass("ph");
	}

	var $active = $j("#newsline2 p.active");

	if ($active.length == 0) {
		$active = $j("#newsline2 p:last");
	}

	var $next = $active.next().next().length ?
			$active.next().next() :
			$j("#newsline2 p:not(.ph):first");

	$next.addClass("active");
	$active.removeClass("active");

	if (ie6) { // bha! another ie6 opacity fail
		$ph.html($next.html());
		$ph.attr("href", $next.attr("href"));
	}
	else {
		$ph.animate({ opacity: 0 }, 500, function() { // hiding
			$ph.html($next.html());
			$ph.attr("href", $next.attr("href"));
			$ph.animate({ opacity: 1 }, 300); // revealing
		});
	}
}






//

var ie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) < 7;

