function changeSlideshow() {
	
	
	if (window.slidesSlided > 0) 
	{
		initialDelay = 0;
	} else 
	{
		initialDelay = 1000;
	}
	window.slidesSlided++;

	if ($('.slideshow').hasClass('short')) 
	{
		$("#background").fullBg();
		var sh = $(".slideshow").height();
		$("#background").fullBg();
		$("#background").hide()
		.css("margin-top","0")
		.delay(initialDelay)
		.animate({
			opacity:'toggle', marginTop:-40},
			2000, 
			'linear',
			function() 
			{
				$("#background").animate({marginTop:sh+20-$("#background").height()},($("#background").height()-sh-20)*80,"linear",
						function() 
						{
							$("#background").animate({opacity:'toggle', marginTop:sh-$("#background").height()},1000,"linear",
														function() 
														{
															changeSlideshow();
														});
						});
			});

	}
	else 	
	$.get('/index/get_random_image', function(data) {

		$('.slideshow').replaceWith(data);
		$("#background").bind("load", function () { 
				$("#background").fullBg();
				var sh = $(".slideshow").height();
				$("#background").fullBg();
				$("#background").hide()
				.css("margin-top","0")
				.delay(initialDelay)
				.animate({
					opacity:'toggle', marginTop:-40},
					2000, 
					'linear',
					function() 
					{
						$("#background").animate({marginTop:sh+20-$("#background").height()},($("#background").height()-sh-20)*40,"linear",
								function() 
								{
									$("#background").animate({opacity:'toggle', marginTop:sh-$("#background").height()},1000,"linear",
																function() 
																{
																	changeSlideshow();
																});
								});
					});
			});		

	});


}

function alignGalleryBoxes() {
	var maxHeight = 0;
	$(".short_news_item.gallery_item").each(function () {
		if ($(this).height() > maxHeight) maxHeight = $(this).height();
	});
	$(".short_news_item.gallery_item").each(function (i,e) {
		diff = "+=" + (maxHeight - $(this).height());
		$(this).find(".news_title").delay(i*200).animate({height: diff},350);
	});
}

function alignNewsBoxes() {
	var maxHeight = 0;
	$(".short_news_item").each(function () {
		if ($(this).height() > maxHeight) maxHeight = $(this).height();
	});
	$(".short_news_item").each(function (i,e) {
		diff = "+=" + (maxHeight - $(this).height());
		$(this).find(".news_short").delay(i*200).animate({height: diff},350);
	});
}

$(document).ready(function() 
{
	
	$(".promo > ul").anythingSlider({
											width: null,      // Override the default CSS width
											height: null,
											hashTags: false,
											buildArrows: false
									});

	$("#datatable").multicolselect(
	{
		
			buttonImage:"http://polymedix.bg/public/images/select_box_button.png",
			valueCol:0,
			hideCol:0
			
	});	
	
	
	$('.main_menu > li > ul').parent()
	.mouseenter(function()
	{
	
		$(this).children("ul").stop(true,true).slideDown("fast");
		$(this).addClass("active");
	
	})
	.mouseleave(function()
	{
	
		$(this).children("ul").slideUp("fast");
		$(this).removeClass("active");
		
	}).children("a").addClass("dropdown");
	
	$('.add_to_cart a').click(function (){
											count = $(this).prev('input').val();
											option_selected = $('.mltsel').val();
											$(this).attr('href',$(this).attr('href')+"/"+count+"/"+option_selected);
											//return false;
										});

	function updateCart() {
		
		$.get('/cart/', function(data) {
		  $('.cart_short').html(data);
		});
	
	}
	
	//$(".short_news_container").vgrid();
	
	//$('.cart_short').fancybox({'autoDimensions':false, 'type' : 'iframe', 'width':940, 'height':460, 'onClosed':updateCart});
});

$(window).load(function() {
	
	window.slidesSlided = 0;
	changeSlideshow();	
	alignGalleryBoxes();
	alignNewsBoxes();
	
});

