$(function(){
	// frontpage slideshow
	var slideitems=$('.slideshow>img');
	if(slideitems.length>0){
		var old_item=0;
		var current_item=0;
		slideitems.eq(0).show();
		if(slideitems.length>1){
			setInterval(function(){
				old_item=current_item;
				current_item=(current_item+1)%slideitems.length;
				slideitems.eq(old_item).fadeOut('slow');
				slideitems.eq(current_item).fadeIn('slow');
			}
			,7000);
		}
	}
	// menu effect
	var menu=$('#menu>ul');
	// stupid IE 6
	if($.browser.msie&&$.browser.version=='6.0'){
		if($('a:contains(" ")','#menu>ul>li:last').length!=0){
			$('#menu>ul>li:last').css({width:$('#menu>ul>li:last>a').text().length*8});
		}
	}
	var hideTimer;
	function timeHide(){
		clearTimeout(hideTimer);
		hideTimer=setTimeout(function(){
			$('#menu>ul>li,#menu>ul>li>ul').fadeOut();
			menu.slideUp('slow');
			$('#menu>ul>li>ul>li').css({top:0});
			$('#menu>.button').fadeIn();
		}
		,6000);
	}
	$('#menu>.button>span').click(function(){
		if(menu.is(':hidden')){
			$('#menu>ul>li').css({display:'block'});
			menu.css({paddingLeft:($(document).width()-menu.width())/2}).slideDown();
			$('#menu>ul>li').hide().fadeIn('slow');
			$('#menu>.button').fadeOut();
			timeHide();
		}else{	
			clearTimeout(hideTimer);
			$('#menu>ul>li,#menu>ul>li>ul').fadeOut();
			menu.slideUp('slow');
			$('#menu>ul>li>ul>li').css({top:0});
			$('#menu>.button').fadeIn();
		}
	});
	$('#menu li').bind('mouseover',function(){
		clearTimeout(hideTimer);
	}).bind('mouseout',function(){
		timeHide();
	});
	$('#menu>ul>li>a:has(+ul)').click(function(){
		$('#menu>ul>li>ul').fadeOut();
		$('#menu>ul>li>ul>li').animate({top:0});
		if($('+ul',this).is(':hidden')){
			$('+ul',this).stop().css({top:$(this).parent().height()}).fadeIn();
			var menuTop=6;
			$('+ul>li',this).each(function(){
				$(this).animate({top:menuTop},{queue:false});
				menuTop+=$(this).height();
			});
		}
		return false;
	});
	// Mouse hover on menu
	// Mouse hover on main menu links
	$('#menu span,#menu li a').hover(function() {
		$(this).animate({color: "#000"}, {duration:500,queue:false});
	}, function () {
		$(this).animate({color: "#888"}, {duration:500,queue:false});
	});
	// marquee effect
	var gallery=$('#gallery>.inner');
	var gallery_as=$('#gallery>.inner>a');
	var gallery_imgs=$('#gallery>.inner>a>img');
	var current_img=0;
	var scrolling=false;
	if(gallery_imgs.length>0){
		gallery.css({left:($(document).width()-gallery_imgs.eq(current_img).width())/2,visibility:'visible'});
		gallery_imgs.each(function(i){
			$(this).parent().bind('mouseover',function(){
				if(!scrolling){
					if(i!=current_img){
						$(this).children('span.protector').animate({opacity:0},{queue:false});
						gallery_as.eq(current_img).children('span.protector').animate({opacity:0.9},{queue:false})
					}
				}
			}).bind('mouseout',function(){
				if(!scrolling){
					if(i!=current_img){
						$(this).children('span.protector').animate({opacity:0.9},{queue:false});
						gallery_as.eq(current_img).children('span.protector').animate({opacity:0},{queue:false});
					}
				}
			});
		});
		gallery_as.eq(current_img).children('span.protector').animate({opacity:0},{queue:false});
		gallery_as.each(function(i){
			$(this).bind('click',function(){
				if(current_img!=i){
					gallery_as.eq(current_img).children('span.protector').animate({opacity:0.9},{queue:false});
					current_img=i;
					scrolling=true;
					gallery.animate({left:($(document).width()-gallery_imgs.eq(current_img).width())/2-gallery_imgs.eq(current_img).offset().left+gallery.offset().left},{duration:'slow',queue:false,
					complete:function(){
						scrolling=false;
						gallery_as.eq(current_img).children('span.protector').animate({opacity:0},{queue:false});
					}
					});return false;
				}else{
					$('.zoom>a.zoomIn').trigger('click');
					return false;
				}
			});
		});
	}
	// zoom & lightBox
	function zoomButtomInit(){
		$('.zoom>a').css({opacity:0.5});
		$('.zoom>a').bind('mouseover',function(){
			if($.browser.msie&&$.browser.version=='6.0'){
				$(this).css({opacity:1});
			}else{
				$(this).animate({opacity:1},{queue:false});
			}
		}).bind('mouseout',function(){
			if($.browser.msie&&$.browser.version=='6.0'){
				$(this).css({opacity:0.5});
			}else{
				$(this).animate({opacity:0.5},{queue:false});
			}
		});
	}
	zoomButtomInit();
	$('.zoom>a.zoomIn').bind('click',function(){
		if($('#background>img').length==0){
			$('body').append('<div id="background"><img id="loading" style="top:'+($(document).height()/2)+'px" src="wp-content/themes/default/images/lightbox-ico-loading.gif"/></div>');
			// stupid IE 6
			if($.browser.msie&&$.browser.version=='6.0'){
				$('#background').css({width:$(document).width()-17,height:$(document).height()});
			}else{
				$('#background').css({height:$(document).height()});
			}
			$('#background').fadeIn('slow');
			var lightBox_img=new Image();
			var src=gallery_as.eq(current_img).attr('rel');
			if($.browser.msie&&$.browser.version=='7.0'){
				src=src+'?'+Math.random();
			}
			$(lightBox_img).attr('src',src).load(function(){
				$('#loading').remove();
				var top=($(document).height()-lightBox_img.height)/2-40;
				top=top<0? 0:top;
				$('#background').append('<img src="'+lightBox_img.src+'" width="'+lightBox_img.width+'" height="'+lightBox_img.height+'" style="margin-top:'+top+'px"/><div class="zoom"><div id="description">'+gallery_imgs.eq(current_img).attr('title')+'</div><a class="zoomOut" href="#">&nbsp;</a></div><div id="protector" style="height:'+lightBox_img.height+'px;top:'+top+'px">&nbsp;</div>');
				$('#protector').animate({opacity:0});
				zoomButtomInit();
				$('.zoom>a.zoomOut,#protector').bind('click',function(){
					$('#background').fadeOut('slow',function(){
						$(this).remove();
					});
				});
			});
		}
	});
	// slider
	if($('#controller .slider').length>0&&gallery_imgs.length>0){
		var ld=($(document).width()-gallery_imgs.eq(0).width())/2;
		var rd=($(document).width()-gallery_imgs.eq(gallery_imgs.length-1).width())/2+gallery.offset().left-gallery_imgs.eq(gallery_imgs.length-1).offset().left;
		$(document).resize(function(){
			ld=($(document).width()-gallery_imgs.eq(0).width())/2;
			rd=($(document).width()-gallery_imgs.eq(gallery_imgs.length-1).width())/2+gallery.offset().left-gallery_imgs.eq(gallery_imgs.length-1).offset().left;
		});
		var speed=0;
		var stopping=false;
		var slider=$('#controller .slider').slider({
			handle:'.slider_handle',
			startValue:50,
			slide:function(e,ui){
				if(!stopping){
					/*if(speed+50-ui.value>4||speed+50-ui.value<-4){
						speed=ui.value-50;
						galleryMove();
					}
					var oldSpeed=speed;
					if(ui.value<20){
						speed=-45;
					}else if(ui.value>80){
						speed=45;
					}else if(ui.value<40){
						speed=-30;
					}else if(ui.value>60){
						speed=30;
					}	else if(ui.value<50){
						speed=-15;
					}else if(ui.value>50){
						speed=15;
					}
					if(oldSpeed!=speed)
					galleryMove();*/
					if(ui.value<50){
						speed=-45;
					}else if(ui.value>50){
						speed=45;
					}
					galleryMove();
				}
			},
			stop:function(e,ui){
				if(ui.value!=50){
					galleryStop();
				}
			}
		});
			// $('#controller .left').mousedown(function(){
			// 	speed=-30;
			// 	galleryMove();
			// });
			// $('#controller .right').mousedown(function(){
			// 	speed=30;
			// 	galleryMove();
			// });
			// $('#controller .left,#controller .right').mouseup(function(){
			// 	galleryStop();
			// });
			$('#controller .left').click(function(){
				speed=-30;
				galleryMoveToNext();
			});
			$('#controller .right').click(function(){
				speed=30;
				galleryMoveToNext();
			});
			$(document).keydown(function(e){
				if(e.keyCode==37){
					speed=-25;
					galleryMoveToNext();
				}else if(e.keyCode==39){
					speed=25;
					galleryMoveToNext();
				}else if(e.keyCode==13||e.keyCode==32){
					if($('#background>img').length==0){
						$('.zoom>a.zoomIn').trigger('click');
					}else{
						$('.zoom>a.zoomOut').trigger('click');
					}
				}
			});
			function galleryMoveToNext(){
				if((speed<0&&current_img>0)||(speed>0&&current_img<gallery_imgs.length-1)){
					var d=Math.ceil(5/Math.abs(speed))*800;
					gallery_as.eq(current_img).children('span.protector').stop().animate({opacity:0.9},{queue:false});
					current_img=speed<0?current_img-1:current_img+1;
					gallery_as.eq(current_img).children('span.protector').stop().animate({opacity:0},{queue:false,duration:d});
					gallery.animate({left:($(document).width()-gallery_imgs.eq(current_img).width())/2-gallery_imgs.eq(current_img).offset().left+gallery.offset().left},{duration:d,queue:false});
				}
			}
			function galleryMove(){
				var galleryLeft=parseInt(gallery.css('left'));
				if((galleryLeft<ld&&(speed<0))||(galleryLeft>rd&&(speed>0))){
					var distance=speed<0?ld-galleryLeft:rd-galleryLeft;
					var duration=Math.abs(distance*50/speed);
					var destination=speed<0?ld:rd;
					$('#gallery>.inner').stop().animate({left:destination},duration,'linear',function(){
						gallery.stop();
					});
				}else{
					gallery.stop();
				}
			}
			function galleryStop(){
				stopping=true;
				if(slider.slider('value')!=50){
					$('.slider_handle').animate({left:95},{
						complete:function(){
							slider.slider('moveTo',50);
							stopping=false;
						}
					});
				}
				gallery.stop();
				for(var i=0;i<gallery_imgs.length;i++){
					var d=$(document).width()/2-gallery_imgs.eq(i).offset().left;
					if(d>0&&d<gallery_imgs.eq(i).width()){
						if(i!=current_img){
							gallery_as.eq(current_img).children('span.protector').animate({opacity:0.9},{queue:false});
							current_img=i;
						}/* else if(i>0&&speed<0){
							gallery_imgs.eq(current_img).animate({opacity:0.15});
							current_img--;
						}else if(i<gallery_imgs.length-1&&speed>0){
							gallery_imgs.eq(current_img).animate({opacity:0.15});
							current_img++;
						}*/
						gallery_as.eq(current_img).children('span.protector').animate({opacity:0},{queue:false});
						gallery.animate({left:($(document).width()-gallery_imgs.eq(current_img).width())/2-gallery_imgs.eq(current_img).offset().left+gallery.offset().left},{duration:'normal',queue:false,complete:function(){
							stopping=false;
						}});
						break;
					}
				}
			}
		}
		// initialize
		if($(window).height()>$('#main').height()+20){
			//$('body').css({paddingTop:($(window).height()-$('#main').height())/2-10});
			$('body').css({paddingTop:($(window).height()-555)/2-10});
		}
		$('.protector').css('opacity',0.9);
		$('#middle,#gallery,#controller').hide().fadeIn('slow');
		// text on image
		var $bg=null;
		if($('p:has(>img.background)').length>0){
			$bg=$('p:has(>img.background)');
		}else if($('img.background').length>0){
			$bg=$('img.background');
		}
		if($bg!=null){
			var isVertical=$('#middle>.content>.inner:has(div.vertical_text)').length>0; 
			$bg.css({position:'absolute',top:0,left:isVertical?200:0,marginBottom:0,width:isVertical?568:'100%',textAlign:'center'});
			$bg.parent().parent().before($bg);
		}
		// homepage li icons drop down effect
		// if($('#middle>.post').length>0){
		// 		var icon=new Image();
		// 		icon.src=template_url+"/images/flower.gif"+'?'+Math.random();
		// 		var showListIcon=function(li){
		// 			if(li.length==1){
		// 				li.prepend($(icon).clone().addClass('icon')).children('img.icon').animate({top:4,opacity:1},{duration:800,complete:function(){
		// 					showListIcon(li.next('li'));
		// 				}});
		// 			}
		// 		}
		// 		$(icon).ready(function(){
		// 			showListIcon($('#middle>.post li:first'));
		// 		});
		// 	}
	});
