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";
var list_no = 0;
var slide_interval = 2000;
function init() {
	$(document).ready(function(){
		$('#canvas').show();				
		//resize_bg();
		switch (template) {
			case "home":
				setup_slideshow();
			break;
			case "gallery":
				if (browser == "") {
				setup_gallery();
				}
			break;
			default:
				
			break;
		}
		if (browser != "") {
			$('#menubar').css('position','absolute');
		}
		resize_bg();
	});
	$(window).resize(function() {
	  	resize_bg();
	});
	
}
function setup_slideshow() {
	$('#slideshow li').eq(list_no).each(function() {
										 var my_class=$(this).attr('class');
										 var info=$(this).attr('class').split('-');
										 var type = info[0];
										 var oriW = info[1];
										 var oriH = info[2];
										 switch (type) {
											 case "img":
											 	if ($(this).find('img').length==0) {
													$(this).show();
													var my_a = $(this).find('a').eq(0);
													 var  my_li = $(this);
													 var src = my_a.attr('href');
													 var thumb = src;
													 if (src.substr(src.length-4)!=".gif") {
														 thumb = src.substr(0,src.length-4)+'.jpg';
													 }
													 var img = new Image();
													 imgH = canvasH-parseInt($('#slideshow').css('margin-top'))-parseInt($('#slideshow').css('margin-top'));
													 imgW = Math.round(oriW/oriH*imgH);
													 my_li.append('<div class="loader" style="width:'+imgW+'px;height:'+imgH+'px"></div>');
													 
													 my_li.append(img).width(imgW).height(imgH);
													 
													   $(img).attr('src', thumb+'?'+Number(new Date())).css('width', imgW).css('height', imgH).addClass(my_class).hide()
														.load(function () { 
															$('#slideshow li img').not(':hidden').fadeOut('slow');
														  $(this).fadeIn('slow',function(){$(this).parent().find('.loader').remove();setTimeout("setup_slideshow()",slide_interval);});
														  
														});
													  my_li.find('a').remove();
												} else {
													var my_img = $(this).find('img').eq(0);
													$('#slideshow li img').not(':hidden').fadeOut('slow');
													my_img.fadeIn('slow',function(){$(this).parent().find('.loader').remove();setTimeout("setup_slideshow()",slide_interval);});
												}
												if (list_no<$('#slideshow li').length-1) {
													list_no++;
												} else {
													list_no = 0;
												}
											 break;
										 }
												});
}

function setup_gallery() {
	$('#img_list li').each(function() {
										 var my_class=$(this).attr('class');
										 var info=$(this).attr('class').split('-');
										 var type = info[0];
										 var oriW = info[1];
										 var oriH = info[2];
										var  my_li = $(this);
										 switch (type) {
											case "mov":
													my_li.css('background','none');
													var src = my_li.find('a').attr('href');
													var thumb = my_li.find('img').attr('src');
													my_li.find('a').attr('onclick','return false');
													$('.but_play',my_li).bind('click',function() {
																						   $('.media_canvas').remove();
																						   my_li.find('.img_box').append('<div class="media_canvas"><div class="media_box"><div id="media"></div></div></div>');
																						   $('.media_canvas').hide().fadeIn();
																						   //alert($(this).width());
																						   $('.media_box').width($(this).width()).height($(this).height()).css('margin-left',-$(this).width()/2).css('margin-top',-$(this).height()/2).css('top','50%').css('left','50%');
																						   setup_video(src,thumb,$(this).width(),$(this).height());
																						   });
													$('.but_play',my_li).bind('mouseenter',function() {
																						   $('img',$(this).parent()).animate({opacity:0.5});
																						   });
													$('.but_play',my_li).bind('mouseleave',function() {
																						   $('img',$(this).parent()).animate({opacity:1});
																						   });
											break;
										 }
										
											
										 });
	var listW = 0;
	$('#img_list>li').not(':hidden').each(function(){listW+=$(this).width()+parseInt($(this).css('margin-right'));});
	$('#img_list').width(listW);
	list_no++;
	
	$('.but_back').bind('click', function() {
												$('body, html').animate({scrollLeft:0},2000, 'easeInOutQuad', function() {});
											});
}


function setup_video(mov_path,img_path,w0,h0) {
$('#media').append('<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('videoplayer.swf', 'media', w0, h0, '10', 
'expressinstall.swf', {img_path:img_path,mov_path:mov_path,auto_start:true}, {allowscriptaccess: 'always', bgcolor: '#000000', menu: 'false', wmode: 'opaque',allowFullScreen:'true'}, {id: 'media'},function(){$('.img_layer .loader').remove();});
}

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 resize_bg() {
		canvasW = $(window).width();
		canvasH = $(window).height();	
		if (canvasW<640) {
			canvasW=640;
		}
		if (canvasH<480) {
			canvasH=480;
		}
		if (canvasH>800) {
			canvasH=800;
		}
		
		if ($('#img_list').length>0 || $('#slideshow').length>0) {
			if ($('#img_list').length>0) {
				imgH = canvasH-parseInt($('#img_list').parent().css('margin-top'))-parseInt($('#img_list').parent().css('margin-top'));
			} else if ($('#slideshow').length>0) {
				imgH = canvasH-parseInt($('#slideshow').css('margin-top'))-parseInt($('#slideshow').css('margin-top'));
			}
			imgW = Math.round(oriW/oriH*imgH);  
			var listW = 0;
			$('#img_list li img, #slideshow li img').each(function(){
											 var my_class=$(this).parent().attr('class');
											 var info=$(this).parent().attr('class').split('-');
											 var type = info[0];
											 var oriW = info[1];
											 var oriH = info[2];
											imgW = Math.round(oriW/oriH*imgH);
											$(this).parent().width(imgW).height(imgH);
											if (my_class!="seperator") {
												$(this).width(imgW).height(imgH);
											}
											});
			//var listW = 0;
			//$('#img_list>li').not(':hidden').each(function(){listW+=$(this).width()+parseInt($(this).css('margin-right'));});
			//$('#img_list').width(listW);
			
		}
		if ($('#menu').height()+170>canvasH) {
			$('#menubar').height($('#menu').height()+170);
		} else {
			$('#menubar').height(canvasH);
		}
		/*
		contentW = 0;
		imgH = 340;
		$('#img_list .img_box img').each(function(){
										 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')));
		$('#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);
		*/
}
init();
//resize_bg();
