var canvasH,canvasW,documentW,documentH,imgH,imgW,oriH,oriW,menuH,scrollL,lastL,touchX,touchY,contentW = 0;
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
var timer;
var menu_status = "close";
function init() {
	$(document).ready(function(){
		$('#canvas').show();				
		canvasH = $(window).height();		
		canvasW = $(window).width();
		documentW = $(document).width();	
		documentH = $(document).height();
		menuH = $('#topbar').height();
		contentW = $('#content').width();
		
		setup_img();
		setup_menu();
		resize_bg();
		//resize_bg();
		//$('#canvas').hide().fadeIn();
		switch (template) {
			case "bio":
			case "contact":
				$('#content').hide().fadeIn();
			break;
			case "home":
			$('#img_frame').append('<div class="loader"></div>');
			$('#img_frame').fadeIn();
			$('#img_frame img').each(function(){									 
												//$(this).removeAttr("width"); 
												//$(this).removeAttr("height");
												$(this).css('opacity',0);
												$(this).load(function(){
													$(this).parent().find('.loader').remove();
													$(this).animate({opacity:1},500);
												});
											 });
			break;
			default:
				
			break;
		}
	});					
	$(window).resize(function() {
	  	resize_bg();
	});
	
	var loader = new Image(); 
	loader.src = "images/loader.gif";
}
function setup_menu() {
	/*
	$('#menu ul ul').hide();
	$('#menu a').bind('click',function() {
							   var elem = $('>ul',$(this).parent());
							   if (elem.length>0) {
								   $('#menu ul ul').hide();
								   if (elem.is(':hidden')) {
								   		elem.show();
								   } else {
									   elem.hide();
								   }
							   		return false;
							   } else {
								   
							   }
							   });
	*/
	//$('#menu ul ul').hide();
	/*
	$('#menu a').each(function() {
							   //alert($(this).href+"|"+filename);
							   //$(this).html($(this).html().replace(' ','<br/>'));
							   });
	*/
	$('#menu a').css('opacity',0.3).attr('rel',0.3);
	$('#menu > ul > li > a').css('opacity',0.5).attr('rel',0.5);
	$('#menu .selected > a').css('opacity',1).attr('rel',1);
	$('#menu ul ul li:nth-child(even):not(.selected) a').removeClass().addClass('even');
	$('#menu a').bind('mouseover',function() {$(this).css('opacity',1)});
	$('#menu a').bind('mouseout',function() {$(this).css('opacity',$(this).attr('rel'))});
	
}
function setup_img() {
	$('#img_list .img_box img').each(function(){
									 var src=$(this).attr('src')+'?'+Number(new Date());
									 /*
									 $(this).addClass('img-'+$(this).width()+'-'+$(this).height());
										$(this).removeAttr("width"); 
										$(this).removeAttr("height");
										*/
									 $(this).attr('src','');
									 $(this).attr('src',src);
										$(this).css('opacity',0);
										$(this).load(function(){
											$(this).parent().parent().find('.loader').remove();
											$(this).animate({opacity:1},500);
										});
										/*
										if ($(this).width()!=0) {
											$(this).parent().parent().find('.loader').remove();
											$(this).animate({opacity:1},500);
										}
										*/
									 });
	$('#img_list .img_box a').bind('mouseover', function() {
												 $('#img_list .img_box a').not(this).css('opacity',0.4);
												 $(this).css('opacity',1);
											});
	$('#img_list .img_box a').bind('mouseout', function() {
											   $('#img_list .img_box a').css('opacity',1);
												// $(this).css('opacity',1);
											});
	$('#img_list .img_box a').bind('click', function() {
											lastL = $(window).scrollLeft();
											var elem = $(this);
											$('#img_list').fadeOut(100, function() {
																				 select_img(elem);
																				 });
											$('#txt_box').fadeOut(100);
											return false;
											});
	$('#img_list .but_box a').bind('mouseover', function() {
												 $(this).css('opacity',1);
											});
	$('#img_list .but_box a').bind('mouseout', function() {
												 $(this).css('opacity',0.4);
											});
	$('#img_list .img_box').append('<div class="loader"></div>');
	$('#img_list .but_box a').css('opacity',0.4);
	
	//$('#img_list .media').append('<div class="but_play"></div>');
	
}
function select_img(elem) {
	var ext_arr = elem.attr('href').split('.');
	var ext = ext_arr[ext_arr.length-1];
	$('#img_frame').fadeIn();
	$('#img_frame').append('<div class="close_but"></div>');
	$('#img_frame').height(canvasH-menuH-parseInt($('#img_frame').css('padding-top'))-parseInt($('#img_frame').css('padding-bottom')));
	$('#img_frame').append('<div class="loader"></div>');
	$('#txt_box,#caption_box').hide();
	switch (ext) {
		case"jpg":
			elem.find('img').clone().attr('src',elem.attr('href')).appendTo('#img_frame');
			
			$('#img_frame img').each(function(){									 
												$(this).removeAttr("width"); 
												$(this).removeAttr("height");
												$(this).css('opacity',0);
												$(this).load(function(){
													$(this).parent().find('.loader').remove();
													$(this).animate({opacity:1},500);
												});
											 });
			
		break;
		case "mov":
			//elem.find('img').clone().attr('src',elem.attr('href')).appendTo('#img_frame');
			
			$('#img_frame').append('<div id="mediaplayer"></div>');
			$('#img_frame').append('<div id="mediaplayer_refresher">s</div>');
			var class_arr =  $('img', elem).attr('class').split("-");
			var img_path = $('img', elem).attr('src');
			var mov_path = $(elem).attr('href');
			var movW = class_arr[1];
			var movH = class_arr[2];
			//var movW = canvasW-120-260;
			//var movH = canvasH-180;
			$('#img_frame #mediaplayer').width(movW).height(movH);
			setup_video(mov_path,img_path,movW,movH);
			//$('#img_frame .media').media({ width: movW, height: movH, autoplay: true, bgColor:'#000000', SCALE:'aspect' });
			$('#img_frame .loader').remove();
		break;
	}
	$('#img_frame .close_but').css('opacity',0.4);
	$('#img_frame .close_but').bind('mouseover', function() {
												 $(this).css('opacity',1);
											});
	$('#img_frame .close_but').bind('mouseout', function() {
												 $(this).css('opacity',0.4);
											});
	$('#img_frame .close_but').bind('click', function() {
													  $('#img_frame').html('');
													  $('#img_frame').hide();
													  $('#img_list').fadeIn();
													  $('#txt_box,#caption_box').fadeIn();
													  resize_bg();
													  resize_bg();
													  $(window).scrollLeft(lastL);
													  clearInterval(timer);
											});
	resize_bg();
	resize_bg();
	
}

