$(document).ready(function() 
{
	//bo czrne ramki przy ie jak fade na png transparent
	
	
	$("a").each(function(e){
		if ($(this).attr("title") == null)
		{
			
			$(this).attr("title", $(this).attr("href"));
		}
	});
	
	var zwin_label = $('#zwin_name').attr('rel');
	var rozwin_label = $('#rozwin_name').attr('rel');
	var czekaj_label = $('#czekaj_name').attr('rel');
	
	var hash = window.location.hash;

	if(hash == '#biomasa')
	{
		$("#uzytkownik_zapytanie_temat_wspolpraca option[value='1']").attr("selected", "selected");	
	}
	else if(hash == '#kogeneracja')
	{
		$("#uzytkownik_zapytanie_temat_wspolpraca option[value='2']").attr("selected", "selected");	
	}
	
	$('a[rel="kotwica"]').click(function(e)
	{
		e.preventDefault();
		var link = $(this).attr('href');
		if(link != null)
		{	
			$(link).find("div.hidden_part").show(); 
			
			$(link+' :first-child').attr("title", zwin_label);
			$(link).find('a.show').hide();
			//$(link).find('a.hide').show();
			
			var fooOffset = $(link).offset(),
	        destination = fooOffset.top;
			jQuery(document).scrollTop(destination);		
		}		
	})
	
		
		
	
	$(function(){
	    $('a[rel="external"]').attr('target','_blank');
	  });
	
	
	$("form input").first().select();																												
	$(".red_border").first().select(); 
	
	function parseQuery ( query ) {
	     var Params = new Object ();
	     if ( ! query ) return Params; // return empty object
	     var Pairs = query.split(/[;&]/); 
	     for ( var i = 0; i < Pairs.length; i++ ) {
	        var KeyVal = Pairs[i].split('=');
	        if ( ! KeyVal || KeyVal.length != 2 ) continue;
	        var key = unescape( KeyVal[0] );
	        var val = unescape( KeyVal[1] );
	        val = val.replace(/\+/g, ' ');
	        Params[key] = val;
	     }
	     return Params;
	  }
	
	var tempActive = null;
		
	function calendar(year)
	{
		$("div#calendar").text(" "+czekaj_label+"...");
		var url= "news/calendar";
	     var action = url.split("?");
	     var queryString = url.replace(/^[^\?]+\??/,'');
	     params = parseQuery( queryString );	    
	     $('#calendar').load(action[0]+year, function(a,b,c){
	    	 $("div#calendar div.left_arrow a").click(function (e)
			{
	    		 var year = parseInt($("div#calendar div.table_calendar p").text());	
	    		 $("div#calendar").text(" "+czekaj_label+"...");
	    		 e.preventDefault();
				year--;				
				calendar("?year="+year);				
			});
			
			$("div#calendar div.right_arrow a").click(function (e)
			{
				var year = parseInt($("div#calendar div.table_calendar p").text());
				$("div#calendar").text(" "+czekaj_label+"...");
				e.preventDefault();				
				year++;				
				calendar("?year="+year);
			});
			
			 $("div#calendar div.table_calendar table a").click(function (e)
			{			 
				 $("div#calendar").hide();
				 $("div#content_text").html("<p>"+czekaj_label+"...</p>");
				 if(tempActive != null)
				 {
					 tempActive.css('background', '#A3D29D'); 
				 }
				 $(this).parent().css('background', '#49A53B');
				 tempActive = $(this).parent();
				  e.preventDefault();
	    		 var url= "news/datamonth";
			     var action = url.split("?");
			     var queryString = url.replace(/^[^\?]+\??/,'');
			     params = parseQuery( queryString );			  
			     $('div#content_text').load(action[0]+'?year='+$("div#calendar div.table_calendar p").text()+"&month="+$(this).text(), function(a,b,c){
			    	 $("div#calendar").show();			    	 
			     })				
			});			
			 
			 $("div#calendar div.table_calendar a.white").click(function (e)
				{			 
					 $("div#calendar").hide();
					 $("div#content_text").html("<p>"+czekaj_label+"...</p>");
					 if(tempActive != null)
					 {
						 tempActive.css('background', '#A3D29D'); 
					 }				
					 tempActive = null;
					 e.preventDefault();
		    		 var url= "news/datayear";
				     var action = url.split("?");
				     var queryString = url.replace(/^[^\?]+\??/,'');
				     params = parseQuery( queryString );				   
				     $('div#content_text').load(action[0]+'?year='+$("div#calendar div.table_calendar p").text(), function(a,b,c){
				    	 $("div#calendar").show();			    	 
				     })				
				});			
			
	     });
	     
	     $("div#calendar").show();
	};
	
	calendar("");
	 
	
	$("div.hidden_news").hide();
	
	
	var image_path = $("div.box div.news p.date a").first().attr('rel');
	
	
	
	if(image_path != null)
	{
		image_path = image_path.substr(0, image_path.length-2);
		
		$("div.box div.news p.date a").append("<img src='"+image_path+"/arrow_down.png' alt='Rozwiń' width='13' height='13' />");
		$("div.box div.news p.date a").attr('title', 'Rozwiń');
		$("div.box div.news p.date a").click(function (e)
		{		
			e.preventDefault();
			
			if($(this).attr("title") == rozwin_label)
			{		
				var fooOffset = $(this).parent().parent().offset();
		        destination = fooOffset.top;
				jQuery(document).scrollTop(destination);
				$(this).attr("title", zwin_label);
				$(this).find("img").attr("src", image_path+"/arrow_up.png");
				
			}
			else if($(this).attr("title") == zwin_label)
			{
				$(this).attr("title", rozwin_label);
				$(this).find("img").attr("src", image_path+"/arrow_down.png");
			}				
			$(this).parent().parent().find("div.hidden_news").toggle();
		});
		$("div.box div.news h2").css("cursor", "pointer");
		$("div.box div.news h2").attr('title', rozwin_label);
		
		$("div.box div.news h2").click(function (e)
		{		
			e.preventDefault();
			
			if($(this).attr("title") == rozwin_label)
			{		
				var fooOffset = $(this).parent().parent().offset();
		        destination = fooOffset.top;
				jQuery(document).scrollTop(destination);
				$(this).attr("title", zwin_label);
				$(this).parent().find("img").first().attr("src", image_path+"/arrow_up.png");
				
			}
			else if($(this).attr("title") == zwin_label)
			{
				$(this).attr("title", rozwin_label);
				$(this).parent().find("img").first().attr("src", image_path+"/arrow_down.png");
			}				
			$(this).parent().find("div.hidden_news").toggle();
		});
	}
	
	$("div.box div.news p.date a").css('display', 'block');
	
	if(image_path == null)
	{
		image_path = $("div.unvisible a.hide").first().attr("rel");
		if(image_path != null)
		{
			image_path = image_path.substr(0, image_path.length-2);
		}			
	}	

	$("div.hidden_part").hide();
	//var space= String.fromCharCode(160);
	//$("div.unvisible a.show").text(space+space+space+space+rozwin_label); 
	//$("div.unvisible a.hide").text(space+space+space+space+zwin_label);
	$("div.unvisible a.show").prev().css("cursor", "pointer");
	var variant = $("div.unvisible a.show").first().attr("rel");
	
	$("div.unvisible a.hide").append("<img src='"+image_path+"/button_zwin"+variant+".png' alt='Zwiń' width='51' height='15' />");
	$("div.unvisible a.show").append("<img src='"+image_path+"/button_rozwin"+variant+".png' alt='Rozwiń' width='51' height='15' />");
	$("div.unvisible a.show").prev().attr("title", rozwin_label);
	
	

	$("div.unvisible a.show").prev().click(function (e)
	{
		e.preventDefault();	
		
		$(this).parent().find("div.hidden_part").toggle();
		
		if($(this).attr("title") == rozwin_label)
		{
			$(this).parent().find("a.show").hide();
			//$(this).parent().find("a.hide").show();
			var fooOffset = $(this).parent().offset(),
	        destination = fooOffset.top;
			jQuery(document).scrollTop(destination);
			$(this).attr("title", zwin_label);
			$(this).parent().find("div.hidden_part").show();
		}
		else if($(this).attr("title") == zwin_label)
		{
			$(this).parent().find("a.show").show();
			//$(this).parent().find("a.hide").hide();
			$(this).attr("title", rozwin_label);
			$(this).parent().find("div.hidden_part").hide();
		}	
		
		
		
		
	});
	
	$("div.unvisible a.show").click(function (e) {	 
		$(this).hide();
		e.preventDefault();	
		$(this).parent().find("div.hidden_part").toggle(); 	
		$(this).prev().attr("title", zwin_label);
		
		var fooOffset = $(this).parent().offset(),
        destination = fooOffset.top;
		jQuery(document).scrollTop(destination);
	});
	
	$("div.hidden_part a.hide").click(function (e) {
		
		$(this).parent().parent().find("a.show").show(); 
		$(this).parent().parent().find("h3").attr("title", rozwin_label);
		e.preventDefault();	
		$(this).parent().toggle(); 
	});	

	

	
});
(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
	})(jQuery)


