var anchor;
var old_anchor;
var valid_anchors = new Array(); 

$(document).ready( function() {
	if (window.location.pathname.substring(0,10) == "/portfolio") {
		//Init AJAX anchor checker
		setInterval("load_port_pages()", 300);
	}
});


/* * * MAIN LOADING FUNCTION * * */

function load_port_pages(){
	hash = document.location.hash.substring(1); 
	if(anchor != hash){
		old_anchor = anchor; 
		anchor = hash;
		if (!anchor) {
			url = '/portfolio/0'; 
		} else {
			url = '/portfolio/' + anchor.replace(':','/');
		}
		
		$("#loading").show();
		
		//Load project
		$.ajax({
			type: "GET",
			url: url,
			beforeSend: function(data){
				$("#port_content").fadeOut(200);
				$("#port_loading").show();
			}, 
			success: function(data){
				$('#port_content').html(data);
			}, 
			complete: function(){
				act_id = $('#act_prj').attr('rel'); 
				$('.proj_tn_current').removeClass('proj_tn_current'); 
				$('#prj_tn-'+act_id).addClass('proj_tn_current'); 
				
				load_prj_tn(act_id); 
				
				$("#port_loading").hide();
				$("#port_content").fadeIn(200);
			}
		});
		
	}
}
	
/* * * CHILD LOADING FUNCTIONS * * */

//Load section thumbnails (index)
function load_sec_tn(id) {
	
	curr_class= 'sec_id-' +id; 
	old_class = $('#port_slider').attr('rel'); 
	
	if (curr_class != old_class) {
		$('#port_slider').attr('rel', curr_class); 
		
		url = "/sections/index/" + id; 
		
		//Load section index (projects)
		$.ajax({
			type: "GET",
			url: url,
			beforeSend: function(data){
				$("#port_slider").slideUp(400);
				$("#port_slider_loading").show();
			}, 
			success: function(data){
				$('#port_slider').html(data);
			}, 
			complete: function() {init_sec(id)}
		});
	}
}

//Load current project thumbnails
function load_prj_tn(id) {
	data = $.getJSON(
		'/images/getJSON/' + id, 
		function (data) {
			if (data.length > 1) {
				$.each(data, function(i,item){
					//alert(item.Image); 
					if (item.Image.type == 'image') {
						img_url = item.Image.url; 
					} else {
						img_url = 'video.png'; 
					}
					
					//*
					$("#port_prj_slider").append(
						'<li id= "proj_img_tn-' + 
						item.Image.id + '"><img src= "/img/usr/projects/tn/' + 
						img_url + '" />' + 
						'<a class= "proj_img_tn_overlay" href= "javascript:port_img(\'' + 
						item.Image.id + '\', \'' + 
						i + '\')">' + 
						//item.Image.id + '\')">' + 
						'</a></li>'
					);
				}); 
				$('#port_prj_slider li:first').addClass('proj_img_tn_current'); 
			}; 
			
			load_prj_img(data); 
		}
	);
}

//Initialize section 
function init_sec(id) {
	//*
	//Change header image
	sec_name = $('#act_sec_name').attr('rel'); 
	sec_id = $('#act_sec').attr('rel'); 
	header_img = $('#cat_header_wrap #'+id); 
	
	$('#cat_header_wrap li').fadeOut(300); 
	header_img.delay(300).fadeIn(300); 
	/*
	$('#cat_header_wrap li').fadeOut(300)
	.parent().find('#'+id)
	.fadeIn(300); 
	/* */

	//Highlight section submenu
	$('#port_submenu li a').removeClass('current'); 
	$('#submenu_sec-'+ id).addClass('current');
	/* */
	
	//Highlight project icon
	act_id = $('#act_prj').attr('rel'); 
	$('.proj_tn_current').removeClass('proj_tn_current'); 
	$('#prj_tn-'+act_id).addClass('proj_tn_current'); 
	
	//Move slider position to current project 
	var max_tn = 5;
	var n_tn = $(".prj_icon").length; 
	var curr_idx = $('.prj_icon').index($('.proj_tn_current')); 
	var padding = 5; 
	
	if (n_tn > max_tn) {
		unit_h = $('.proj_tn_current').outerHeight(); 
		tn_pos = Math.floor($('.proj_tn_current').position().top); 
		//var next = $('.proj_tn_current ~ li').length; 
		var next = n_tn - curr_idx - 1; 
		if (next<max_tn) {
			tn_pos -=  unit_h * (max_tn-1-next);
		}
		
	} else {
		tn_pos = 0; 
	}
	//alert("No. of tn's: " + n_tn + "; Next: " + next + "; loading goal: " + -tn_pos); 
	$("#port_slider").css('top', -tn_pos + padding); 
		
	
	//Show/hide scrolling arrows
	if (n_tn > 4 && next >= 4 ) {
		$('#port_slider_next').show(); 
	} else {
		$('#port_slider_next').hide(); 
	}
	
	if (parseInt($('#port_slider').css('top'))<0) {
		$('#port_slider_back').show(); 
	} else {
		$('#port_slider_back').hide(); 
	}
	
	//Finalize loading
	$("#port_slider_loading").hide();
	$("#port_slider").slideDown(400);
}
	


//Load current project images

function load_prj_img(data) {
	$('#port_media_slider').empty(); 
	$.each(data, function(i,item){
		if (item.Image.type == 'image') {
			if( $('#act_prj_url').attr('rel') ) {
				$('#port_media_slider').append( 
					'<li><a href= "http://' + $('#act_prj_url').attr('rel') + 
					'" target= "_blank"><img src= "/img/usr/projects/' + 
					item.Image.url + '" /></a></li>'
				); 
			} else {
				$('#port_media_slider').append( 
					'<li><img src= "/img/usr/projects/' + 
					item.Image.url + '" /></li>'
				); 
				//img_loader('/img/usr/projects/' + item.Image.url); 
			}
		} else if (item.Image.type == 'movie') {
			$('#port_media_slider').append('<li><div id= "video_swf"></div></li>'); 
			load_video(item.Image.url); 
		} else {
			$('#port_media_slider').append('<li><div id= "video_swf">' + item.Image.external + '</div></li>'); 
		}; 
	}); 
}


//Load video

function load_video(url) {
	var flashvars = {
		file: '/video/usr/' + url, 
		bgcolor: 'black', 
		autostart: 'true', 
		stretching: 'uniform'
	};
	var params = {
		allowfullscreen: 'false', 
		wmode: 'opaque', 
		allowscriptaccess: 'always'
	};
	var attributes = {
		id: 'video_swf'
	};
	
	swfobject.embedSWF( "/swf/mplayer.swf", "video_swf", "544", "325", "9", "expressInstall.swf", flashvars, params, attributes);
}

