jQuery.ajaxSetup({contentType: "application/x-www-form-urlencoded; charset=utf-8"});

var scroller_videos_time = 25000;
var scroller_videos_direction = 'to_right';
var scroller_videos_timeout;

$(document).ready(function(){
	/* top banner */
	$("#top_banner_left .items").TopBanner({ mousestop: true, time: 5000 });
	$("#top_banner_right .items").TopBanner({ mousestop: true, time: 5000 });
	
	$('#scroller_videos .viewer').scrollLeft(0);
	$('#scroller_videos .viewer').scrollTop(0);
	$('#scroller_videos .next a, #scroller_videos .prev a').click(function(){
		return false;
	})
	
	scroller_videos();
	
	$('a.new_window').click(function(){
		window.open($(this).attr("href"));
		return false;
	})
	
	$('#recommend_video a').click(function () {
		$('#polecName, #polecMail').val('');
		$('#statusPolec').html('');
		$('#polecForm input').removeClass('errorInput');
		$('#div_recommend').jqmShow();
		return false;
	});
	
	$('#add_newsletter a').click(function () {
		$('#nlName, #nlMail').val('');
		document.getElementById('nlZgoda').checked = '';
		$('#statusNl').html('');
		$('#nlForm input').removeClass('errorInput');
		$('#div_newsletter').jqmShow();
		return false;
	});
	
	$('#div_recommend').jqm();
	$('#div_newsletter').jqm();
	
	$('#search_word').focus(function(){
		if($(this).val() == 'szukaj filmu w serwisie') $(this).val('');
	})
	
	$('#search_submit').click(function(){
		if($('#search_word').val() == '') $('#search_word').val('szukaj filmu w serwisie'); else
		search($('#search_word').val());
	})
	
	$('#footer_menu > li').click(function(){
		var id = $('#footer_menu > li').index(this);
		var page = $('#footer_menu > li:eq(' + id + ') > a').attr('href');
		
		getContent("page", page);
		return false;
	});
	
	menu_scrolling();
	
	/* preloader images */
	$.preloadImages('/data/img/loader.gif','/data/img/loader_texts.gif');
})


$(function(){
	$('.scrolling').jScrollPane({scrollbarWidth:23, scrollbarMargin:23, dragMinHeight:34, dragMaxHeight:34});
})


/* all functions */
jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){ jQuery("<img>").attr("src", arguments[i]); }
}

function menu_scrolling(){
	$('#navi .menu').jScrollPane({scrollbarWidth:23, scrollbarMargin:23, dragMinHeight:34, dragMaxHeight:34});
}

var regEmail = /^.+\@.+\..+$/;

function encodeString(text) {
	encoded = text.replace("///g","%2F");
	encoded = encoded.replace("/?/g","%3F");
	encoded = encoded.replace("/=/g","%3D");
	encoded = encoded.replace("/&/g","%26");
	encoded = encoded.replace("/#/g","%23");
	encoded = encoded.replace("/@/g","%40");
	encoded = encoded.replace("/r/g","");
	encoded = encoded.replace("/n/g","%0A");

	return encodeURIComponent(encoded);
}

function showMenu(link_recommend) {
	$('#polecLink').val(link_recommend);
	
	$('#navi').fadeOut('slow', function () {
		$('#arrow_up, #arrow_down').fadeOut('slow');
		$('#navi').html('<div class="loader"><img src="/data/img/loader.gif" alt="" /></div>');
		$('#navi').fadeIn('slow');
	});
	
	$.ajax({
		 type: "GET",
		 url: "/inc/ajax_show_menu.php",
		 success: function(msg){
		 $('#navi').fadeIn('slow', function () {
				$('#navi').hide();
				$('#navi').html(msg);
				$('#navi').fadeIn('slow');
			});
		 }
	 });
}


function showCats(ID, link_recommend) {
	$('#polecLink').val(link_recommend);
	
	$('#navi').fadeOut('slow', function () {
		$('#navi').html('<div class="loader"><img src="/data/img/loader.gif" alt="" /></div>');
		$('#navi').fadeIn('slow');
	});
	
	send_to_ga(link_recommend);
	getPromotionLinks(ID);
	getContent("section", ID);

	$.ajax({
		 type: "GET",
		 url: "/inc/ajax_show_cats.php",
		 data: "ID="+ID,
		 success: function(msg){
		 $('#navi').fadeIn('slow', function () {
				$('#navi').hide();
				$('#navi').html( msg );
				$('#navi').fadeIn('slow');
				menu_scrolling();
			});
		 }
	 });
}


