var totalSlides = 3; // total number of slides.
var slideWidth = 620; // width of each slide.
var menuWidth = 192; // width of each menu.
var currentSlide = 1;
var lo = 0;
var winw = 0;
var curMenu = null;
var previouslyClicked = null;
function next(index){
	index++;
	if(index == $('div.active ul li').length) index--;
	var leftOffset = ( winw - $('div.active ul li').eq(index).innerWidth() ) / 2;
	$("html,body").animate({
		scrollLeft: ($('div.active ul li').eq(index).offset().left - leftOffset) + 'px'
	}, "slow", "swing");
	return false;
}
function prev(index){
	if(index < 0) index = 0;
	var leftOffset = ( winw - $('div.active ul li').eq(index).innerWidth() ) / 2;
	$("html,body").animate({
		scrollLeft: ($('div.active ul li').eq(index).offset().left - leftOffset) + 'px'
	}, "slow", "swing");
	return false;
}
function menu(index,ref){
	var i = null;
	if(ref != previouslyClicked){
		$("li.selected").removeClass('selected');
		$('li.parent').removeClass('selected');
		if(ref.parentNode.id == 'home'){
			$('ul#ul_'+(index+1)+' li').eq(1).addClass('selected');
		}
		$(ref).addClass('selected');
		//if(previouslyClicked != null) $(previouslyClicked).removeClass('selected');
		previouslyClicked = ref;
	}
	if(index != -1){
		if(!isNaN(index)) index++;
		i = index;
		
		$('div.video').empty();
			if(!isNaN(index)) var ind = index+'_1';
			else var ind = index;
			var vdo = $('div#menu_' + ind + ' div.video'); 
			if(vdo.length){
				$("li:has('div.video')").width(580);
				$("div.bottom-shadow").eq(1).empty().append('<img src="images/bb-shadow.png" alt="" width="564" height="8" border="0" />');
			//player
			var f = vdo.attr('title');

			var player = '<object id="player1" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9.0.115" width="572" height="400"><param name=bgcolor value="#000000"><param name=movie value="videos/flvplayer.swf"><param name=allowfullscreen value="true"><param name=allowscriptaccess value="always"><param name=wmode value="opaque"><param name="flashvars" value="file='+f+'&fullscreen=true&controlbar=bottom&backcolor=0xc2986a"><embed name="player1" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="opaque" width="572" height="400" bgcolor="#000000" src="videos/flvplayer.swf" allowfullscreen="true" allowscriptaccess="always" flashvars="file='+f+'&fullscreen=true&controlbar=bottom&backcolor=0xc2986a"></embed></object>';
			vdo.append(player);
			}
	}
	if(!isNaN(index)){ // 1, 2, 3
	var move = true;
		if(index > 0){
			if($('ul#ul_'+(index)+' li').length == 1)
				move = false;
		}
		if(index < 0) index = 0;
		if(move){
			$('#menus').animate({
				left: -(menuWidth * (index)) + "px",
				top:"90px"
			}, "normal", "swing", 
			function(){ // callback fn of animation
				if(i){
					//bind('menu_'+ i);
					//bind('menu_'+i+'_1');
				}
			});
			if(i) bind('menu_'+i+'_1');
		} else if(i){ bind('menu_'+ i);}
	} else if(i){
		bind('menu_'+ i);
	}
}
function getID(id){
	return id.split('_').pop();
}
function bind(id){
	curMenu = id;

	$('.active').removeClass().addClass('inactive').css('display','none');
	
		$('#'+curMenu).removeClass().addClass('active').fadeIn("slow");	
	
	//$('div#'+curMenu).fadeIn('slow');
	
	if(curMenu == 'menu_0'){
		window.scrollTo(0,0);
	}
	
	var t = $('div#'+curMenu+' ul li').length;
	var w = (t * slideWidth) + 270;
	var winh = $(window).height();
	var mt = (winh - 408)/2;
	if(curMenu == 'menu_9') w = 1800; // special
	else if(curMenu == 'menu_10') w = 4035;
	winw = $(window).width();
	lo = (winw - slideWidth) / 2;
	var ml = (winw - 830) / 2;
	if(curMenu == 'menu_0' || $('div#'+curMenu+' ul li').length == 1){
		lo = 0;
	}
	$('#wrapper').css({width:w+'px', marginLeft:ml+'px', marginTop:mt+'px'});
	
	$('#'+curMenu+" .imgHolder img:not(.loaded)").each(function(){
		if(this.complete)
			$(this).addClass('loaded').fadeIn('slow');
	});
}

