﻿	this.imagePreview = function()
	{
		//console.log("imgPreview()");
		
		xOffset = 10;
		yOffset = 30;
		
		$(".preview").hover(function(e)
		{
			
			var a = this.src.split("?");
			var b = a[1].split("&");
			var prefix = a[0] + "?" + b[0];
			
			prefix = prefix + "&width=320&height=240&crop=";
			
			//console.log(prefix);

			//nie chowa obrazków
			var wys= (240 + e.pageY - $(document).scrollTop()-$(window).height() );
			var szer= (320 + e.pageX - $(document).scrollLeft()-$(window).width() );
			
			this.t = this.title;
			this.title = "";
			var c = (this.t != "") ? "<br/>" + this.t : "";
			$("body").append("<p id='preview'><img src='"+ prefix +"' alt='Loading image' />"+ c +"</p>");			

			if (wys<0){wys=0;}
			if (szer<0){szer=0;}else{szer=320+ yOffset+ yOffset;}

 				 
			$("#preview")

				.css("top",(e.pageY - xOffset-wys) + "px")
				.css("left",(e.pageX + yOffset-szer) + "px")
				.fadeIn("fast");						
		},
		function()
		{
			this.title = this.t;	
			$("#preview").remove();
		});	
		$(".preview").mousemove(function(e)
		{
			var wys= (240 + e.pageY - $(document).scrollTop()-$(window).height() );
			var szer= (320 + e.pageX - $(document).scrollLeft()-$(window).width() );

			if (wys<0){wys=0;}
			if (szer<0){szer=0;}else{szer=320+ yOffset+ yOffset;}
			$("#preview")				
				.css("top",(e.pageY - xOffset-wys) + "px")
				.css("left",(e.pageX + yOffset-szer) + "px");
		});			
	};