function setup_video(mov_path,img_path,w0,h0) {
$('#mediaplayer').html('<embed align="middle" src="'+mov_path+'" controller="true" autoplay="true" type="video/quicktime" cache="true" loop="false" scale="ASPECT" width="'+w0+'" height="'+h0+'" BGCOLOR="BLACK" />');

swfobject.embedSWF(base_path+'videoplayer.swf', 'mediaplayer', w0, h0, '10', 
'expressinstall.swf', {img_path:img_path,mov_path:mov_path,auto_start:true}, {allowscriptaccess: 'always', bgcolor: '#000000', menu: 'false', wmode: 'window',allowFullScreen:'true'}, {id: 'mediaplayer'},function(){resize_bg();});

	 timer=setInterval("refresh_video()",200);
}
function refresh_video() {
		$('#img_frame #mediaplayer_refresher').html(timer);
}
function resize_bg() {
		canvasW = $(window).width();
		canvasH = $(window).height();	
		if (canvasW<640) {
			canvasW=640;
		}
		if (canvasH<240) {
			canvasH=240;
		}
		contentW = 0;
		imgH = 340;
		$('#img_list .img_box img').each(function(){
										 
		/*
										 var class_arr =  $(this).attr('class').split("-");
										 oriW = class_arr[1];
										 oriH = class_arr[2];
										 //imgH = canvasH-menuH-parseInt($('#img_list').css('padding-top'))-parseInt($('#img_list').css('padding-bottom'));
										 imgH = 340;
										 imgW = oriW/oriH*imgH;
										 $(this).width(imgW);
										 $(this).height(imgH);
										 //$(this).css('padding-top',menuH);
										 contentW+=imgW+parseInt($(this).parent().parent().css('margin-right'));
		*/
										 contentW+=$(this).width()+parseInt($(this).css('margin-right'));
										 });
		$('#img_frame img').each(function(){
										 var class_arr =  $(this).attr('class').split("-");
										 oriW = class_arr[1];
										 oriH = class_arr[2];
										 imgH = canvasH-menuH-parseInt($('#img_frame').css('padding-top'))-parseInt($('#img_frame').css('padding-bottom'));
										 imgW = Math.round(oriW/oriH*imgH);
										 $(this).width(imgW);
										 $(this).height(imgH);
										 $(this).css('padding-top',menuH);
											$('#img_frame').width(imgW);
										 });
		$('#img_list').width(contentW+$('#img_list .but_box').width()+10);
		//contentW = $('#img_list').width();
		$('#img_list').css('margin-top',canvasH-menuH-60-imgH);
		$('#img_frame').height(canvasH-menuH-parseInt($('#img_frame').css('padding-top'))-parseInt($('#img_frame').css('padding-bottom')));
		//contentW+=$('#img_list').css('padding-left')+parseInt($('#img_list').css('padding-right'));
		//$('#topbar').width(canvasW);
		/*
		$('#content').width(contentW);
		if (menu_status=="close") {
			$('#canvas').width(contentW);
		} else {
			$('#canvas').width(canvasW);
		}
		$('#content').height(canvasH);
		*/
		$('#menubar').height(canvasH-parseInt($('#menubar').css('padding-top'))-parseInt($('#menubar').css('top'))-60);
		$('#footer').css('top',canvasH-60);
		
		$('#caption_box, #txt_box').width(canvasW-380);
		$('.txt_bio').width(canvasW-380-$('#bio_img').width()-40);
		
}
/*
function panel_opened(){
	menu_status = "open";
	$('#content').hide();
	$('#menubar').fadeIn();
	$(window).scrollLeft(0);
	$('#topbut').html('BACK');
	 $(document).unbind('touchmove');
	$('#canvas').width(canvasW);
	move_panel();
}
function panel_closed(){
	menu_status = "close";
	$('#topbut').html('MENU');
	 $(document).unbind('touchmove');
	move_panel();
}
*/
function select_parent(elem) {
	var my_parent = elem.parent();
	while (my_parent.attr('id')!="menubar") {
		if (my_parent[0].tagName=="LI") {
			my_parent.addClass('parent');
		}
		my_parent = my_parent.parent();
	}
	elem.parent().removeClass().addClass('selected')
}
function reset_menu() {
	$('#menubar ul ul').hide();
	$('#menubar .selected').removeClass();
	$('#menubar a').each(function() {
								  	if ($(this).attr('href').toLowerCase()==filename.toLowerCase()) {
										select_parent($(this));
										if ($('> ul',$(this).parent()).length>0) {
											//$('#menu_dn').html('<ul>'+$('> ul',$(this).parent()).html()+'</ul>');
											$('> ul',$(this).parent()).show();
										} else {
											if ($(this).parent().parent().parent()[0].tagName!="DIV") {
												$('> ul',$(this).parent().parent().parent()).show();
												//$('#menu_dn').html('<ul>'+$('> ul',$(this).parent().parent().parent()).html()+'</ul>');
											}
											//$('#menu_dn').html($('> ul',$(this).parent().parent().parent()).parent().html());
											
										}
									}
								  });
}
init();
//resize_bg();
