var szerokosc_filmow_youtube = 522;
var wysokosc_filmow_youtube = 349;



function addSuffix(suffix, str){
	var img = str.split('/');
	img[img.length-1] = suffix+img[img.length-1];
	var new_img = img[0];
	for(i=1; i<img.length;i++) new_img+='/'+img[i];
	return new_img;
}

function removeSuffix(suffix, str){
	var img = str.split('/');
	img[img.length-1] = img[img.length-1].replace(suffix,'');
	var new_img = img[0];
	for(i=1; i<img.length;i++) new_img+='/'+img[i];
	return new_img;
}

function resize_img(img, resize_val)
{
	//var img_width = img.width();
	//var img_height = img.height();
	
	var tmp = img.attr('rel').split('_');
	var img_width = tmp[1];
	var img_height = tmp[2];
	
	var window_width = $(window).width();
	var window_height = $(window).height();
	if(img_width/*+140*/ > window_width)
	{
	//$('#shadow').css('background-color', '#600');
		var old_img_width = img_width;
		img_width = window_width/*-140*/;
		var procent_pomniejszenia_szerokosci = (100 - ((img_width * 100) / old_img_width));
		img_height = img_height - ((img_height * procent_pomniejszenia_szerokosci) / 100);
		img.css('width', img_width+'px');
		img.css('height', img_height+'px');
	}
	else if(img_width/*+140*/ < window_width)
	{
	//$('#shadow').css('background-color', '#fff');
		var old_img_width = img_width;
		img_width = window_width/*-140*/;
		var procent_powiekszenia_szerokosci = (100 - ((img_width * 100) / old_img_width));
		if(img_width > window_width/*-140*/)
			img_height = img_height + ((img_height * (0-procent_powiekszenia_szerokosci)) / 100);
		else
			img_width = old_img_width;
		img.css('width', img_width+'px');
		img.css('height', img_height+'px');
	}
	if(img_height+100 > window_height)
	{
		var old_img_height = img_height;
		img_height = window_height - 100;
		var procent_pomniejszenia_wysokosci = (100 - ((img_height * 100) / old_img_height));
		img_width = img_width - ((img_width * procent_pomniejszenia_wysokosci) / 100);
		img.css('width', img_width+'px');
		img.css('height', img_height+'px');
	}
	else if(img_height+100 < window_height)
	{
	//$('#shadow').css('background-color', '#060');
		var old_img_height = img_height;
		img_height = window_height - 100;
		var procent_powiekszenia_wysokosci = (100 - ((img_height * 100) / old_img_height));
		if(img_height > window_height-100)
			img_width = img_width + ((img_width * (0-procent_powiekszenia_wysokosci)) / 100);
		else
			img_height = old_img_height;
		img.css('width', img_width+'px');
		img.css('height', img_height+'px');
	}
	
	var tmp2 = img.attr('rel').split('_');
	var img_width2 = tmp2[1];
	var img_height2 = tmp2[2];
	if(img.width() > img_width2 || img.height() > img_height2)
	{
		img.css('width', img_width2+'px');
		img.css('height', img_height2+'px');
	}
	
	$('#mbox').css('top', (($(window).height() / 2) - (img.height() / 2) - 20)+'px');
}

function animacje_napisow()
{
	$('#menu li a').hover(function() {
		$(this).stop(true, false).animate({
			color: "#C13C39"
		}, 300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#000"
		}, 300);
	});
	
	$('#wyslij_brief').hover(function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#5d5d5d'
		}, 300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#000",
			backgroundColor: '#A6BFCD'
		}, 300);
	});
	
	$('#see_all, #case_nav .see_all').hover(function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#5d5d5d'
		}, 300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#000",
			backgroundColor: '#FFD46C'
		}, 300);
	});
	
	$(' #sciagnij').hover(function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#5d5d5d'
		}, 300);
		$(this).children('span').fadeIn(300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#000",
			backgroundColor: '#FFD46C'
		}, 300);
		$(this).children('span').fadeOut(300);
	});
	
	$('#case_nav .prev, #case_nav .next').hover(function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#5d5d5d'
		}, 300);
		$(this).children('span').fadeIn(300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#000",
			backgroundColor: '#E2E2E2'
		}, 300);
		$(this).children('span').fadeOut(300);
	});
	
	$('#send').hover(function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#5d5d5d'
		}, 300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#A6BFCD'
		}, 300);
	});
	
	$('#wyslij_brief').hover(function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#5d5d5d'
		}, 300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#000",
			backgroundColor: '#A6BFCD'
		}, 300);
	});
	
	$('#close_ok').hover(function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#5d5d5d'
		}, 300);
	}, function() {
		$(this).stop(true, false).animate({
			color: "#fff",
			backgroundColor: '#a6bfcd'
		}, 300);
	});
	
	$('#thumbs li').hover(function() {
		$(this).children('span').fadeIn(300);
	}, function() {
		$(this).children('span').fadeOut(300);
	});
	
	$('#close').hover(function() {
		$('#close2').fadeIn(300);
	});
	
	$('#close2').hover(function() {
		
	}, function() {
		$('#close2').fadeOut(300);
	});
	
	$('#close3').hover(function() {
		$('#close4').fadeIn(300);
	});
	
	$('#close4').hover(function() {
		
	}, function() {
		$('#close4').fadeOut(300);
	});
}

