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 img_no = 0;
var img_tot = 0;
var listW = 0;
function init() {
	$(document).ready(function(){
		resize_bg();
		switch (page) {
			case "about_dlm":
			case "production":
				setup_text(420);
			break;
			case "contact_us":
				setup_text();
			break;
			default:
				setup_text();
				setup_gallery();
			break;
		}
		
		fade_topbar();
		$(window).bind("scroll",move_panel);
	});					
	$(window).resize(function() {
	  	resize_bg();
		if (browser=="") {
			move_panel();
		}
	});
	
}
function setup_text(my_w) {
	if ($('#img_list .txt').length>0) {
		var txt = $('#img_list .txt').html();
		$('#img_list .txt').html('');
		txt_arr = txt.split(' ');
		var old_txt = "";
		var my_txt = "";
		var col_no = 0;
		var max_h = 360;
		var col_w = 210;
		if (my_w) {
			col_w = my_w;
		}
		$('#img_list .txt').append('<div class="col"></div>');
		$('#img_list .txt .col').width(col_w-parseInt($('#img_list .txt .col').css('padding-right')));
		$('#img_list .txt').width($('#img_list .txt .col').length*col_w);
		
		for (var i=0;i<txt_arr.length;i++) {
			my_col = $('#img_list .txt .col').eq(col_no);
			my_txt = txt_arr[i]+' ';
			my_col.html(old_txt+my_txt);
			if (my_col.height()>max_h) {
				my_col.html(old_txt);
				$('#img_list .txt').append('<div class="col">'+my_txt+'</div>');
				$('#img_list .txt .col').width(col_w-parseInt($('#img_list .txt .col').css('padding-right')));
				col_no++;
				$('#img_list .txt').width($('#img_list .txt .col').length*col_w);
				old_txt = my_txt;
			} else {
				old_txt += my_txt;	
			}
		}
		$('#img_list .txt').append('<div class="clear"></div>');
		$('#img_list .txt').each(function() {
															listW+=$(this).width()+parseInt($(this).css('margin-right'));
															$('#img_list').width(listW);
															if (listW+240>canvasW) {
																$('#gradient').width(listW+240);
															} else {
																$('#gradient').width(canvasW);
															}
														  });
	}
}
function setup_gallery() {
	if ($('#topbar .page').length==0) {
		$('#topbar').append('<div class="page"></div>');
		img_tot = $('#img_list a').not('.link').length;
		if (browser!="") {
			$('#topbar').css('position','absolute').css('right','auto');
		}
	}
	if ($('#bottombar .but_menu').length==0) {
		//if (browser!="") {
			$('#bottombar').append('<div class="but_menu">&lt;&lt; Menu &nbsp; |</div>');
			$('#bottombar .but_menu').bind('click', function() {
													//$(window).scrollLeft(0);
													//if (browser!="") {
														//$(window).scrollLeft(0);
														//$('body').animate({scrollLeft:0},500, function() {fade_topbar();});
													//} else {
														$('#topbar, #bottombar').hide();
														$('body, html').animate({scrollLeft:0},2000, 'easeInOutQuad', function() {fade_topbar();});
													//}
													
													});
		//}
	}
	$('#img_list a').not('.link').eq(0).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 src = $(this).attr('href');
										// var thumb = src.substr(0,src.length-4)+'.jpg';
										 var thumb = src;
										 var img = new Image();
										// imgH = canvasH-parseInt($('#img_list').css('margin-top'))-parseInt($('#img_list').css('margin-top'));
										 imgH = 370;
										 imgW = Math.round(oriW/oriH*imgH);
										 
										 var  my_li = $(this).parent();
										 my_li.append('<div class="loader"></div>');
										 my_li.append('<div class="img_box"></div>');
										 $('.img_box',my_li).append(img).width(imgW).height(imgH);
										 $('.loader',my_li).height(imgH);
										 my_li.width(imgW).height(imgH+149);
										 
										 
											listW += imgW+parseInt(my_li.css('margin-right'));
											//$('#img_list>li').each(function(){listW+=$(this).width()+parseInt($(this).css('margin-right'));});
											$('#img_list').width(listW);
											if (listW+240>canvasW) {
												$('#gradient').width(listW+240);
											} else {
												$('#gradient').width(canvasW);
											}
	
										 
										   $(img).attr('src', thumb+'?'+Number(new Date())).css('width', imgW).css('height', imgH).addClass(my_class).hide()
											.load(function () { 
												var clone_img = $(this).clone().addClass('reflected').css('top',imgH);
												$(this).parent().append(clone_img);
												$(this).parent().parent().find('a').not('.link').remove();
											  
												img_no++;
												$('#topbar .page').html('Loading, '+img_no+'/'+img_tot+' images loaded >>>');
												if (img_no==img_tot) {
													$('#topbar .page').fadeOut('',function(){$('#topbar .page').remove()});
												}
												
												if (browser=="") {
													$(this).fadeIn('fast',function(){ $(this).parent().find('.loader').remove();setup_gallery();});
												} else {
													$(this).show().parent().parent().find('.loader').remove();setup_gallery();
												}
											  
												clone_img.show();
											});
										
											
										 });		

}


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 move_panel() {
		if (timer) {
			clearTimeout(timer);
		}
			$('#topbar, #bottombar').hide();
			var timer = setTimeout("fade_topbar()",100);
		
}
function fade_topbar() {
		scrollL = $(window).scrollLeft();
		$("#bottombar").css('left',scrollL+$(window).width()-$('#bottombar').width()-10);
		$("#topbar").css('left',scrollL+$(window).width()-$('#topbar').width()-10);
	$('#topbar, #bottombar').fadeIn();
	$('#bottombar .but_menu').show();
		if ($(window).scrollLeft()==0) {
	$('#bottombar .but_menu').hide();
	
		}
}
function resize_bg() {
		canvasW = $(window).width();
		canvasH = $(window).height();	
		if (canvasW<640) {
			canvasW=640;
		}
		if (canvasH<$('#canvas').height()) {
			canvasH=$('#canvas').height();
		}
		/*
		if (canvasH>370) {
			canvasH=370;
		}
		*/
		var myY = (canvasH-$('#canvas').height())/2
		$('#canvas').css('margin-top',myY);
		$('#bottombar').css('bottom',-myY+10);
		
		
		if (listW+240<canvasW) {
			$('#gradient').width(canvasW);
		} else {
			$('#gradient').width(listW+240);
		}
		/*
		imgH = 370;
		imgW = Math.round(oriW/oriH*imgH);  
		var listW = 0;
		$('#img_list>li img').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];
										imgW = Math.round(oriW/oriH*imgH); 
										$(this).parent().width(imgW).height(imgH);
										$(this).width(imgW).height(imgH);
										listW+=$(this).parent().width()+parseInt($(this).parent().css('margin-right'));
										});
		$('#img_list').width(listW);
		*/
}
init();
//resize_bg();


