$(function() {
	/* water mark box */
	$("input.watermark").each(function(){
		$(this).attr("lang", $(this).attr("value"));
		$(this).css("color", "#CCC");
		$(this).focus(function(){
			if($(this).attr("value") == $(this).attr("lang")) {
				$(this).css("color", "#000");
				$(this).attr("value", "")
			}
		}).blur(function(){
			if($(this).attr("value") == "") {
				$(this).css("color", "#CCC");
				$(this).attr("value", $(this).attr("lang"))
			}
		})
	});
	/* datepicker init */
	$("input[rel=datepicker]").simpleDatepicker();
	/* init fancyboxes */
	$("a[rel=photobook]").fancybox({
		"transitionIn"		: "elastic",
		"transitionOut"		: "elastic",
		"type"				: "image", 
		"titlePosition"    	: "over", 
		"height"			: "98%",
		"titleFormat"		: function(title, array, index, opts) {
			return "<span id=\"fancybox-title-over\">" + title + "</span>";
			return "<span id=\"fancybox-title-over\">Image " + (index+1) + " / " + array.length + "</span>";
		}
	});
	/* init fancyboxes [IFRAME] */
	$("a.fancyboxIframe").each(function(){
		$(this).fancybox({
			"width"				: "90%",
			"height"			: "90%",
			"autoScale"     	: false,
			"titlePosition"		: "inside",
			"type"				: "iframe"
		});
	});
	$("a.fancybox").each(function(){
		$(this).fancybox({
			"width"				: 700,
			"height"			: 400,
			"autoScale"     	: false,
			"titleShow"			: false,
			"type"				: "iframe"
		});
	});
	$("a.showLarge").each(function(){
		$(this).fancybox({
			"transitionIn"		: "elastic",
			"transitionOut"		: "elastic",
			"type"				: "image", 
			"titlePosition"    	: "over"
		});
	});
	$("a").focus(function(){
		this.blur();
	});
	
	$("ul.roundAbout3d").each(function(){
		var elm = $(this);
		var speed = elm.attr("speed");
		elm.roundabout({
			shape: "square", 
			minOpacity: 0.0,
			minScale: 0.8
		});
		// ANIMATE IF SPEED IS SET
		if(speed > 0)
			setInterval(function(){ elm.roundabout_animateToNextChild(); }, speed)
	});
	/* init slideshow cycle */
	$("img[rel=slideshow]").each(function(){
		var img = $(this);
		var div = img.parent();								  
		div.css({"width" : img.width(), "height" : img.height()});
		div.cycle({
			fx 				: "fade",
			timeout			: parseInt(img.attr("speed"))
		});
	});
});

$(window).load(function(){
	/* auto size divs, if there's a container columns */
	ResizeColumns();
});

function ResizeColumns() {
	var elm = $("div.columns");
	var maxheight = 0; var newheight = 0;
	$(elm).children().each(function(){
		$(this).css("height", null);
		newheight = $(this).height();
		if(maxheight < newheight) {
			maxheight = newheight;
		}
	});
	$(elm).children().each(function(){
		if(!$(this).hasClass("clear")) {
			$(this).height(maxheight);
		}
	});
}

function popw(url) {
	var winHeight = 400; var winWidth = 600;
	var winLeft = (window.screen.width/2)-(winWidth+10);
	var winTop = (window.screen.height/2)-(winHeight+50);
	var winID = (new Date()).getTime();
	var winProps = "width="+winWidth+",height="+winHeight+",left="+winLeft+",top="+winTop+",screenX="+winLeft+",screenY="+winTop+",scrollbars=yes,menubar=no,toolbar=no,location=no,directories=no";
	var win = window.open(url, winID, winProps); 
	if(win) win.focus();
	return false;
}

function swapImage(elmId, fileId) {
	var elm = $("#" + elmId);
	elm.css("cursor", "pointer");
	elm.attr("src", "/file" + fileId + ".jpg");
	elm.click(function(){
		$.fancybox({
			"href"			: "/file" +fileId + ".jpg",
			"autoScale"   	: true,
			"transitionIn"  : "none",
			"transitionOut" : "none",
			"type"    		: "image"
		 });		
	});
}
