
var $j = jQuery.noConflict();

$j(function() {

	// comment next lines to disable features

	lightBox(); // initialize & configure lightbox script

	navigation(); // dynamic light selection in the menu (windows7-like)

	if ($j("form#contact").length > 0) contactForm(); // initialize javascript validators for the contact form

	if ($j("#slides").length > 0) setInterval("slides()", getMeta("pandora.1")); // slide timer

	if ($j("#newsline").length > 0) setInterval("newsline()", getMeta("pandora.2")); // news timer

	if ($j("#folio").length > 0) folio(500, 0.6); // portfolio sleek image shade (timer, opacity)

});


// functions

function getMeta($name) {
	return $j("meta[name=" + $name + "]").attr('content');
}

function contactForm() {

	$j(".input", "form#contact").blur(function() { validateInput($j(this)); }); // validate when unfocus

	$j("#submit", "form#contact").click(function() { // validate on submit
		$j(".input", "form#contact").each(function() { validateInput($j(this)); })
		if (!isFormValid())
			return false;
	});

}

function isFormValid() {

	return $j(".input.incorrect", "form#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-Z]{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 navigation() {
	$j("li a", "#nav").
		mousemove(function(e) {
			var relativeY = (e.pageY - this.offsetTop - 45) / 4;
			$j(this).css("backgroundPosition", "center " + relativeY + "px");
		}).
		mouseout(function(e) {
			$j(this).css("backgroundPosition", "center center");
		}).
		attr("title", ""); // remove title popup in WP
}

//

function lightBox() {
	$j("a[rel^='lightbox']").prettyPhoto({
		animationSpeed: 'fast', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.5, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function() { }
	});
}

function xlightBox() {
			$j("a[rel^='lightbox']").prettyPhoto({
				animationSpeed: 'normal', /* fast/slow/normal */
				opacity: 0.80, /* Value between 0 and 1 */
				showTitle: true, /* true/false */
				allowresize: true, /* true/false */
				default_width: 500,
				default_height: 344,
				counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
				theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
				hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
				wmode: 'opaque', /* Set the flash wmode attribute */
				autoplay: true, /* Automatically start videos: True/False */
				modal: false, /* If set to true, only the close button will close the window */
				changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
				callback: function(){}, /* Called when prettyPhoto is closed */
				markup: '<div class="pp_pic_holder"> \
							<div class="pp_top"> \
								<div class="pp_left"></div> \
								<div class="pp_middle"></div> \
								<div class="pp_right"></div> \
							</div> \
							<div class="pp_content_container"> \
								<div class="pp_left"> \
								<div class="pp_right"> \
									<div class="pp_content"> \
										<div class="pp_fade"> \
											<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
											<div class="pp_loaderIcon"></div> \
											<div class="pp_hoverContainer"> \
												<a class="pp_next" href="#">next</a> \
												<a class="pp_previous" href="#">previous</a> \
											</div> \
											<div id="pp_full_res"></div> \
											<div class="pp_details clearfix"> \
												<a class="pp_close" href="#">Close</a> \
												<p class="pp_description"></p> \
												<div class="pp_nav"> \
													<a href="#" class="pp_arrow_previous">Previous</a> \
													<p class="currentTextHolder">0/0</p> \
													<a href="#" class="pp_arrow_next">Next</a> \
												</div> \
											</div> \
										</div> \
									</div> \
								</div> \
								</div> \
							</div> \
							<div class="pp_bottom"> \
								<div class="pp_left"></div> \
								<div class="pp_middle"></div> \
								<div class="pp_right"></div> \
							</div> \
						</div> \
						<div class="pp_overlay"></div> \
						<div class="ppt"></div>',
				image_markup: '<img id="fullResImage" src="" />',
				flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
				quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
				iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
				inline_markup: '<div class="pp_inline clearfix">{content}</div>'
			});

}

//

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").
				css({ opacity: 0 });
		});

}

//

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
		});
	}
}

//

var ie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) < 7;