$(document).ready(function()
{

	imagePreview();
	
	var stopRotation = [0, 0];
	
	$('[id^="featured_news"] > *').hover(function()
	{
		stopRotation[1] = 1;
	},
	function()
	{
		stopRotation[1] = 0;
	});
	
	
	function send_pm(priv)
	{
		//console.log(priv);
	
		var urg = 0;
		var acpt = 0;
		
		if ($("input[name=pilna]").attr("checked"))
			urg = 1;
	
		if ($("input[name=accept]").attr("checked") || $("input[name=accept]").val() == '')
			acpt = 1;
			
		//console.log(acpt);
			
		
		var msg = $("[name=nazwa]").val();
		msg = jQuery.trim(msg);

		var mail = "";
		var defmess = $("#priv_msg_def").val();
		
		var bMail = false;
		
		if ($("#srcmail").length) {
			mail = $("#srcmail").val();
			
			if (mail.length > 3)
				bMail = true;
			else
				bMail = false;
		}
		else {
			mail = "";
			bMail = false;
		}
		bSend = true;
		var alert_text = '';
		if (defmess == msg) {
			alert_text += 'Please do not use default text!<br/>';
			bSend = false;
		}
		if (msg.length < 20 || msg.length > 1500) {
			alert_text += 'Please type 20 to 1500 characters!<br/>';
			bSend = false;
		}

		if (!bMail) {
			alert_text += 'Please fill in the E-Mail field if you are not logged in!<br/>';
			bSend = false;
		}
		if (!acpt){
			alert_text += 'Please accept terms!<br/>';
			bSend = false;
		}
		
		if (bSend == false) {
			$("#msgReport").html(alert_text);
			$("#msgReport").dialog("option", "title", "");
			$("#msgReport").dialog("open");
			return false;
		}
		
		var order = "accept=" + acpt + "&pilne=" + urg + "&nazwa=" + msg;
		
		order += "&destmail=" + $("#destmail").val();
		order += "&srcmail=" + $("#srcmail").val();
		if ($("#companyid"))
			order += "&companyid=" + $("#companyid").val();
		if (priv != "0")
			order += "&private=" + priv;
		
		//console.log(order);
		
 		$.post("includes/private_message.php", order, function(data)
		{
			//alert(data);
			var txtSearchDefault = "";
			
			$("#msgReport").html(data);
			$("#msgReport").dialog("option", "title", "");
			$("#msgReport").dialog("open");
			$("[name=nazwa]").val(txtSearchDefault);
			
			//alert("Message sent.");
		});
		
		
		
		//return false;
	}
	
	$("#sendPM").click(function()
	{
		var v = $("#priv_msg").val();
		//console.log("v=" + v);
		//$("#middle").append("<div>" + v + "</div>");
		
		send_pm(v);
			
		return false;
	});
	
	
	$("#msgReport").dialog(
	{
		autoOpen: false, 
		resizable: false, 
		modal: true, 
		minWidth: '300px',
		minHeight: '300px',
		maxWidth: '600px',
		maxHeight: '500px',
		dialogClass: 'alert', 
		draggable: 'false'
	});
	
	
	
	$(".statstop .text").click(function()
	{
		//window.location = $("#trendsLink").attr('href');
		window.location = 'http://alladyn.net/,,1,trends.html';
	});
	
	$(".bgwdtop .text").click(function()
	{
		//window.location = $("#featured_title2 a").attr('href');
		window.location = 'http://alladyn.net/,,1,news.html';
	});
	
	
	

	$('#pl_show').click(function()
	{
		$('#roll_pl').toggle();
	});
	

	$('#en_show').click(function()
	{
		$('#roll_en').toggle();
	});
	
	
	$('#offerShortDescription').keyup(function()
	{
		$('#chars_left_pl').html(function()
		{
			return "Wpisz tytul <span style='font-size: 10px; font-weight: normal;'>(pozostało Ci " +(50 - $('#offerShortDescription').val().length)+ " znaków)</span>";
		});
		
	});
	
	$('#offerShortDescription_en').keyup(function()
	{
		//alert();
		$('#chars_left_en').html(function()
		{
			return "Wpisz tytul po angielsku <span style='font-size: 10px; font-weight: normal;'>(pozostało Ci " +(50 - $('#offerShortDescription_en').val().length)+ " znaków)</span>";
		});
		
	});
	
	
	
	$('#slider-code').tinycarousel({ pager: false, interval: 2000  });	
	$('#slider-code').tinycarousel({ pager: false, interval: 2000  });
	


	
	//domyslny tekst w polach tekstowych
	$(".defaultText").focus(function(srcc){
		if ($(this).val() == $(this)[0].title){
			$(this).val("");
		}
	});				     
	$(".defaultText").blur(function(){
		if ($(this).val() == ""){
			$(this).val($(this)[0].title);
		}
	});
	$(".defaultText").blur();
	$(".text2").focus(function(){
		$(this).select();
	});
	$(".text2").keyup(function(){
		var value = $(this).val();
		$("#sckeyword").val(value);
	}).keyup();

	
	//newsy w naglowku
	$('#country_list a').click(function(e){
		var e = $(this);
		$('#country_list a').removeClass('active');
		e.addClass('active');
		$('#news_list ul').css('display','none');
		$('#news_list ul.'+e.attr('rel')).css('display','block');
		return false;
	});
	
	//wyroznione newsy
	
	
	
	var visible_items = 4;
	var total_items   = $('.slider li').size(); 
	var item_width = 73;
	//wstepne ustawienie linkow prev / next
	
	$('#featured_news #prev a').css('display','block');

	$('#featured_news #next a').click(function(e){		
		var obj = $(this);		
		id=obj.attr('rel');
		nid = parseInt(parseInt(id)+1);
		if (nid > 5)
			nid = 1;
		if (nid < 1)
			nid = 5;
		obj.attr('rel',nid);
		obj = $('#fn_'+nid);
		change_featured_content(nid, obj);
		return false;
	});

	$('#featured_news #prev a').click(function(e){
		var obj = $(this);		
		id=obj.attr('rel');
		nid = parseInt(parseInt(id)-1);
		if (nid > 5)
			nid = 1;
		if (nid < 1)
			nid = 5;
		obj.attr('rel',nid);
		obj = $('#fn_'+nid);
		change_featured_content(nid, obj);
		return false;
	});
	
	$('#featured_controls a').click(function(e){
		var obj = $(this);			
		change_featured_content(obj.attr('rel'), obj);
		return false;
	});


	//wstepne ustawienie linkow prev / next w newsach
	
	$('#featured_news2 #prev2 a').css('display','block');

	$('#featured_news2 #next2 a').click(function(e){		
		var obj = $(this);		
		idx=obj.attr('rel');
		nid2 = parseInt(parseInt(idx)+1);
		if (nid2 > 5)
			nid2 = 1;
		if (nid2 < 1)
			nid2 = 5;
		obj.attr('rel',nid2);
		obj = $('#fn_'+nid2);
		change_featured_content2(nid2, obj);
		return false;
	});

	$('#featured_news2 #prev2 a').click(function(e){
		var obj = $(this);		
		idx=obj.attr('rel');
		nid2 = parseInt(parseInt(idx)-1);
		if (nid2 > 5)
			nid2 = 1;
		if (nid2 < 1)
			nid2 = 5;
		obj.attr('rel',nid2);
		obj = $('#fn_'+nid2);
		change_featured_content2(nid2, obj);
		return false;
	});
	$('#featured_controls2 a').click(function(e){
		var obj = $(this);			
		change_featured_content2(obj.attr('rel'), obj);
		return false;
	});

	
	//wiadomosci wg kraju - wybor jezyka
	$("#news_select_language_container").mouseover(function() {
		$('#countries_list').css('overflow','visible');
		$('#countries_list ul').css('background-color','#095F98');
	}).mouseout(function() {
		$('#countries_list').css('overflow','hidden');
		$('#countries_list ul').css('background-color','transparent');
	});

	$('#news_select_language a.arrow').click(function(){
		return false;
	});
	/*
	$('#countries_list ul li a').click(function(e){
		var e = $(this);
		$('#selected_country').attr('src','gfx/news_flags/'+e.attr('rel')+'.png').attr('alt',e.attr('rel').toUpperCase());
		$('#country_news div.news_list').css('display','none');
		$('#country_news p.gg').css('display','none');
		$('#country_news div.'+e.attr('rel')).css('display','block');
		$('#country_news p.'+e.attr('rel')).css('display','block');
		$('#countries_list').css('overflow','hidden');
		$('#countries_list ul').css('background-color','transparent');
		return false;
	});
	*/
	//rynki wg kraju - wybor jezyka
	$("#news_select_language_container2").mouseover(function() {
		$('#countries_list2').css('overflow','visible');
		$('#countries_list2 ul').css('background-color','#095F98');
	}).mouseout(function() {
		$('#countries_list2').css('overflow','hidden');
		$('#countries_list2 ul').css('background-color','transparent');
	});

	$('#news_select_language2 a.arrow').click(function(){
		return false;
	});

	$('#countries_list2 ul li a').click(function(e){
		var e = $(this);
		$('#selected_country2').attr('src','gfx/news_flags/'+e.attr('rel')+'.png').attr('alt',e.attr('rel').toUpperCase());
		$('#country_news2 div.news_list').css('display','none');
		$('#country_news2 div.'+e.attr('rel')).css('display','block');
		
		$('#countries_list2').css('overflow','hidden');
		$('#countries_list2 ul').css('background-color','transparent');
		return false;
	});
	
	//zaufali nam
	//$('#slide_holder').agile_carousel({});

	//menu glowne - lista krajow
	$("#main_menu li").has('a.top').mouseover(function(e) {
		var e = $(this).children();
		e.addClass('selected');
		$(this).css('background-image','url("gfx/main_menu_hover_bg.gif")');
		var menu = "#"+e.attr('rel');
		var offset = e.offset();
		var left = parseInt(offset.left - ((128 - ($(this).width())) / 2));	
		var top = 149;
		$(menu).css('left',left+'px');
		$(menu).css('top',top+'px');
		$(menu).css('z-index','1000');
		$(menu).show();
	}).mouseout(function() {
		$(this).css('background-image','url("gfx/blank.gif")');
		var e = $(this).children();
		e.removeClass('selected');
		var menu = "#"+e.attr('rel');
		$(menu).hide();
	});

	//Finanse / sonda / raporty
	$('#boxes_titles_right_column a').click(function(e){
		var e = $(this);
		var the_box = e.attr('rel');
		$('#boxes_titles_right_column a').removeClass('active');
		e.addClass('active');
		$('#boxes_right_column div').css('display','none');
		$('#boxes_right_column div#'+e.attr('rel')).fadeIn('fast');
		return false;
	});
	
	//partnerzy
	/*$('#partners').ctRotator(partnerzy, {
		speed:4000,
		showCount: 1,
		itemRenderer:function(item){
			return '<li class="ctrotator-item"><a href="' +
			item.url+ '"><img src="' + item.image +
			'" alt="' + item.title + '"/></a></li>';
		}
	});
*/
	//tlumaczenia - rynek , ludzie, opinie
	$('a.original').click(function(e){
		var e = $(this);
		var element = '#' + e.attr('rel');
		if (e.attr('rel') == 'wd' || e.attr('rel') == 'trend') {
			for (i=0;i<=e.attr('alt');i++) {
				var original_text = $(element+'_'+i+'_original').html();
				$(element+'_'+i).html(original_text);
			}
			//$('#bbl').load("slang.php", {'lang':e.attr('title') ,'type':e.attr('rel')} );
		}
		else if (e.attr('rel') == 'articlex') {
			var original_text = $('#article_h_original').html();
			$('#article_h').html(original_text);
			var original_text = $('#article_l_original').html();
			$('#article_l').html(original_text);
			/*
			var original_text = $('#article_c_original').html();
			$('#article_c').html(original_text);
			*/
				var original_text = $('#article_c_'+i+'_original').html();
			for (i=0;i<=e.attr('alt');i++) {
				$('#article_c_'+i).html(original_text);
			}
		}
		else if (e.attr('rel').substr(0,5) == 'dnews') {
			var original_text = $(element+'_original').html();
			$(element).html(original_text);
			for (i=0;i<=e.attr('alt');i++) {
				var original_text = $(element+'_'+i+'_original').html();
				$(element+'_'+i).html(original_text);
			}
		}
		else {
			var original_text = $(element+'_original').html();
			$(element).html(original_text);
			//$('#bbl').load("slang.php", {'lang':e.attr('alt') ,'type':e.attr('title')} );
		}
		$(element+'_o').hide();
		$(element+'_t').show();
		
		return false;
	});

	$('a.translate').click(function(e){
		var e = $(this);
		var element = '#'+ e.attr('rel');
		if (e.attr('rel') == 'wd' || e.attr('rel') == 'trend') {
			for (i=0;i<=e.attr('alt');i++) {
				var translated_text = $(element+'_'+i+'_translated').html();
				$(element+'_'+i).html(translated_text);
			}
			//$('#bbl').load("slang.php", {'lang':e.attr('title') ,'type':e.attr('rel')} );
		}
		else if (e.attr('rel') == 'articlex') {
			var translated_text = $('#article_h_translated').html();
			$('#article_h').html(translated_text);
			var translated_text = $('#article_l_translated').html();
			$('#article_l').html(translated_text);
			/*
			var translated_text = $('#article_c_translated').html();
			$('#article_c').html(translated_text);
			*/
			for (i=0;i<=e.attr('alt');i++) {
				var translated_text = $('#article_c_'+i+'_translated').html();
				$('#article_c_'+i).html(translated_text);
			}
		}
		else if (e.attr('rel').substr(0,5) == 'dnews') {
			var translated_text = $(element+'_translated').html();
			$(element).html(translated_text);
			for (i=0;i<=e.attr('alt');i++) {
				var translated_text = $(element+'_'+i+'_translated').html();
				$(element+'_'+i).html(translated_text);
			}
		}
		else {
			var translated_text = $(element+'_translated').html();
			$(element).html(translated_text);
			//$('#bbl').load("slang.php", {'lang':e.attr('alt') ,'type':e.attr('title')} );
		}
		$(element+'_t').hide();
		$(element+'_o').show();
		return false;
	});
	
	

	if (document.getElementById('branding'))
		google.language.getBranding('branding');
	
	
});
var timeoutId;
//zmiana wyroznionego newsa
	function change_featured_content(id, e, ab, ba){
		//tytuly
		$('#featured_controls a').removeClass('active');
		e.addClass('active');
		sLink = '<a href="'+featuredNews[id].url+'">';
		eLink = '</a>';
		//zdjecie
		$('#trendslink').attr('href', featuredNews[id].url);
		
		$('#featured_image').fadeTo('fast','0.1', function(){
			$('#featured_image img').css('margin-top', '8px');
			$('#featured_image a').attr('href',featuredNews[id].url);
			$('#featured_image img').attr('src',featuredNews[id].image);
		});
		$('#featured_image').fadeTo('fast','1');
		sContent = '<span style="font:11px Verdana"> - '+featuredNews[id].content.slice(0,70)+"</span>";
		//tytul
		$('#featured_title').html(sLink+featuredNews[id].title+sContent+eLink);
		
		$("#featured_title").position({
			my: "left top",
			at: "left bottom",
			of: $("#featured_image"), // or $("#otherdiv)
			collision: "fit"
		})
		
		$('#featured_content').html(sLink+featuredNews[id].content+eLink);
		


		nid = parseInt(parseInt(id) + 1);

		first_by_country = $($(e).parent().parent().attr('class')+'a:first').attr('id');

		cat = $(e).parent().parent().attr('class');
		min = parseInt($('#featured_controls ul.'+cat+' li a:first').attr('rel'));
		max = parseInt($('#featured_controls ul.'+cat+' li a:last').attr('rel'));
		
		
		
		$('#fn_'+ba).click(function(e){
					
			ab=$('#fn_'+ba);	
			//alert(ab);
			
			if (ba != nid){
			ba = nid;
		}});
		

		
		if(nid > max){
			nid = min;
		}
		
		
		window.clearTimeout(timeoutId);
		
		timeoutId = setTimeout("change_featured_content($('#featured_controls a#fn_'+nid).attr('rel'), $('#featured_controls a#fn_'+nid))",6000);
		
	};
	
	
	var timeoutIdx;	
