$(document).ready(function(){
	var link_rating = location.href;
	link_rating = link_rating.split('/');
	if( link_rating[5] == 'show' ){
		$('input.rating_user_nv').rating({
			required: true,
			callback: function(value, link){
				$('#at_article').html('<img src="/images/loading_temp.gif" style="margin:0 auto; display:block;" />');
				$('.rating_grades').html($(this).val());
				$(this).rating('readOnly',true);
				var rate = $(this).val();
				$.ajax({
					type: "post",
				   	url: "/rate_article",
				   	data: "rate_article=1&article_id="+article+"&rate="+rate,
				   	dataType: "html",
				   	success: function(msg){
				   		if(msg != 0){
				   			msg = msg.split('_');
				   			var avarage = msg[0];
					   		var total_votes = msg[1];
					   		var star_index = msg[2];
					   		var i = 1;
					   		
					   		var content = '<div class="rating_results"><span class="strong">Ocena ogólna</span></div>';
					   		while (i <= 20){
					   			if(i == star_index){
					   				content = content+'<input name="au_article" type="radio" class="rating_total" value="'+i+'" checked="checked"/>';
					   			}
					   			else{
					   				content = content+'<input name="au_article" type="radio" class="rating_total" value="'+i+'"/>';
					   			}
					   			i++;
					   		}
					   		content = content+' <span class="rating_grades">'+avarage+' ('+total_votes+')</span>';
					   		$('#at_article').html(content);
					   		$('input.rating_total').rating({
					   			required: true,
					   			split: 4,
					   			readOnly: true
					   		})
			   			}
			   		}
				});
			}
		});
		$('input.rating_user_v').rating({
			required: true,
			readOnly: true
		});
		$('input.rating_total').rating({
			required: true,
			split: 4,
			readOnly: true
		});
	}
	
	
	
	
	
	
	$("#add_div_right").hide();
	$("#add_image_right").click(function () {
      $("#add_div_right").slideToggle("slow");
    });
	
	var link = location.href;
	link = link.split('/');
	if( link[link.length-1] == 'show' ){
		$('.comm_cont').truncate({max_length: 200, more: 'rozwiń', less: 'zwiń'});
	}

	// [P]:Start

	$(".add_subcomment_form").hide();
	$(".add_subcomment_form_span").click(function () {
		$("#" + $(this).attr("id") + "F").toggle("slow");
   	});

   	$(".show_subcomments").click(function() {
 		$("#" + $(this).attr("id") + "C").fadeIn();
		$("#" + $(this).attr("id") + "H").show();
		$(this).hide();
		$("#" + $(this).attr("id") + "H").click(function () {
			ids = $(this).attr("id").substr(0,$(this).attr("id").length-1);
			$("#" + ids).show();
			$("#" + ids + "C").fadeOut();
			$(this).hide();
		});
   	});

	// [P]:End
	
	//////////////////////////////////////////////// FUNCTIONS /////////////////////////////////////////////////////////
	function GetCity(country_id){
		$.ajax({
			type: "post",
	   		url: "/add_article",
	   		data: "get_city=1&country_id="+country_id,
	   		dataType: "html",
	   		success: function(msg){
	   			if(msg==0){
	   				$("#city_select_select").html('Brak miast');
	   			}
	   			else{
	   				$("#city_select_select").html('<select name="ci_id" id="form_city"><option value="0">- - bez miasta - -</option>'+msg+'</select>');
				}
				$("#select_city_row").show("fast");
			}
		});	
	}
	
	function GetCurrentPage(page_number){
		$.ajax({
			type: "post",
			url: "/articles/get_content_page",
	   		data: "get_content_page=1&article_id="+article+"&page_number="+page_number,
	   		dataType: "html",
	   		success: function(msg){
	   			if(msg==0){
	   				$("#article_content").html('Wystąpił błąd');
	   			}
	   			else{
	   				$("#article_content").fadeOut("slow", function () {
	   					$("#article_content").html(msg);
	   				});
	   				$("#article_content").fadeIn("slow");
				}
			}
		});
	}
	//////////////////////////////////////////////// FUNCTIONS END ////////////////////////////////////////////////////
	
	$("#add_comment_form2").hide();
	$("#comm_list_bottom_span").click(function () {
	      $("#add_comment_form2").toggle("slow");
	});
	
	$("#next_site").click(function () {
		var form = $("#pagination_select_page")[0];
		var count_pages = form.options.length;
		var max_index = parseFloat(count_pages) - parseFloat(1);
		
		var next_site = $("#next_site").attr('class');
		next_site = next_site.split('_');
		next_site = next_site[2];
		
		var prev_site = $("#prev_site").attr('class');
		prev_site = prev_site.split('_');
		if( prev_site[2] == '' ){ prev_site = -1; } else{ prev_site = prev_site[2]; }
		
		if( next_site >= 0 ){
			if( parseFloat(next_site) + parseFloat(1) <= count_pages ){ GetCurrentPage( parseFloat(next_site) + parseFloat(1) ); }
			/*
			for(var i=0; i<count_pages; i++) {
				form.options[i].removeAttribute("selected");
			}
			form.options[next_site].setAttribute('selected','selected');
			*/
			if( parseFloat(next_site) + parseFloat(2) == count_pages ){ $("#next_site").attr('class','next_site_'); $("#next_site").css("display","none"); }
			else{ $("#next_site").attr('class','next_site_'+(parseFloat(next_site) + parseFloat(1))); }
			if( next_site >= 0 ){ $("#prev_site").css("display","inline"); }
			$("#prev_site").attr('class','prev_site_'+(parseFloat(prev_site) + parseFloat(1)));
			$('#current_page').html(parseFloat(next_site) + parseFloat(1));
		}
	});
	
	$("#prev_site").click(function () {
		var form = $("#pagination_select_page")[0];
		var count_pages = form.options.length;
		var max_index = parseFloat(count_pages) - parseFloat(1);
		
		var prev_site = $("#prev_site").attr('class');
		prev_site = prev_site.split('_');
		prev_site = prev_site[2];
		
		var next_site = $("#next_site").attr('class');
		next_site = next_site.split('_');
		if( next_site[2] == '' ){ next_site = count_pages; } else{ next_site = next_site[2]; }
		
		if( prev_site > -1 ){
			if( parseFloat(prev_site) >= 0 ){ GetCurrentPage( parseFloat(prev_site) + parseFloat(1) ); }
			/*
			for(var i=0; i<count_pages; i++) {
				form.options[i].removeAttribute("selected");
			}
			form.options[prev_site].setAttribute('selected','selected');
			*/
			//alert(parseFloat(prev_site) + parseFloat(1));
			if( parseFloat(prev_site) + parseFloat(1) == 1 ){ $("#prev_site").attr('class','prev_site_'); $("#prev_site").css("display","none"); }
			else{ $("#prev_site").attr('class','prev_site_'+(parseFloat(prev_site) - parseFloat(1))); }
			if( prev_site >= 0 ){ $("#next_site").css("display","inline"); }
			$("#next_site").attr('class','next_site_'+(parseFloat(next_site) - parseFloat(1)));
			$('#current_page').html(parseFloat(prev_site) + parseFloat(1));
		}
	});
	
	$("#pagination_select_page").change(function () {
		if( $(this).val() >= 0 ){
			GetCurrentPage($(this).val());
			var form = $("#pagination_select_page")[0];
			var count_pages = form.options.length;
			
			var selected_item = $(this).val();
			var selected_index = parseFloat($(this).val()) - parseFloat(1);
			if( selected_item < count_pages && selected_item > 1 ){
				$("#next_site").css("display","inline");
				$("#prev_site").css("display","inline");
			}
			if( selected_item == 1 ){
				$("#next_site").css("display","inline");
				$("#prev_site").css("display","none");
			}
			if( selected_item == parseFloat(count_pages) - parseFloat(1) ){
				$("#next_site").css("display","none");
				$("#prev_site").css("display","inline");
			}
			
			if( selected_item == 1 ){
				$("#next_site").attr('class','next_site_'+selected_item);
				$("#prev_site").attr('class','prev_site_');
			}
			if( selected_item == count_pages ){
				$("#next_site").attr('class','next_site_');
				$("#prev_site").attr('class','prev_site_'+(parseFloat(selected_index) - parseFloat(1)));
			}
			if( selected_item < count_pages && selected_item > 1 ){
				$("#next_site").attr('class','next_site_'+(parseFloat(selected_index) + parseFloat(1)));
				$("#prev_site").attr('class','prev_site_'+(parseFloat(selected_index) - parseFloat(1)));
			}
			
			for(var i=0; i<count_pages; i++) {
				form.options[i].removeAttribute("selected");
			}
			form.options[0].setAttribute('selected','selected');
			$('#current_page').html($(this).val());
			//var set_index = parseFloat($(this).val()) - parseFloat(1);
		}
	});
	
	/*$('#article_pagination').cycle({ 
    	fx:    'scrollHorz',
    	timeout: 0,
	    speed: 'fast',
	    next:   '#pagination_next', 
	    prev:   '#pagination_prev'
	});*/
	
	if($("#select_country").val()<1) {
		$("#select_city_row").hide();
	}
	
	/*if( $("#select_country").val() > 0 && $("#form_city").val() == 0 ){
		$("#city_select_select").hide();
		$("#no_city").show();
	}*/
	
	$("#select_country").change(function () {
		if($(this).val()>0) {
			GetCity($(this).val());			
			//MakeDisabled();
		}
		else{
			$("#select_city_row").hide();
			$("#form_city").html('');
			//MakeDisabled();
		}
	});
	
	
	$('#select_cat').click(function () {
	      $("#cat_list").slideToggle("fast");
	});
	
	$("#add_comment_form").hide();
	$("#show_add_comment_form_text").click(function () {
      $("#add_comment_form").toggle("slow");
    });
	
	$('#article_gallery_bottom_bar').cycle({
    	fx:    'scrollHorz',
	    timeout: 0,
	    next:   '#gallery_next',
	    prev:   '#gallery_prev'
	});

	var $container = $('#medium_img').cycle({
	    fx:     'fade',
	    speed:  2000,
    	timeout: 0
	});

	$('#article_gallery_bottom_bar div').children("img").each(function(i) {
    $(this).click(function() {
        $container.cycle(i);
        return false;
        });
	});
	
});

function mapaStart()
	{
		if(GBrowserIsCompatible())
		{
			var mapa = new GMap2(document.getElementById("article_top_2"),{mapTypes: [G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP,G_SATELLITE_3D_MAP]});
			mapa.setCenter(new GLatLng(p_lat,p_lon),p_zoom);
			
			// kontrolki mapy
			mapa.addControl(new GLargeMapControl());
			var typyMapy = mapa.getMapTypes();    
			typyMapy[0].getName= function() { return "Mapa";}
			typyMapy[1].getName = function() { return "Satelita";}
			typyMapy[2].getName = function() { return "Hybryda";}
			mapa.addControl(new GMapTypeControl());
			mapa.addControl(new GScaleControl());
			mapa.enableDoubleClickZoom();
			mapa.enableContinuousZoom();						
		}
	}