// JavaScript Document
var ww,wh = 0;
var url = window.location.pathname;
if(url!=''){
	url = url.split('/');
	}
var loadedimg = null;
var loadedsrc = null;
var loadedar = 0;
var cri = 0;
var cph = 0;
var img = null;
var scroller_pos = 0;
var scroller_max = 0;
var scroller_a = 0;
var slideshow = 0;
var slideshow_c,slideshow_max = 0;

$(document).ready(function(){


	$('.zoom').fancybox({
		overlayOpacity: 0.9,
		padding:0,
		overlayColor: '#000',
		transitionIn : 'none',
		transitionOut : 'none',
		changeSpeed : 0,
		changeFade: 10,
		titlePosition : 'over',
		hideOnContentClick : true,
		'onComplete': function() {
			$("#fancybox-outer").bind("contextmenu", function () {
				$.fancybox.close();
				return false;
				});
			}
	});
	
	$('.videozoom').fancybox({
		overlayOpacity: 0.9,
		padding:0,
		overlayColor: '#000',
		transitionIn : 'none',
		transitionOut : 'none',
		changeSpeed : 0,
		changeFade: 10,
		type : 'swf',
		width: '720',
		height: '576',
		swf : {'wmode':'transparent','allowfullscreen':'true'},
		titlePosition : 'over',
		hideOnContentClick : true,
		'onComplete': function() {
			$("#fancybox-outer").bind("contextmenu", function () {
				$.fancybox.close();
				return false;
				});
			}
	});

	$('#commentform').attr('action','/comment_post').hide();
	$('#reply-title').click(function(){
		$('#commentform').slideToggle();
		})
	
	resizer();
	$(window).resize(function(){resizer();});
	
	//setTimeout(function(){content_resize()},1000);
	
	$('.topmenu a.sub').click(function(){
		$('.topmenu a.active').removeClass('active');
		var sid = $(this).addClass('active').attr('id');
		$('.submenu').hide(200);
		$('.submenu.'+sid).css('left',$(this).position().left).show(1000);
		return false;
		});
	
	if(url!=''){
		$('.topmenu a').each(function(){
			if($(this).attr('href')==('/'+url[1])) $(this).addClass('active');
			else if($(this).attr('href')==('#'+url[1])) $(this).addClass('active');
			})
		
		if(url[2]!=undefined){
			$('.submenu a[href="'+ url[0] +'/'+ url[1] +'/' + url[2] + '"]').addClass('active');
			}
		
		setTimeout(function(){
			$('.topmenu a.active').click();
			},500);
		}
	
	
	
	
	
	
	
	$('.thumbs a').click(function(){
		$('.thumbs a.active').removeClass('active').stop().animate({'opacity':0.5},500);
		$(this).addClass('active').stop().animate({'opacity':1},50);
		
		img = $("<img/>").css({'opacity':0});
		img.attr("src", $(this).attr('href')).load(function() {
			loadedar = this.width/this.height;
			fadenewimage();
			});
		
		loadedsrc = $(this).attr('href');
		loadedvideo = $(this).attr('alt');
		slideshow_c = $(this).attr('accesskey');
		return false;
		})
	
	setTimeout(function(){$('.thumbs a:first').click()},800);
	
	$('.thumbs a').css('opacity',0.5);
	$('.thumbs a').mouseenter(function(){
				$(this).stop().animate({'opacity':1},50);
			}).mouseleave(function(){
				if(!$(this).hasClass('active'))$(this).stop().animate({'opacity':0.5},500);
			});
	/* count scroller elements */
	scroller_max = Math.ceil(($('.thumbs a').size()*80)/720)-1;
	
	$('.thumbs .back').click(function(){
		if(!scroller_a){
			scroller_pos--;
			if(scroller_pos<0) scroller_pos = 0;
			}
			console.log(scroller_pos)
			scroller_a = 1;
			$('.tholder').animate({'left':-scroller_pos*720},500,'easeOutExpo',function(){scroller_a=0;})
			$(this).blur();
		})
	
	$('.thumbs .next').click(function(){
		if(!scroller_a){
			scroller_pos++;
			if(scroller_pos>=scroller_max) scroller_pos = scroller_max;
			}
			console.log(scroller_pos)
			scroller_a = 1;
			$('.tholder').animate({'left':-scroller_pos*720},500,'easeOutExpo',function(){scroller_a=0;})
			$(this).blur();
		})


	slideshow_max = $('.thumbs a').size()-1;

	$('.slideshow').click(function(){
		$(this).toggleClass('playing');
		if($(this).hasClass('playing'))
			{
			clearInterval(slideshow);
			slideshow = setInterval(function(){
				if(slideshow_c==slideshow_max) slideshow_c = 0;
				else slideshow_c++;
				$('.thumbs a').eq(slideshow_c).click();
				},3000)
			}
		else clearInterval(slideshow);
		})




setTimeout(function(){$('.gallery-text').slideUp(1500)},2000);
	$('h1.gallery').click(function(){
		$('.gallery-text').slideToggle(1500);
		})



$('.content-holder .pnext').click(function(){
		if(slideshow_c==slideshow_max) slideshow_c = 0;
		else slideshow_c++;
		$('.thumbs a').eq(slideshow_c).click();
		})

$('.content-holder .pback').click(function(){
		if(slideshow_c==0) slideshow_c = slideshow_max;
		else slideshow_c--;
		$('.thumbs a').eq(slideshow_c).click();
		})
//.content-holder .pback

});







	function fadenewimage(){
		$('.content-holder img').fadeOut(200,function(){
									$(this).parent().empty();
									$('.content-holder a').append(img);
									
									if(loadedvideo!='') $('.content-holder a').attr('href',loadedvideo.replace(new RegExp("watch\\?v=", "i"), 'v/')+'&autoplay=1');
									else  $('.content-holder a').attr('href', loadedsrc);
									
									resize_show()
									});
		}



