var Al_Service_Info = {current: false, dont: 0};

jQuery(document).ready(function(){
	jQuery('ul.sf-menu>li:last-child').addClass('last');
	jQuery('.services_content').hide();
	Al_Service_Info.services = $("#services > div.services_item");
	Al_Service_Info.services_j = [];
	for(var i = 0, to = Al_Service_Info.services.size(); i < to; i++)
	{
		Al_Service_Info.services_j.push($(Al_Service_Info.services.get(i)));
		$(Al_Service_Info.services.get(i)).attr('num', i);
	}
	
	
	jQuery('.services_item h3 a').click(function(){
		if(Al_Service_Info.dont > 0) return false;
		t = jQuery(this);

		var current = t.parent().parent().attr('num');

		if(Al_Service_Info.current !== false)
		{
			closeService(
				$(Al_Service_Info.services.get(Al_Service_Info.current))
			);
		}
		if(Al_Service_Info.current === false || Al_Service_Info.current != current)
		{
			openService(
				$(Al_Service_Info.services.get(current))
			);
			Al_Service_Info.current = current;
		}
		else Al_Service_Info.current = false;		
		
		return false;
	})

	jQuery('#db_says_link').click(function(){
		jQuery('#db_says_block:not(.expanded)').animate( { width:"100%"}, 450, function(){
			jQuery('#db_says_text').css({display:'block'});
			var now_height = jQuery('#db_says_text').height();			
			jQuery('#db_says_text').css({display:'none'});
			jQuery('#db_says_text').css({display:'block', height: 0}).animate({height:now_height}, 400, function(){				
				//jQuery(this).css('height', now_height);
				jQuery('#db_says_block').addClass('expanded');
			});
		});
		jQuery('#db_says_block.expanded #db_says_text').animate({height:'0'}, 300, function(){
			jQuery(this).css({display:'none',height:'auto'});
			jQuery('#db_says_block.expanded').animate( { width:"14em"}, 450,	function(){
				jQuery('#db_says_block').removeClass('expanded');
			});
			//jQuery('#db_says_text').css('height', 'auto');
		});
		return false;
	});
	
	jQuery('#popup').clone(true).appendTo("body");
	jQuery('#popup:first').remove();
	
	jQuery('#popup_video').clone(true).appendTo("body");
	jQuery('#popup_video:first').remove();

	jQuery('.widget_archive li').prepend('&gt; ');
	jQuery('.post:last').addClass('last');

	jQuery('#searchsubmit').click(function(){
		if (jQuery('#s').val()=='') {return false;}
	})
	
	
	
	//jQuery('.home_banners a span').hide();

	jQuery('.home_banners a').hover(function(){
			jQuery(this).children('span').fadeOut(350);
	})

	jQuery('.home_banners a').mouseover(function(){
			jQuery(this).children('span').not(':animated').fadeIn(150);
	})
})

var cur_image = 1;

function curtain_setup() {
	var wh = $(window).height();
	var dh = $(document).height();
	var h = dh > wh ? wh : dh;
 	var docWidth = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;
 	jQuery('#curtain').css({
		//height: docHeight,
		height: h + 'px',
		width: docWidth
	});
}

function openform() {
	curtain_setup();
	jQuery('.slide_item').hide();
	jQuery('.slide_item:first').show();
	jQuery('.gal_thumbs a:first').addClass('active');
	jQuery('#curtain').css({opacity: "0.7"});
	jQuery('#curtain').fadeIn('fast');
	if (jQuery('.gal_thumbs a').size() < 2) {
		jQuery('.gal_controls').hide();
	}
	var marginLeft = -(jQuery('#popup').innerWidth()/2);
	var marginTop = -(jQuery('#popup').outerHeight()/2);
	//alert($('#popup').height());
	jQuery('#popup').css({
		marginLeft: marginLeft,
		marginTop: marginTop
	});
	jQuery('#popup').fadeIn('fast');
}
var tmp_once = true;
function openvideo(y, v, i) {
	curtain_setup();
	jQuery('#curtain').css({opacity: "0.7"});
	jQuery('#curtain').fadeIn('fast');

	var stls = 'display:block;width:576px;height:324px;text-align: center; background-image: URL(' + ((i && i !== 'false') ? i : '/uploads/video/default.png') + ');background-repeat: no-repeat; background-position: center center;';
	jQuery('#popup_video a#player').attr("href", v).attr("style", stls).html("Click to play");
	jQuery('#popup_video a#year').html(y);

	flowplayer("player", "/flowplayer/flowplayer-3.2.5.swf", {
		clip: {
			autoPlay: false, scale: 'orig'
		},
		playlist: [
			{url: i,autoPlay: true}, v
		]
	}).load();
	var marginLeft = -($('#popup_video').outerWidth()/2);
	var marginTop = -($('#popup_video').outerHeight()/2);
	//alert($('#popup').height());
	jQuery('#popup_video').css({
		marginLeft: marginLeft,
		marginTop: marginTop
	});

	jQuery('#popup_video').fadeIn('fast');


}