function showItems(ID_cat, ID, link_recommend) {
	$('#polecLink').val(link_recommend);
	
	$('#navi').fadeOut('slow', function () {
		$('#navi').html('<div class="loader"><img src="/data/img/loader.gif" alt="" /></div>');
		$('#navi').fadeIn('slow');
	});
	
	send_to_ga(link_recommend);
	getPromotionLinks(ID);
	getContent("category", ID_cat);

	$.ajax({
		 type: "GET",
		 url: "/inc/ajax_show_items.php",
		 data: "ID_cat="+ID_cat+"&ID_menu="+ID,
		 success: function(msg){
		 $('#navi').fadeIn('slow', function () {
				$('#navi').hide();
				$('#navi').html( msg );
				$('#navi').fadeIn('slow');
				menu_scrolling();
			});
		 }
	 });
}

function search(word){
	$('#search input').attr('disabled','disabled');
	
	$('#navi').fadeOut('slow', function () {
		$('#navi').html('<div class="loader"><img src="/data/img/loader.gif" alt="" /></div>');
		$('#navi').fadeIn('slow');
	});
	
	$.ajax({
		 type: "POST",
		 url: "/inc/ajax_search.php",
		 data: "word="+encodeString(word),
		 success: function(msg){
		 $('#navi').fadeIn('slow', function () {
				$('#navi').hide();
				$('#navi').html( msg );
				$('#navi').fadeIn('slow');
				menu_scrolling();
				$('#search input').removeAttr('disabled');
			});
		 }
	 });
}


function showLink(link, okno){
	if(okno == "T") window.open(link); else window.location = link;
}

function playFLV(fileVideo, id, link_recommend){
	$('#polecLink').val(link_recommend);
	
	send_to_ga(link_recommend);
	getContent("item", id);
	
	var type = fileVideo.substr(fileVideo.length-3,3);

	if(type == 'png' || type == 'jpg'){
		$('#video').html("<img src='"+ fileVideo +"' alt='' />");
	} else {
		$('#video').css("background-color", "#000");
		
		var so = new SWFObject("/fms/swfs/videoPlayer.swf", "swf_player", "640", "377", "9", "#000");
		so.addParam("wmode", "transparent");
		so.addParam("allowFullScreen", "true");
		so.addVariable("&videoWidth", "0");
		so.addVariable("videoHeight", "0");
		so.addVariable("dsControl", "manual");
		so.addVariable("dsSensitivity", "100");
		so.addVariable("DS_Status", "true");
		so.addVariable("streamType", "vod");
		so.addVariable("autoStart", "true");
		so.addVariable("serverURL", fileVideo);
		
		so.write("player");
	};
}

function getContent(type, id){
	if(id > 0 || type == "page"){
		$("#left_text").hide();
		$("#left_text").html('<p style="text-align: center; padding-top: 40px;"><img src="/data/img/loader_texts.gif" alt="" /></p>');
		
		$.ajax({
			type: "POST",
			url: "/inc/ajax_get_content.php",
			data: "type="+type+"&id="+id,
			success: function(msg){
				$('#left_text').fadeIn('slow', function (){
					$('#left_text').hide().html(msg).fadeIn('slow');
					$('#text_3').jScrollPane({scrollbarWidth:23, scrollbarMargin:23, dragMinHeight:34, dragMaxHeight:34});
				});
			}
		});
	}
}

function getPromotionLinks(id){
	if(id > 0){
		$("#right_text").hide();
		$("#right_text").html('<p style="text-align: center; padding-top: 40px;"><img src="/data/img/loader_texts.gif" alt="" /></p>');
		
		$.ajax({
			type: "POST",
			url: "/inc/ajax_get_promotion_links.php",
			data: "id="+id,
			success: function(msg){
				$('#right_text').fadeIn('slow', function (){
					$('#right_text').hide().html(msg).fadeIn('slow');
					$('#promotion_links').jScrollPane({scrollbarWidth:23, scrollbarMargin:23, dragMinHeight:34, dragMaxHeight:34});
				});
			}
		});
	}
}