function resize_show(){
	var cw = $('.content-holder').width()-4;//borders
	var ch = $('.content-holder').height()-4;//borders
	var car = cw/ch;
	
	//resize image object according to AR
	var imgw,imgh = 0;
	if(car<=loadedar){
		imgw = cw;
		imgh = Math.round(cw/loadedar);
		marginl = 0;
		margint = Math.round((ch-imgh)/2);
		}
	else{
		imgh = ch;
		imgw = Math.round(ch*loadedar);
		marginl = Math.round((cw-imgw)/2);
		margint = 0;
		}
	
	$('.content-holder .pnext').css({'margin-top':(ch/2)-40,'margin-left':cw-40});
	$('.content-holder .pback').css({'margin-top':(ch/2)-40});
	
	//console.log($('.content-holder').width(),$('.content-holder').height());
	$('.content-holder img').css({
					'width':imgw,
					'height':imgh,
					'margin-left':marginl,
					'margin-top':margint
					 }).animate({'opacity':1},400);
	
	
	}


	function content_resize(){
		$('.content-holder').stop().animate({'height':cph},500,'easeInOutExpo',function(){resize_show()});
		}



	function resizer(){
		wh = $(window).height();
		ww = $(window).width();
		
		cph = wh-310;
		if(cph<350) cph = 350;
		
		var bw,bh = 0;
		
		if(ww>=990) bw = ww;
		else bw = 990;
		
		if(wh>=600) bh = wh;
		else bh = 600;
		
		/*
		if(wh>$('.body-wrapper').height()) bh=wh;
		else bh = $('.body-wrapper').height();
		*/
		
		if(cri!=undefined) clearTimeout(cri);
		cri = setTimeout(function(){content_resize()},300);
		
		$('.bg').stop().animate({'width':bw,'height':bh},200);
		}
