function nav_overs(id,h,spd){
	$('#'+id+' ul li.link img').hover(function() {
 		$(this).animate({ marginTop: -h }, spd);	
	}, function() {
		$(this).animate({ marginTop: -0 }, spd);          
	});	
}

function checkPos(flg){
	if(flg == 'passive'){
		var lpos = parseInt($("#portfolio").css('marginLeft'));
		var ttlcol = $("#portfolio div.col").not('.detail').size();
		var rpos = (((ttlcol-3)*-312) - 458);
		if(lpos <= -770){ $("#left-arrow").show(); } else { $("#left-arrow").hide(); }
		if(lpos > -458){ $("#portfolio").animate({"marginLeft": "-458px"}, 100, "easeOutQuad", function(){checkPos();}); }
		
		var d = $("#portfolio .col:last").position();
		var dpos = d.left;
		
		//check if a detail panel is open
		if(dpos == ((ttlcol*312)+312)){
			if(lpos >= (rpos +312)){ $("#right-arrow").show(); } else { $("#right-arrow").show(); }
		} else {
			if(lpos <= rpos){ $("#right-arrow").hide(); } else { $("#right-arrow").show(); }
		}
		
		
	} else {
		$('#detail').remove();
		$(".item").removeClass('transparent');
		
		var lpos = parseInt($("#portfolio").css('marginLeft'));
		var ttlcol = $("#portfolio div.col").not('.detail').size();
		var rpos = (((ttlcol-3)*-312) - 458);
				
		if(lpos <= -770){ $("#left-arrow").show(); } else { $("#left-arrow").hide(); }
		if(lpos <= rpos){ $("#right-arrow").hide(); } else { $("#right-arrow").show(); }
		if(lpos > -458){ $("#portfolio").animate({"marginLeft": "-458px"}, 100, "easeOutQuad", function(){checkPos();}); }
		if(lpos < rpos){ $("#portfolio").animate({"marginLeft": rpos+"px"}, 100, "easeOutQuad", function(){checkPos();}); }
	}
}

function addDetail(eid,id){
	$("#"+eid).addClass('transparent');
	$(".item").not("#"+eid).removeClass('transparent');
	$("#"+id).remove();
	
	$.ajax({
		type: "POST",
		url: "ajax.get-detail.php",
		data: 'guid='+eid,
		cache: false,
		success: function(msg){
			//alert(msg);
			$("#"+eid).parent(".col").after(msg);
			
			$('a.close-detail').bind('click', function(){ var pid = $(this).parent('li').parent('ul').parent('div.detail').attr("id"); $('#detail').remove(); closeDetail(); });
			$('a.pager-detail').bind('click', function(){  $('a.pager-detail').not(this).parent('li').removeClass('act'); $(this).parent('li').addClass('act'); var index = $(".detailnav .pager-detail").index(this); galleryPager(index); });
	
			var ppos = parseInt($("#portfolio").css('marginLeft'));
			var d = $("#detail").position();
			var dpos = d.left;
	
			var ofst = Math.abs(ppos) - 458;
			var mamt = dpos - ofst - 312;
	
			$("#portfolio").animate({"marginLeft": "-="+mamt+"px"}, 200, "easeOutQuad", function(){ checkPos('passive'); });
			
			var pgttl = $('#detail .detailtext h3').text();
			document.title = 'This is Tim - Web Design & Development / '+pgttl;
		
		}
	});
}


function closeDetail(){
	$(".item").removeClass('transparent');
	var lpos = parseInt($("#portfolio").css('marginLeft'));
	var ttlcol = $("#portfolio div.col").not('.detail').size();
	var rpos = ((ttlcol*-312) + 478);
	
	if(lpos < rpos){ $("#portfolio").animate({"marginLeft": rpos+"px"}, 200, "easeOutQuad", function(){ }); } else { $("#portfolio").css('marginLeft',lpos+'px'); }
}

function galleryPager(nm){
	var lpos = nm*-594;
	$('#detail-gallery').animate({"marginLeft": lpos+"px"}, 200, "easeOutQuad", function(){ });
}

function loadProject(guid){
	addDetail(guid,"detail");
}

function wipePanel(dir){
	if(dir == 'right'){
		$("#portfolio").animate({"marginLeft":"+=312px"},200,"easeOutQuad",function(){checkPos();});
	} else {
		$("#portfolio").animate({"marginLeft":"-=312px"},200,"easeOutQuad",function(){checkPos();});
	}
}

function isiPad(){
    return (navigator.platform.indexOf("iPad") != -1);
}

$(document).ready(function(){
	nav_overs('featured','26',0);
	$(".item").hover(function(){ $(this).addClass('act'); }, function(){ $(this).removeClass('act'); });
	$("#right-arrow").click(function(){ $("#portfolio").animate({"marginLeft": "-=312px"}, 200, "easeOutQuad", function(){checkPos();}); });
	$("#left-arrow").click(function(){ $("#portfolio").animate({"marginLeft": "+=312px"}, 200, "easeOutQuad", function(){checkPos();}); });
	$("#left-arrow").hide();
	$(".item").click(function(){ var eid = $(this).attr("id"); addDetail(eid,"detail");  });
	$("#emaillink").hover(function(){ $(this).attr('href','mailto:tim@thisistim.com'); }, function(){ $(this).attr('href',''); });
	if(isiPad()){
	$("#herowrap").touchwipe({
    	wipeLeft: function(){ wipePanel("left"); },
     	wipeRight: function(){ wipePanel("right"); },
     	min_move_x: 20,
     	min_move_y: 200,
     	preventDefaultEvents: true
	});
	}
});
