var t, n;
var timer_on = 1;
var column7time = 500;
var column7start = false;

function imgpreload(imgs,settings)
{
	if (settings instanceof Function) { settings = {all:settings}; }

	if (typeof imgs == "string") { imgs = [imgs]; }

	var loaded = [];
	var t = imgs.length;
	var i = 0;

	for (i; i<t; i++)
	{
		var img = new Image();
		img.onload = function()
		{
			loaded.push(this);
			if (settings.each instanceof Function) { settings.each.call(this); }
			if (loaded.length>=t && settings.all instanceof Function) { settings.all.call(loaded); }
		};
		img.src = imgs[i];
	}
}


if (typeof jQuery != "undefined")
{
	(function($){

		$.imgpreload = imgpreload;

		$.fn.imgpreload = function(settings)
		{
			settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);

			this.each(function()
			{
				var elem = this;

				imgpreload($(this).attr('src'),function()
				{
					if (settings.each instanceof Function) { settings.each.call(elem); }
				});
			});

			var urls = [];

			this.each(function()
			{
				urls.push($(this).attr('src'));
			});

			var selection = this;

			imgpreload(urls,function()
			{
				if (settings.all instanceof Function) { settings.all.call(selection); }
			});

			return this;
		};

		$.fn.imgpreload.defaults =
		{
			each: null 
			, all: null 
		};

	})(jQuery);
}

