var rotator = 1;
var tmp;
var rid = 0;

var active;
var pag_tmp;
var pages;

var rp = 1;
var rp_tmp = 1;

$(function (){
	//facebook widget
	$(".facebook").toggle(function(){
		$(".facebook").stop(true, false).animate({right:"0"},"medium");
	},function(){
		$(".facebook").stop(true, false).animate({right:"-272"},"medium");
	},500);
}); 

function update() {
	$(".pag_active").attr("class","pag_not_active");
	$(".pag_not_active#id"+active).attr("class","pag_active");
}

function toggle(show,hide,right) {
	if(right) {
		if(show<pages) { $(".pag_not_active#id"+show).show("normal"); }
		if(hide == 2) { $(".dots_left").show("normal"); }
		if(hide>1) { $(".pag_not_active#id"+hide).hide("normal"); }
		if(show == pages-1) { $(".dots_right").hide("normal"); }
	}
	else {
		if(hide<pages) $(".pag_not_active#id"+hide).hide("normal");
		if(show == 2) $(".dots_left").hide("normal");
		if(show>1) $(".pag_not_active#id"+show).show("normal");
		if(hide == pages-1) $(".dots_right").show("normal");
	}
}

function setImage(path,id,x,y) {
	$("."+id).html("<img src=\""+path+"\" width="+x+" height="+y+" />");
	$("."+id).show("normal");
}

$(document).ready( function() {
	pages = $("input[name='how_many_pages']").attr("value");
	active = 2;
	theRotator();
	
	var last = 0;
	$("#mainmenu a").hover( function() {
		if(last != 0) last.removeClass("active");
		last = $(this).parent();
		last.addClass("active");
		var list = $(this).next().html();
		$("#submenu ul").html(list);
	});
	
	$(".search_input").focusin( function() {
		if($(this).val() == "wyszukaj frazę...") $(this).val("");
	});
	
	$(".search_input").focusout( function() {
		if($(this).val() == "") $(this).val("wyszukaj frazę...");
	});
	
	$(".loginpanel_user").focusin( function() {
		if($(this).val() == "login") $(this).val("");
	});
	
	$(".loginpanel_user").focusout( function() {
		if($(this).val() == "") $(this).val("login");
	});
	
	$(".loginpanel_pass").focusin( function() {
		if($(this).val() == "password") $(this).val("");
	});
	
	$(".loginpanel_pass").focusout( function() {
		if($(this).val() == "") $(this).val("password");
	});
	
	$(".newsletter_input").focusin( function() {
		if($(this).val() == "twój email...") $(this).val("");
	});
	
	$(".newsletter_input").focusout( function() {
		if($(this).val() == "") $(this).val("twój email...");
	});
	
	$(".register_button").click( function() {
		window.location.href = "register.html";
	});
	
	$(".logout_button").click( function() {
		window.location.href = "logout.html";
	});
	
	$(".myprofile_button").click( function() {
		window.location.href = "my_profile.html";
	});
	
	$(".archive_button").click( function() {
		window.location.href = "news.html";
	});
	
	$("#rotator_numbers a").click( function() {
		clearInterval(rid);
		tmp = rotator;
		rotator = $(this).attr("id");
		if(rotator != tmp) {
			$(".rot_active").attr('class','rot_not_active'+tmp);
			$(".rot_not_active"+rotator).attr('class','rot_active');
			$("#rotator_news").html($(".rotator"+rotator).html());
		}
		rid = setInterval('rotate()',6000);
		return false;
	});
	
	$(".prevPage").click( function() {
		//alert("prev");
		if(active>1) {
			pag_tmp = active;
			--active;
			toggle(active-1,active+2,0);
			update();
			$("#three_news_cont").animate({"top": "+=396px"}, "normal");
		}
	});
	
	$(".nextPage").click( function() {
		if(active<pages) {
			pag_tmp = active;
			++active;
			toggle(active+1,active-2,1);
			update();
			$("#three_news_cont").animate({"top": "-=396px"}, "normal");
		}
	});
	
	$(".pag_not_active, .pag_active").click( function() {
		pag_tmp = active;
		active = parseInt($(this).attr("id").substring(2));
		if(pag_tmp!=active) {
			if(active==1) {
				update();
				toggle(2,pag_tmp+1,0);
				if(pag_tmp>2) toggle(2,pag_tmp,0);
				if(pag_tmp>3) toggle(2,pag_tmp-1,0);
			}
			else if(active==pages) {
				update();
				toggle(pages-1,pag_tmp-1,1);
				if(pag_tmp<pages-1) toggle(pages-1,pag_tmp,1);
				if(pag_tmp<pages-2) toggle(pages-1,pag_tmp+1,1);
			}
			else if(active>pag_tmp) {
				toggle(active+1,active-2,1);
				update();
			}
			else if(active<pag_tmp) {
				toggle(active-1,active+2,0);
				update();
			}
			var hmpx = 396 * (pag_tmp-active);
			$("#three_news_cont").animate({"top": "+="+hmpx+"px"}, "normal");
		}
	});
	
	$("#News_image").change( function() {
		if($(this).val() != "")
			setImage("images/news/"+$(this).val(),"news_image_preview",204,83);
		else
			$(".news_image_preview").hide("normal");
	});
	
	$("#News_mini_image").change( function() {
		if($(this).val() != "")
			setImage("images/news/mini/"+$(this).val(),"news_mini_image_preview",99,99);
		else
			$(".news_mini_image_preview").hide("normal"); 
	});
	
	$("#Articles_image").change( function() {
		if($(this).val() != "")
			setImage("images/articles/"+$(this).val(),"news_image_preview",204,83);
		else
			$(".news_image_preview").hide("normal");
	});
	
	$("#Articles_mini_image").change( function() {
		if($(this).val() != "")
			setImage("images/articles/mini/"+$(this).val(),"news_mini_image_preview",58,25);
		else
			$(".news_mini_image_preview").hide("normal"); 
	});
	
	$("#right_panel_tabs .tab"+rp).addClass("rp_active");
	$("#right_panel_content").html($("#rp"+rp).html());
	
	$("#right_panel_tabs a").click( function() {
		rp_tmp = rp;
		rp = parseInt($(this).parent().attr("class").substring(3));
		if(rp!=rp_tmp) {
			$("#right_panel_tabs .tab"+rp_tmp).removeClass("rp_active");
			$("#right_panel_tabs .tab"+rp).addClass("rp_active");
			$("#right_panel_content").html($("#rp"+rp).html());
		}
		return false;
	});
});