function closeform() {
	if(jQuery('#popup_video a#player:visible').size())
	{
		jQuery('#player').css("background","none");
		flowplayer("player").unload();
		//jQuery('#player').attr('href','');
		//jQuery('#player').attr('style','');
		/*jQuery('#popup_video a#player').children().each(function(){
			jQuery(this).remove();
		});*/
		jQuery('#player').remove();
		jQuery('#popup_video div h3').after('<a id="player"></a>');
	}
	jQuery('.gal_thumbs a').removeClass('active');
	jQuery('#popup').fadeOut('fast');
	jQuery('#popup_video').fadeOut('fast');
	jQuery('#curtain').fadeOut('fast');
}


$(document).ready(function()
{
	//openform();
	jQuery('#curtain').click(function(){
		closeform();
	});

	jQuery('.closeform').click(function(){
		closeform();
		return false;
	});

	jQuery('#slide_show_link').click(function(){
		openform();
		return false;
	});

	jQuery('.gal_thumbs a').click(function(){
		if (jQuery(this).hasClass('active')) {return false; }
		jQuery('.gal_thumbs a').removeClass('active');
		jQuery(this).addClass('active');
		var n = $(this).attr('rel');
		jQuery('.slide_item').hide();
		jQuery('#slide_'+n).fadeIn(230);
		cur_image = n;
		return false;
	});

	jQuery('#slideshow_next').click(function(){
		if (parseInt(cur_image) < jQuery('.gal_slides .slide_item').size()) {
			cur_image++;
			jQuery('.gal_thumbs a').removeClass('active');
			jQuery('.gal_thumbs a:eq('+ (cur_image-1) +')').addClass('active');
			jQuery('.slide_item').hide();
			jQuery('#slide_'+(cur_image)).fadeIn(230);
		}
		return false;
	});

	jQuery('#slideshow_prev').click(function(){
		if (parseInt(cur_image) > 1) {
			cur_image = cur_image - 1;
			jQuery('.gal_thumbs a').removeClass('active');
			jQuery('.gal_thumbs a:eq('+ (cur_image-1) +')').addClass('active');
			jQuery('.slide_item').hide();
			jQuery('#slide_'+(cur_image)).fadeIn(230);
		}
		return false;
	});
	
	if (window.location.hash != '') {		
		var hash = parseInt(window.location.hash.substring(2))-1;
		//alert(hash);
	}		

});

$(window).resize(function(){
	curtain_setup();
});

$(window).scroll(function (){
	curtain_setup();
});

function closeService($divService)
{
	var $sc = $divService.find("div.services_content");
	serv_height = $sc.height();
	serv_time = serv_height * 3.4;
	Al_Service_Info.dont++;
	
	t = $divService.find('h3 > a');
	
	$sc.slideUp(serv_time, (function(t){
		return function()
		{
			Al_Service_Info.dont--;
			t.removeClass('active');
		}
	})(t));
	//alert(serv_height);
}

function openService($divService)
{
	var $sc = $divService.find("div.services_content");
	$sc.css({display: 'block'});
	serv_height = $sc.height();
	$sc.css({display: 'none'});
	
	serv_time = serv_height * 3.4;
	Al_Service_Info.dont++;
	
	t = $divService.find('h3 > a');
	
	$sc.css({display:'block', height: 0}).animate({height:serv_height}, serv_time, (function(t){
		return function()
		{
			Al_Service_Info.dont--;
			t.addClass('active');
		}
	})(t));
	//alert(serv_height);
}