function goHome(){
	window.scrollTo(0,0);
	/*$('#'+curMenu).removeClass().addClass('inactive').hide();
	
	$('#'+curMenu).addClass('active').fadeIn("slow");*/
	
	$.ajax({
			url: 'contents/index.php',
			success: function(d){
				$('div#container').empty().append(d).hide().fadeIn("slow");
				$('#wrapper').width(850);
				if(previouslyClicked != null) $(previouslyClicked).removeClass('selected');
			}
		});
	$('#menus').animate({
		left:"0px",
		top:"90px"
	}, "normal", "swing");
}
function fix(){
	$('div.right-shadow:empty').append('<img src="images/rr-shadow.png" alt="" width="8" height="392" border="0" />');
	$('div.bottom-shadow:empty').append('<img src="images/bb-shadow281.png" alt="" width="282" height="8" border="0" />');
	$('div.r-corner:empty').append('<img src="images/r-corner.png" alt="" width="8" height="8" border="0" />');
	$('div.l-corner:empty').append('<img src="images/l-corner.png" alt="" width="8" height="8" border="0" />');
	$('div.t-corner:empty').append('<img src="images/t-corner.png" alt="" width="8" height="8" border="0" />');	
}
$(function(){
	if($.browser.msie && parseInt($.browser.version.substr(0,1)) <= 5) window.location = 'old-browser.html';
	
	fix();
	
	curMenu = $('div#container div:first').attr("id"); //'menu_0';
	$('.imgHolder img').hide();	
	bind(curMenu);
	$('#logo').click(goHome);
	$('#menus').css({width: ($('#menus .w200').length * menuWidth)+'px'});
	$('div.active img.next').live('click', function(){
		var index = $('div.active img.next').index(this);
		next(index);
	});
	$('div.active img.prev').live('click', function(){
		var index = $('div.active img.prev').index(this);
		prev(index);
	});
	$('div.active img.back').live('click', function(){
		prev(0);
	});
	
	$('ul#home li a').live('click', function(){
		var index = $('ul#home li').index(this.parentNode);
		var ref = this.parentNode;
		var href = $(this).attr("href");
		$.ajax({
			url: 'contents/'+href,
			success: function(d){
				$('div#container').empty().append(d).hide().fadeIn("slow");
				fix();
				menu(index,ref);
			}
		});
		return false;
	}).focus(function(){$(this).blur();});
	
	$('ul.other > li.parent a').live('click', function(){
		return false;
		/*var index = $('ul.other > li.parent').index(this.parentNode);
		var ref = this.parentNode;
		var href = $(this).attr("href");
		$.ajax({
			url: 'contents/'+href,
			success: function(d){
				$('div#container').empty().append(d).fadeIn("slow");
				fix();
				menu(index,ref);
			}
		});
		return false;*/
	}).focus(function(){$(this).blur();});
	
	$("ul.other").each(function(i){
		var id = getID(this.id);
		$("ul#ul_" + id + " li.sub a").live('click', function(){
			var index = $("ul#ul_" + id + " li.sub a").index(this);
			var ref = this.parentNode;
			var href = $(this).attr("href");
			$.ajax({
			url: 'contents/'+href,
			success: function(d){
				$('div#container').empty().append(d).hide().fadeIn("slow");
				fix();
				menu(id+'_'+(index+1),ref);
				}
			});
			return false;
		}).focus(function(){$(this).blur();});
	});
	$('a.slideBack').live('click', function(){
		menu(-1,this);
	});
	$(".imgHolder img:not(.loaded)").load(function(){
		$(this).addClass('loaded').fadeIn('slow');
	});
	$('#wrapper').fadeIn('slow');

	$(".imgHolderHome div img").live("click", function(){
		var indx = $(".imgHolderHome div img").index(this);
		$('ul#home li a:eq('+indx+')').trigger("click");
	});
});