function smartColumns() { //Create a function that calculates the smart columns

        //Reset column size to a 100% once view port has been adjusted
	$("ul.column").css({ 'width' : "100%"});

	var colWrap = $("ul.column").width()-20; //Get the width of row
	if (colWrap<470) colWrap=470;
	var colNum = Math.floor(colWrap / 235); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

	$("ul.column").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("ul.column li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	

}	

function smartColumns2() { //Create a function that calculates the smart columns
        //Reset column size to a 100% once view port has been adjusted
	$("ul.column2").css({ 'width' : "100%"});

	var colWrap = $("ul.column2").width()-20; //Get the width of row
	if (colWrap<430) colWrap=430;
	var colNum = Math.floor(colWrap / 95); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column
	
	var height = colWrap * 432 / 768;
	
	$(".big_photo .video").css({ 'width' : colWrap, 'height' : height});
	$(".big_photo .video > iframe").attr('width',colWrap).attr('height',height);
	
	$("ul.column2").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("ul.column2 li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	

}	

function smartColumns5() { //Create a function that calculates the smart columns
        //Reset column size to a 100% once view port has been adjusted
	$("ul.column5").css({ 'width' : "100%"});

	var colWrap = $("ul.column5").width()-20; //Get the width of row
	var colNum = Math.floor(colWrap / 235); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

	$("ul.column5").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("ul.column5 li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	

}	

function nextImage()
	{
		clearTimeout(t);
		if ($('ul.change li .block a.active').parent().parent().next().find('a').length)
			element=$('ul.change li .block a.active').parent().parent().next().find('a');
			else
			element=$('ul.change li:eq(0) .block a');
		$(element).click();		
	}
	
function nextImage3()
	{
		clearTimeout(v);
		if ($('ul.change3 li .block a.active').parent().parent().next().find('a').length)
			element=$('ul.change3 li .block a.active').parent().parent().next().find('a');
			else
			element=$('ul.change3 li:eq(0) .block a');
		$(element).click();		
	}	

$(document).ready(function() {	
					
	$(".press_more_a").click(function() {
		$(".press_more_span").show();
		$(this).hide();
		return false;
	})
					
	// SHARE: Display the share-links by clicking on share
	  $("li.pt-share").mouseenter(function(){  
		$(this).find('ul.pt-share').addClass("pt-share-show");
		$(this).addClass('pt-share-hover');
	  }).mouseleave(function(){  
		$(this).find('ul.pt-share').removeClass("pt-share-show");
		$(this).removeClass("pt-share-hover");
	  });

	
	$("#footer .search").click(function() {
		$(".search_form form").toggle();
		return false;
	})
	$("#menu").accordion( { active: false, autoHeight: false, header: 'a.head1', alwaysOpen: false, active: '.selected', animated: 'easeOutExpo', collapsible: true });	
	$("#menu ul.head3").accordion( { active: false, autoHeight: false, header: 'a.head3', alwaysOpen: false, active: '.selected', animated: 'easeOutExpo', collapsible: true });
	$("#menu ul.head2").accordion( { active: false, autoHeight: false, header: 'a.head2', alwaysOpen: false, active: '.selected', animated: 'easeOutExpo', collapsible: true });
	
	$('.lightBox').lightBox();
	$('.lightBox2').lightBox();
	$('.lightBox3').lightBox();
	
	if ($('ul.column').length)
		{
			smartColumns();
			$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
				smartColumns();
			});
		}
		
	if ($('ul.column2').length)
		{
			smartColumns2();
			$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
				smartColumns2();
			});
		}
		
	if ($('ul.column5').length)
		{
			smartColumns5();
			$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
				smartColumns5();
			});
		}	
	
	$('ul.change li a').click(function() {					
		clearTimeout(t);
		if ($('ul.change li').length<=1) return false;		
		timer_on=0;
		link=$(this);
		height=$('.big_photo').height();
		$('.big_photo').css('height',height);		
		$('.big_photo div.container').fadeOut(1000,function() {								
				if ($(link).hasClass('vimeo'))
					{
						$('.big_photo img').hide();
						$('.big_photo .video').html($(link).parent().find('.video').html()).show();
						$('.big_photo div.container').fadeIn(1000, function() { $('.big_photo').css('height','auto');});						
						return false
					}
					else
					{
						$('.big_photo .video').html(' ');
						$('.big_photo .video').hide();						
						$('.big_photo img').attr('src',$(link).attr('href')).show();
						$('.big_photo div.container').fadeIn(1000, function() { height=$('.big_photo').height(); $('.big_photo').css('height',height); $('.big_photo').css('height','auto'); t=setTimeout(nextImage,3000); return false;});
						return false;
					}			
				return false;
			});
		$('ul.change a').removeClass('active');
		$(this).addClass('active');
		return false;
	})	
	
	$('ul.change3 li a').click(function() {					
		clearTimeout(v);
		if ($('ul.change3 li').length<=1) return false;		
		timer_on=0;
		link=$(this);
		height=$('.big_photo3').height();
		$('.big_photo3').css('height',height);
		$('.big_photo3 div.container').fadeOut(1000,function() {
				$('.big_photo3 a').attr('href',$(link).attr('rel'));
				$('.big_photo3 img').attr('src',$(link).attr('href')).show();				
				$('.big_photo3 div.container').fadeIn(1000, function() { $('.big_photo3').css('height','auto'); v=setTimeout(nextImage3,3000);});
			});
		$('ul.change3 a').removeClass('active');
		$(this).addClass('active');
		return false;
	})		
	
	/*$('ul.change li a').click(function() {
		if ($('ul.change li').length<=1) return false;
		clearTimeout(t);
		timer_on=0;
		link=$(this).attr('href');
		height=$('.big_photo').height();
		$('.big_photo').css('height',height);		
		$('.big_photo img').fadeOut(1000,function() {
				$('.big_photo img').attr('src',link);
				$('.big_photo img').fadeIn(1000, function() { $('.big_photo').css('height','auto'); timer_on=1; t=setTimeout("nextImage()",3000); });
			});
		$('ul.change a').removeClass('active');
		$(this).addClass('active');
		return false;
	})	*/
	
	if ($('ul.change li').length>1) 
		{
			$('ul.change a').each(function () {$.imgpreload($(this).attr('href'),function() {})})
			if ($('.big_photo .video').html()=='' || $('.big_photo .video').html()==' ')
			t=setTimeout(nextImage,3000);
		}
		
	if ($('ul.change3 li').length>1) 
		{
			$('ul.change3 a').each(function () {$.imgpreload($(this).attr('href'),function() {})})		
			v=setTimeout(nextImage3,3000);
		}
	
	//nextImage();
	
	/*$('ul.column7 li a.link').click(function() {		
		link=$(this).attr('href');
		photo=$(this);
		$(this).parent().parent().parent().find('.last_block').removeClass('last_block');
		$(this).parent().parent().addClass('last_block');
		act_photo=$(this).find('img').attr('src');
		height=$('.big_photo2').height();
		text=$(this).parent().find('.text').html();
		old_text=$('.column7 .block_text div').html();
		last_img=$('.big_photo2 img').attr('alt');
		$('.big_photo2').css('height',height);
		$('ul.column7 .block_text div').fadeOut(1000);		
		$(this).find('img').fadeOut(1000);
		$('.big_photo2 img').fadeOut(1000,function() {
				$('.big_photo2 img').attr('src',link);
				$('.big_photo2 img').attr('alt',act_photo);
				$(photo).find('img').attr('src',last_img);
				$(photo).find('img').fadeIn(1000);
				$(photo).parent().find('.text').html(old_text);
				$('ul.column7 .block_text div').html(text).fadeIn(1000);
				$('.big_photo2 img').fadeIn(1000, function() { $('.big_photo2').css('height','auto'); });
			});		
		$('ul.change a').removeClass('active');
		$(this).addClass('active');
		return false;
	})	
	*/
	
	if ($('ul.column7').length) 
		{
			$('ul.column7 a.link').each(function () {$.imgpreload($(this).attr('href'),function() {})})
			n=setTimeout(change_photo2,7000);
		}
	
	$('.big_photo2').hover(
		function() {
			clearTimeout(n);
		},
		function() {
			n=setTimeout(change_photo2,7000);
		});
	
	$('ul.column7 .block_text').hover(
		function() {
			clearTimeout(n);
		},
		function() {
			n=setTimeout(change_photo2,7000);
		});
	
	$('.big_photo').hover(
		function() {
			clearTimeout(t);
		},
		function() {
			if ($(this).find('.video').html()=='' || $(this).find('.video').html()==' ')
				t=setTimeout(nextImage,3000);
		});
	
	$('.big_photo3').hover(
		function() {
			clearTimeout(v);
		},
		function() {
			v=setTimeout(nextImage3,3000);
		});
	
	$('ul.column7 li a.link').click(function() {
		if (column7start) return false;
		column7start = true;
		clearTimeout(n);
		photo = $(this).attr('href');
		li = $(this).parent().parent();
		$('.big_photo2 img').fadeOut(1000);
		$('ul.column7 li.active .block_text >div').fadeOut(500,function() {
			$('ul.column7 li.active .block_text').animate({ width: '0px', height: '0px', opacity: 0}, column7time, function () {
				$('ul.column7 li.active .block').css({opacity: 0, display: 'block'}).animate({opacity: 1},column7time);
				$('ul.column7 li.active ').animate({width: '102px'},column7time,function() {
					$(li).find('.block').animate({opacity: 0},column7time);
					$(li).animate({width: '214px'},column7time);
					$(this).removeClass('active');
									
					$('.big_photo2 img').attr('src',photo);
					$('.big_photo2 img').fadeIn(1000);
					
					$(li).find('.block').animate({width: '194px'},column7time,function() {
						$(this).hide();
						$(li).find('.block_text > div').hide();
						$(li).find('.block_text').css({opacity: 0, display: 'block',width: '0px', height: '0px'}).animate({opacity: 1, width: '194px', height: '183px'},column7time, function() {
							$(li).find('.block_text > div').fadeIn(500);																															  
							$(li).addClass('active');											
							n=setTimeout(change_photo2,7000);
							column7start = false;
						})					
					});
				})
			})
		return false;
		})
//		$('ul.column7 li').removeClass('active');
//		$(this).parent().parent().addClass('active');						
		return false;
	})	
	
	
	
	
	$('#sidebar li a').live('click',function() {
		$('#sidebar li a').removeClass('active');
		$(this).addClass('active');
		return false;
	})
	
	$(".din").css('visibility','visible');
	
	
});