/* popups*/
function sendPolec(){
	$('#statusPolec').html('<img src="/data/img/loader.gif" alt="" style="vertical-align: middle; float: left; padding-right: 5px;" /> Wysyłanie maila');
	$('#polecForm input').attr('disabled','disabled');
	$('#polecForm label span').remove();

	var status = '';
	if($('#polecName').val() == ''){
		//$('#polecName').prev('label').append(' <span>Wymagane!</span>');
		status = "Uzupełnij osobę polecającą.";
	}
	if($('#polecMail').val() == '' && status == ''){
		//$('#polecMail').addClass('errorInput');
		status = "Popraw zaznaczone pola.";
		status = "Uzupełnij e-mail znajomego.";
	}
	if(!regEmail.test($('#polecMail').val()) && status == ''){
		//$('#polecMail').addClass('errorInput');
		status = "Błędny adres e-mail.";
	}

	if(status == ''){
		$.ajax({
		 type: "POST",
		 url: "/inc/ajax_send_polec.php",
		 data: "polecName="+encodeString($('#polecName').val())+"&polecMail="+encodeString($('#polecMail').val())+"&polecLink="+encodeString($('#polecLink').val()),
		 success: function(msg){
			$('#statusPolec').fadeIn('fast', function () {
				$('#statusPolec').hide();
				$('#statusPolec').html( msg );
				$('#statusPolec').fadeIn('slow');
			});
			}
		})
	} else {
		$('#statusPolec').html(status);
	}
	
	$('#polecForm input').removeAttr('disabled');
	
	return false;
}

function showPolec(){ $('#div_recommend').jqmShow(); }

function sendNewsletter(){
	$('#statusNl').html('<img src="/data/img/loader.gif" alt="" style="vertical-align: middle; float: left; padding-right: 5px;" /> Wysyłanie maila');
	$('#nlForm input').attr('disabled','disabled');
	$('#nlForm input').removeClass('errorInput');

	var status = '';
	
	if($('#nlName').val() == ''){
		//$('#nlName').addClass('errorInput');
		status = "Uzupełnij Imię i nazwisko.";
	}
	if($('#nlMail').val() == '' && status == ''){
		//$('#nlMail').addClass('errorInput');
		status = "Uzupełnij e-mail.";
	}
	if(!regEmail.test($('#nlMail').val()) && status == ''){
		//$('#nlMail').addClass('errorInput');
		status = "Błędny adres e-mail.";
	}
	
	if(document.getElementById('nlZgoda').checked == '' && status == '') { status = "Prosimy o wyrażenie zgody."; }

	if(status == ''){
		$.ajax({
		 type: "POST",
		 url: "/inc/ajax_save_newsletter.php",
		 data: "nlName="+encodeString($('#nlName').val())+"&nlMail="+encodeString($('#nlMail').val()),
		 success: function(msg){
			$('#statusNl').fadeIn('fast', function () {
				$('#statusNl').hide();
				$('#statusNl').html( msg );
				$('#statusNl').fadeIn('slow');
			});
			}
		})
	} else {
		$('#statusNl').html(status);
	}
	
	$('#nlForm input').removeAttr('disabled');
	
	return false;
}

function send_to_ga(link){
	try {
	var pageTracker = _gat._getTracker('UA-12030796-1');
	pageTracker._trackPageview(link.substr(21));
	} catch(err) {}
}


function scroller_videos(){
	var scroller_videos = $('#scroller_videos');
	var viewer = scroller_videos.find('.viewer');
	
	scroller_videos.hover(function() {
		viewer.stop();
		clearTimeout(scroller_videos_timeout);
	}, function() {
		scroller_videos_timeout = setTimeout('scroller_videos_viewer(0);', 1000);
	});
	
	$('#scroller_videos .next a, #scroller_videos .prev a').mouseup(function(){
		viewer.stop();
		scroller_videos_time = 25000;
	}).mousedown(function(){
		viewer.stop();
		clearTimeout(scroller_videos_timeout);
		scroller_videos_time = 3000;
		if($(this).parent().attr('class') == 'next')
			scroller_videos_direction = 'to_right';
		else
			scroller_videos_direction = 'to_left';
		
		scroller_videos_viewer(1);
	});
	
	setTimeout("scroller_videos_viewer(0);", 3000);
}

function scroller_videos_viewer(stop){
	var viewer = $('#scroller_videos .viewer');
	
	if(scroller_videos_direction == 'to_right') var width = viewer.find('.items').width()-912;
	if(scroller_videos_direction == 'to_left') var width = 0;
	
	var time_2 = scroller_videos_time * (viewer.find('.items').width() - 912 - viewer.scrollLeft()) / (viewer.find('.items').width() - 912);
	
	if(scroller_videos_direction == 'to_left') time_2 = scroller_videos_time - time_2;
	
	viewer.scrollTo({top:'0px',left:width+'px'}, Math.round(time_2), {
		onAfter:function(){
			if(stop == 0){
				if(scroller_videos_direction == 'to_right')
					scroller_videos_direction = 'to_left';
				else if(scroller_videos_direction == 'to_left')
					scroller_videos_direction = 'to_right';
					
				scroller_videos_timeout = setTimeout('scroller_videos_viewer(0);', 1000);
			}
		}
	});
}