var timer = null;
var leftright = '';
var step = 22;
var currentPosition = 0;
var width = 0;
var gwt = null;
var cntr = 0;


$(document).ready(function(){

	if($("a.fancybox").length)
		$("a.fancybox").fancybox();
		
	resizeHome();
	$(window).resize(function(){
		resizeHome();
	});

	//fix for HTML5 placeholder which is not yet a standard
	$("input, textarea").each(function(){
		if($(this).attr("placeholder")&&!$(this).val()){
			$(this).val($(this).attr("placeholder"));
		}
	});
	$("input, textarea").focus(function(){
		if($(this).attr("placeholder")&&$(this).val()==$(this).attr("placeholder")){
				$(this).val('');
		}
	});
	$("input, textarea").blur(function(){
		if($(this).attr("placeholder")&&!$(this).val()){
				$(this).val($(this).attr("placeholder"));
		}
	});

	//slider on series page
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))){
		$('#imgs').touch({
			animate: true,
			sticky: false,
			dragx: true,
			dragy: false,
			rotate: false,
			resort: false,
			scale: false
		});
		$('#rightControl').hide();
		$('#leftControl').hide();
	}else{
		getWidth();
		$('#leftControl').hide();
		if(width<665){
			$('#rightControl').hide();
		}
		// Create event listeners for .arrow clicks
		$('.aarrow')
			.bind('mousedown', function(){
						clearTimeout(timer);
						leftright = ($(this).attr('id')=='rightControl') ? 'right' : 'left';
						//run the timer function
						move();
								});
		$('.aarrow')
			.bind('mouseup', function(){
						//clear the timer
						clearTimeout(timer);
								});
	}

	$("#imgs a").click(function(){
		$("#imgs a").removeClass("selected");
		$(this).addClass("selected");
		var id = $(this).attr('id').substr(2, $(this).attr('id').length);
		var pid = $("#portfolioid").val();
		$.post("series.php", {action:'loadImage', id:id, pid:pid}, function(data){
			$("#details").html(data);
			$("a.fancybox").fancybox();
		});
	});

	$(".content .title a.right").click(function(){
		if($(this).html()=="back to series"){
			$(this).html("series description");
			$("#series").show();
			$("#seriesDescription").hide();
		}else{
			$(this).html("back to series");
			$("#series").hide();
			$("#seriesDescription").show();
		}
	});

});

function resizeHome(){
	if($(".home>img").length==1){
		var h = parseInt($(window).height()*0.7);
		$(".home>img").height(h);
	}
}

function getWidth(){
	width = $('#imgs').width();
	cntr++;
	if(width>200&&cntr>2){
		clearTimeout(gwt);
	}else{
		gwt = setTimeout('getWidth()',100);
	}
}

function move(){
	getWidth();
	if(leftright=='right'){
		currentPosition-=step;
		if(-currentPosition>width-345){
			currentPosition = -width+345;
			$('#rightControl').hide();
			clearTimeout(timer);
		}else{
			$('#imgs').css('left',currentPosition+'px');
			timer = setTimeout(move, 30);
			$('#leftControl').show();
		}
	}else{
		currentPosition+=step;
		if(currentPosition>=0){
			currentPosition = 0;
			$('#imgs').css('left',currentPosition+'px');
			clearTimeout(timer);
			$('#leftControl').hide();
		}else{
			$('#imgs').css('left',currentPosition+'px');
			timer = setTimeout(move,30);
			$('#rightControl').show();
		}
	}
}

function submitFrm(id){
	$("#"+id).submit();
}