function theRotator() {
	//Set the opacity of all images to 0
	//$('div.rotator1 img').css({opacity: 0.0});
	//$('div.rotator2 img').css({opacity: 0.0});
	//$('div.rotator3 img').css({opacity: 0.0});
	
	$("#rotator_news").html($(".rotator"+rotator).html());
	$(".rot_not_active"+rotator).attr('class','rot_active');
	//Get the first image and display it (gets set to full opacity)
	//$('div#rotator_news img').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	
	rid = setInterval('rotate()',6000);
}

function rotate() {
	tmp = parseInt(rotator);
	rotator = (tmp == 3 ? 1 : tmp+1);
	$(".rot_active").attr('class','rot_not_active'+tmp);
	$(".rot_not_active"+rotator).attr('class','rot_active');
	$("#rotator_news").html($(".rotator"+rotator).html());
	//$("#rotator_news img").animate({opacity: 1.0}, 1000);
	//Get the first image
	//var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    //if ( current.length == 0 ) current = $('div.rotator ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	//var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
	
	//Un-comment the 3 lines below to get the images in random order
	
	//var sibs = current.siblings();
        //var rndNum = Math.floor(Math.random() * sibs.length );
        //var next = $( sibs[ rndNum ] );
			

	//Set the fade in effect for the next image, the show class has higher z-index
	//next.css({opacity: 0.0})
	//.addClass('show')
	//.animate({opacity: 1.0}, 1000);

	//Hide the current image
	//current.animate({opacity: 0.0}, 1000)
	//.removeClass('show');
	
};