jQuery(document).ready(function(){
	animacje_napisow();

	$('#case_main li').hover(function() {
		$(this).stop(true, false).animate({
			backgroundColor: '#e2e2e2'
		}, 300);
	}, function() {
		$(this).stop(true, false).animate({
			backgroundColor: '#fff'
		}, 300);
	});
	if($("input#hid").is("input")) {
		$("input#hid").change(function(){
			$("input#file_info").attr('value', $("input#hid").attr('value'));	
		});
	}
		$("img#close, img#close2, img#close3, img#close4, #close_ok").click(function(){
			$("#brief, #shadow, #thanks").fadeOut(300);
			return false;
		});
	if($("#wyslij_brief").is("a")){
		$("#wyslij_brief").click(function(){
			$("#brief, #shadow").fadeIn(300);
			return false;
		});
	}
	
	if($("ul#thumbs").is("ul")) {
		$("ul#thumbs li").click(function(){
			$('#shadow, #mbox, #mbox_close, #next, #prev').fadeIn(300);
			//$('#wait').fadeIn(300);
			$('#mbox #counter').fadeOut(300);
			//$('#mbox ul').css('width', '200px');
			//$('#mbox ul .center').css('width', '60px');
			//$('#mbox').css('top', ($(document).scrollTop()+100)+'px');
			var li = $(this);
			if(li.hasClass('film'))
			{
				$("#big").hide();
				$("#mbox iframe").fadeIn(500);
				$('#mbox ul').css('width', (szerokosc_filmow_youtube/*+140*/)+'px');
				$('#mbox ul .center').css('width', szerokosc_filmow_youtube+'px');
				$("#mbox iframe").attr('src', 'http://www.youtube.com/embed/'+li.attr('rel'));
				$('#mbox').css('top', (($(window).height() / 2) - (wysokosc_filmow_youtube / 2) - 20)+'px');
				$('#mbox #counter').fadeIn(300);
				$('#wait').fadeOut(300);
			}
			else
			{
				$("#big").fadeIn(500);
				$("#mbox iframe").hide();
				$('#big').load(function() {
					$('#big').attr('rel', li.children('img').attr('id'));
					resize_img($(this), false);
					$('#mbox ul').css('width', ($('#big').width()/*+140*/)+'px');
					$('#mbox ul .center').css('width', $('#big').width()+'px');
					$('#mbox #counter').fadeIn(300);
					$('#wait').fadeOut(300);
					$("#big").fadeIn(500);
					$('#big').unbind('load');
				});
				
			}
			var new_img = addSuffix('big_', $("img",$(this)).attr("src"));
			$("#big").attr('src',new_img);
				
			var tmp = ($('#big').attr('src')).split('/');
			var tmp2 = (tmp[tmp.length-1]).split('.');
			var tmp3 = (tmp2[0]).split('_');
			var current_number = tmp3[2];
			$('#mbox #counter .first').html(current_number);
			return false;
		});
	}
	
	$("#mbox_close").click(function(){
		$("#mbox, #shadow, #mbox_close, #next, #prev").fadeOut(300);
		if(!$.browser.msie)
			$('#video-player-flash').stopVideo();
		//if($.browser.msie)
			//$('#video-player-flash').get(0).stopVideo();
		return false;
	});
	
	var elements = $('ul#thumbs li').get();
	var count_thumbs = elements.length;
	$('#mbox #counter .last').html(count_thumbs);
	
	if($("#big").is("img")){
		$("#next").click(function(){
			//$('#wait').fadeIn(300);
			$('#mbox #counter').fadeOut(300);
			//$('#mbox ul').css('width', '200px');
			//$('#mbox ul .center').css('width', '60px');
			$("#big").fadeOut(500, function() {
				var tmp = ($('#big').attr('src')).split('/');
				var tmp2 = (tmp[tmp.length-1]).split('.');
				var tmp3 = (tmp2[0]).split('_');
				var current_number = tmp3[2];
				if(current_number < count_thumbs)
					current_number++;
				else
					current_number = 1;
				$('#mbox #counter .first').html(current_number);
				var new_img = '';
				for(var i=0; i < (tmp.length-1); i++)
					new_img += tmp[i]+'/';
				new_img += 'big_'+tmp3[1]+'_'+current_number+'.jpg';
				
				$("#big").attr('src',new_img);
				var li = $('#thumbs #li_'+current_number);
				if(li.hasClass('film'))
				{
					$("#big").hide();
					$("#mbox iframe").fadeIn(500);
					$('#mbox ul').css('width', (szerokosc_filmow_youtube/*+140*/)+'px');
					$('#mbox ul .center').css('width', szerokosc_filmow_youtube+'px');
					$("#mbox iframe").attr('src', 'http://www.youtube.com/embed/'+li.attr('rel'));
					$('#mbox').css('top', (($(window).height() / 2) - (wysokosc_filmow_youtube / 2) - 20)+'px');
					$('#mbox #counter').fadeIn(300);
					$('#wait').fadeOut(300);
				}
				else
				{
					$("#big").fadeIn(500);
					$("#mbox iframe").hide();
					$('#big').load(function() {
						$('#big').attr('rel', $('#thumbs #li_'+current_number+' img').attr('id'));
						resize_img($(this), false);
						$('#mbox ul').css('width', ($('#big').width()/*+140*/)+'px');
						$('#mbox ul .center').css('width', $('#big').width()+'px');
						$('#mbox #counter').fadeIn(300);
						$('#wait').fadeOut(300);
						$("#big").fadeIn(500);
						$('#big').unbind('load');
					});
				}
			});
			return false;
		});
		$("#prev").click(function(){
			//$('#wait').fadeIn(300);
			$('#mbox #counter').fadeOut(300);
			//$('#mbox ul').css('width', '200px');
			//$('#mbox ul .center').css('width', '60px');
			$("#big").fadeOut(500, function() {
				var tmp = ($('#big').attr('src')).split('/');
				var tmp2 = (tmp[tmp.length-1]).split('.');
				var tmp3 = (tmp2[0]).split('_');
				var current_number = tmp3[2];
				if(current_number > 1)
					current_number--;
				else
					current_number = count_thumbs;
				$('#mbox #counter .first').html(current_number);
				var new_img = '';
				for(var i=0; i < (tmp.length-1); i++)
					new_img += tmp[i]+'/';
				new_img += 'big_'+tmp3[1]+'_'+current_number+'.jpg';
				
				$("#big").attr('src',new_img);
				var li = $('#thumbs #li_'+current_number);
				if(li.hasClass('film'))
				{
					$("#big").hide();
					$("#mbox iframe").fadeIn(500);
					$('#mbox ul').css('width', (szerokosc_filmow_youtube/*+140*/)+'px');
					$('#mbox ul .center').css('width', szerokosc_filmow_youtube+'px');
					$("#mbox iframe").attr('src', 'http://www.youtube.com/embed/'+li.attr('rel'));
					$('#mbox').css('top', (($(window).height() / 2) - (wysokosc_filmow_youtube / 2) - 20)+'px');
					$('#mbox #counter').fadeIn(300);
					$('#wait').fadeOut(300);
				}
				else
				{
					$("#big").fadeIn(500);
					$("#mbox iframe").hide();
					$('#big').load(function() {
						$('#big').attr('rel', $('#thumbs #li_'+current_number+' img').attr('id'));
						resize_img($(this), false);
						$('#mbox ul').css('width', ($('#big').width()/*+140*/)+'px');
						$('#mbox ul .center').css('width', $('#big').width()+'px');
						$('#mbox #counter').fadeIn(300);
						$('#wait').fadeOut(300);
						$("#big").fadeIn(500);
						$('#big').unbind('load');
					});
				}
			});
			return false;
		});
		
		$(window).resize(function() {
			resize_img($('#big'), true);
			$('#mbox ul').css('width', ($('#big').width()/*+140*/)+'px');
			$('#mbox ul .center').css('width', $('#big').width()+'px');
			if($('#big').is(':hidden'))
				$('#mbox').css('top', (($(window).height() / 2) - (wysokosc_filmow_youtube / 2) - 20)+'px');
		});
	}
});