//zmiana wyroznionego newsa
	function change_featured_content2(idx, e){
		
		//tytuly
		$('#featured_controls2 a').removeClass('active');
		e.addClass('active');
		sLink = '<a href="'+featuredNews2[idx].url+'">';
		eLink = '</a>';
		//zdjecie
		
		$('#newslink').attr('href', featuredNews2[idx].url);
		
		$('#featured_image2').fadeTo('fast','0.1', function() {
			$('#featured_image2 img').css('margin-top', '0px');
			$('#featured_image2 a').attr('href',featuredNews2[idx].url);
			$('#featured_image2 img').attr('src',featuredNews2[idx].image);
			
		});
		$('#featured_image2').fadeTo('fast','1');
		
		//tytul 
		$('#featured_title2').html(sLink+featuredNews2[idx].title+eLink);
		$('#featured_content2').html((sLink+featuredNews2[idx].content+eLink).replace('<br/>', ' '));
		
		if ((sLink+featuredNews2[idx].title+eLink).length < 100)
		{
			$('#featured_content2').css("height", "143px");
			//console.log("<100: " + (sLink+featuredNews2[idx].title+eLink).length + $('#featured_content2').css("height"));
		}
		else if ((sLink+featuredNews2[idx].title+eLink).length > 240)
		{
			$('#featured_content2').css("height", "110px");
			//console.log(">100: " + (sLink+featuredNews2[idx].title+eLink).length + $('#featured_content2').css("height"));
		}
		else
		{
			$('#featured_content2').css("height", "128px");
			//console.log("else: " + (sLink+featuredNews2[idx].title+eLink).length + $('#featured_content2').css("height"));

		}

		nid2 = parseInt(parseInt(idx) + 1);

		first_by_country = $($(e).parent().parent().attr('class')+'a:first').attr('idx');

		cat2 = $(e).parent().parent().attr('class');
		min2 = parseInt($('#featured_controls2 ul.'+cat+' li a:first').attr('rel'));
		max2 = parseInt($('#featured_controls2 ul.'+cat+' li a:last').attr('rel'));
		
		if(nid2 > max2){
			nid2 = min2;
		}
		window.clearTimeout(timeoutIdx);
		
		timeoutIdx = setTimeout("change_featured_content2($('#featured_controls2 a#fn_'+nid2).attr('rel'), $('#featured_controls2 a#fn_'+nid2))",9000);
		
	};