function change_photo2()
{
	if ($('ul.column7 li.active').next().length)
		object=$('ul.column7 li.active').next();
		else
		object=$('ul.column7 li:eq(0)');
	$(object).find('a.link').click();
}

function change_photo()
{
	element=$('.last_block a.link');		
	link=$(element).attr('href');
	photo=$(element);
	$(element).parent().parent().parent().find('.last_block').removeClass('last_block');
	if ($(element).parent().parent().next().length)
		$(element).parent().parent().next().addClass('last_block');
		else
		$('ul.column7 li:eq(1)').addClass('last_block');
	act_photo=$(element).find('img').attr('src');
	height=$('.big_photo2').height();
	text=$(element).parent().find('.text').html();
	old_text=$('.column7 .block_text div').html();
	last_img=$('.big_photo2 img').attr('alt');
	$('.big_photo2').css('height',height);
	$('ul.column7 .block_text div').fadeOut(1000);		
	$(element).find('img').fadeOut(1000);
	$('.big_photo2 img').fadeOut(1000,function() {
			$('.big_photo2 img').attr('src',link);
			$('.big_photo2 img').attr('alt',act_photo);
			$(photo).find('img').attr('src',last_img);
			$(photo).find('img').fadeIn(1000);
			$(photo).parent().find('.text').html(old_text);
			$('ul.column7 .block_text div').html(text).fadeIn(1000);
			$('.big_photo2 img').fadeIn(1000, function() { $('.big_photo2').css('height','auto'); });
		});		
	$('ul.change a').removeClass('active');
	$(this).addClass('active');
	setTimeout(change_photo,5000);
	return false;
}
