
var rel_art_timer;

var num_galleries;
if(num_galleries == undefined){num_galleries=5;}

var num_right_galleries;
if(num_right_galleries == undefined){num_right_galleries=5;}

//var num_popular_galleries=10;

var num_offer;
if(num_offer == undefined){num_offer=5;}

var num_arts_by_rate;
if(num_arts_by_rate == undefined){num_arts_by_rate=4;}


var rel_old_art;
var rel_art_interval;
//var rel_art_timer = 8000;
//var rel_art_count = 3;
var rel_old_art = 0;
var rel_current_art = 0;


$(document).ready(function(){

	// art powiazane podobne ////////////////////////////
	if(rel_art_timer != undefined){
		nav_related_arts();
	}


	// pokaz box pasaz //////////////////////////////////
	show_shops();
	

	// top menu pozostale ///////////////////////////////
	$('.others').mouseover(function() {
		$('.others a').addClass("roll");
		$('#others').show();
	}); 
	$('.others').mouseout(function() {
		$('.others a').removeClass("roll");
		$('#others').hide();
	});
	

    // gwiazdki ocenianie w arcie //////////////////////////////////////////
    rate_box_stars();

	// galerie ///////////////////////////////////////////
	nav_galleries(num_galleries);
	
	// right galerie ///////////////////////////////////////////
	nav_right_galleries(num_right_galleries);
	
	// box artykyly wg oceny ///////////////////////////////////////////
	nav_arts_by_rate(num_arts_by_rate);
	
	// tooltipy //////////////////////////////////////////
	//tooltip(); 
	
	// komis oferty //////////////////////////////////////
	nav_offer(num_offer);

	repair_ads();
	repair_home_bill();

	$(this).everyTime(500, 'ads', function() {
		repair_ads();
		repair_home_bill();
	});

});


function repair_ads() {
	boxes = $(".ads_placement div div");
	for(i=0; i < boxes.length; i++) {
		is_ads = false;
		for(j=0; j < boxes[i].childNodes.length; j++) {
			if (boxes[i].childNodes[j].tagName && boxes[i].childNodes[j].tagName != 'SCRIPT' && boxes[i].childNodes[j].tagName != '!') {
				is_ads = true;
				//alert('jest');
				break;
			}
		}
		if (is_ads) {
			boxes[i].style.padding = "10px";
			boxes[i].style.border = "1px solid #eee";
			boxes[i].style.marginBottom = "10px";
			boxes[i].style.textAlign = "center";
			$(".ads_placement div div");
		}
	}
}

function repair_home_bill() {

	boxes = $(".home_ads_placement div div div div");
	for(i=0; i < boxes.length; i++) {
		is_ads = false;
		for(j=0; j < boxes[i].childNodes.length; j++) {
			if (boxes[i].childNodes[j].tagName && boxes[i].childNodes[j].tagName != 'SCRIPT' && boxes[i].childNodes[j].tagName != '!') {
				is_ads = true;
				//alert('jest');
				break;
			}
		}
		if (is_ads) {
			$("#ad_bill_bonus").show();
			$(".home_ads_placement .ad_info").show();
			$(".home_ads_placement .ad_home_bill").css({paddingBottom: '15px' });
			$(".home_ads_placement .ad_home_div").css({borderRight: '1px solid #eee' });
		}
	}
}

function show_shops() {
	$('a.show_shops').click(function (e) {
		e.preventDefault();
		$('#shops_box').modal({onClose: function () {
			$('#ad_bill').css({visibility: 'visible' });
			$.modal.close();
		}});
		$('#ad_bill').css({visibility: 'hidden' });
		showBox('Shops', '#shops_box', 0);
	});
}


function image_nav() {
	$(".img").hover(
		function () {
			$(".img .prev, .img .next").stop();
			$(".img .prev, .img .next").animate({ opacity: 0.7}, 200 );
			$(".img .prev, .img .next").hover(
				function () {
					$(this).stop();
					$(this).animate({ opacity: 1}, 200 );
				}, 
				function () {
					//$(this).fadeTo("fast", 0.70);
					$(this).stop();
					$(this).animate({ opacity: 0.7}, 200 );
				}
			);
		}, 
		function () {
			$(".img .prev, .img .next").stop();
			//$(".img .prev, .img .next").fadeTo("fast", 0.30);
			$(".img .prev, .img .next").animate({ opacity: 0.2}, 200 );

		}
	);
}


function nav_galleries(num_galleries) {
	$(".galleries .gallery").each(function (i) {
	
		//var num_galleries=5;
		
		$(".btn_next").eq(i).click(function () {
			asc=i+1;
			if(asc>num_galleries-1){
				asc=num_galleries-1;
			}else{
				$(".galleries .gallery:eq("+i+")").hide();
				$(".galleries .gallery:eq("+asc+")").show();
			}
			if(asc>num_galleries-2){
				$(".btn_next:eq("+asc+")").addClass("btn_next_disabled");
			}
		});
		
		$(".btn_prev").eq(i).click(function () {
			desc=i-1;
			if(desc<0){
				desc=0;
			}else{
				$(".galleries .gallery:eq("+i+")").hide();	
				$(".galleries .gallery:eq("+desc+")").show();
			}
			if(desc<1){
				$(".btn_prev:eq("+desc+")").addClass("btn_prev_disabled");
			}
		});
		
	});
}


function nav_right_galleries(num_right_galleries) {
	$(".galleries .gallery").each(function (i) {
	
		//var num_galleries=5;
		
		$(".btn_next").eq(i).click(function () {
			asc=i+1;
			if(asc>num_right_galleries-1){
				asc=num_right_galleries-1;
			}else{
				$(".galleries .gallery:eq("+i+")").hide();
				$(".galleries .gallery:eq("+asc+")").show();
			}
			if(asc>num_galleries-2){
				$(".btn_next:eq("+asc+")").addClass("btn_next_disabled");
			}
		});
		
		$(".btn_prev").eq(i).click(function () {
			desc=i-1;
			if(desc<0){
				desc=0;
			}else{
				$(".galleries .gallery:eq("+i+")").hide();	
				$(".galleries .gallery:eq("+desc+")").show();
			}
			if(desc<1){
				$(".btn_prev:eq("+desc+")").addClass("btn_prev_disabled");
			}
		});
		
	});
}

function nav_related_galleries() {

	var rg_i = 0;
	var rg_n = 1;
	var rg_m = 6;
	var rg_d = 6000;

	$("#photo").oneTime(rg_d, 'rg_t', function() {
		set_related_galleries(1);
	});

	function set_related_galleries(direction) {
		if(direction==1){
			$(".related_gallery:eq(" + rg_i + ")").hide();
			rg_i++;
			$(".related_gallery:eq(" + rg_i + ")").hide();
			rg_i++;
			
			if(rg_n == rg_m){
				rg_i = 0;
			}
			$(".related_gallery:eq(" + rg_i + ")").show();
			rg_i++;
			$(".related_gallery:eq(" + rg_i + ")").show();
			rg_i--;
			
			if(rg_n == rg_m){
				rg_n = 0;
			}
			rg_n++;
		}else{
			$(".related_gallery:eq(" + rg_i + ")").hide();
			rg_i++;
			$(".related_gallery:eq(" + rg_i + ")").hide();
			rg_i--;
	
			if(rg_n == 1){
				rg_i = rg_m*2;
			}
	
			rg_i--;
			$(".related_gallery:eq(" + rg_i + ")").show();
			rg_i--;
			$(".related_gallery:eq(" + rg_i + ")").show();
			//rg_i--;
			
			if(rg_n == 1){
				rg_n = rg_m+1;
			}
			rg_n--;
		}
	
		$(".nav_next_prev_tabs_gray .num").text(rg_n+"/"+rg_m);
		//$(this).animate({ opacity: 1}, 200 );
		$("#photo").stopTime('rg_t');
		$("#photo").oneTime(rg_d, 'rg_t', function() {
			set_related_galleries(1);
		});
	}
	
	$(".nav_next_prev_tabs_gray a.btn_next").click(function () {
		set_related_galleries(1);
	});
	
	$(".nav_next_prev_tabs_gray a.btn_prev").click(function () {
		set_related_galleries(0);
	});
			
	$(".related_gallery").hover(
		function () {
			$("#photo").stopTime('rg_t');
		}, 
		function () {
			$("#photo").oneTime(rg_d, 'rg_t', function() {
				set_related_galleries(1);
			});
		}
	);

}


// box z artykulami (home) auto, mouseover switch
function nav_arts(arts_box, set_art, max_art, art_time) {

	$(arts_box).oneTime(art_time, 'a_t', function() {
		switch_arts(arts_box, set_art, max_art, art_time);
	});

	$(arts_box + " .nav_link a").each(function (i) {
		$(this).hover(
			function () {
				//alert(i);
				switch_arts(arts_box, i, max_art, art_time);
				$(arts_box).stopTime('a_t');
			}, 
			function () {
				$(arts_box).oneTime(art_time, 'a_t', function() {
					switch_arts(arts_box, i, max_art, art_time);
				});
			}
		);
	});

	$(arts_box).hover(
		function () {
			$(arts_box).stopTime('a_t');
		}, 
		function () {
			$(arts_box).oneTime(art_time, 'a_t', function() {
				switch_arts(arts_box, i, max_art, art_time);
			});
		}
	);

}

function switch_arts(arts_box, set_art, max_art, art_time) {

	if(set_art == max_art-1){
		old_art = 0;
	}else{
		old_art = set_art-1;	
	}

	$(arts_box + " .news").hide();
	$(arts_box + " .nav_link a").removeClass("sel");
	
	$(arts_box + " .news:eq(" + set_art + ")").show();
	$(arts_box + " .nav_link a:eq(" + set_art + ")").addClass("sel");

	if(set_art == max_art-1){
		next_art = 0;
	}else{
		next_art = set_art+1;
	}

	$(arts_box).oneTime(art_time, 'a_t', function() {
		switch_arts(arts_box, next_art, max_art, art_time);
	});

}


function nav_offer(num_offer) {
	var offer_i = 1;
	$("#offer_box .random_offer .nav a").click(function () {
	
		$("#offer_box .random_offer li:eq("+offer_i+")").hide();
		
		if ( $(this).hasClass("prev") ){
			if(offer_i==1){offer_i=num_offer;}else{offer_i=offer_i-1;}
		}
		if ( $(this).hasClass("next") ){
			if(offer_i==num_offer){offer_i=1;}else{offer_i=offer_i+1;}
		}
		$("#offer_box .random_offer .nav .count").text(offer_i+"/"+num_offer);
		$("#offer_box .random_offer li:eq("+offer_i+")").show();
	});
}


function showBox(sBoxType, sBoxPlacement, nLimit) {
	
	$(sBoxPlacement).animate({opacity: 0.8}, 100 );
	$(sBoxPlacement + " .loader").show();
	
	$.ajax({
		type: 'POST',
		url: '/ajaxPages/boxes/' + sBoxType + '.php5',
		data: 'nLimit=' +  nLimit,
		complete: function() {
			$(sBoxPlacement).load("ajaxPages/boxes/" + sBoxType + ".php5?nLimit=" +  nLimit);
			$(sBoxPlacement).animate({opacity: 1.0}, 1000 );
			$(sBoxPlacement + " .loader").hide();
	   }
	});
}


function nav_arts_by_rate(num_arts_by_rate) {
	var arts_i = 0;
	$("#right_articles_by_rate .title_tabs .nav a").click(function () {
	
		$("#right_articles_by_rate .3arts:eq("+arts_i+")").hide();
		
		if ( $(this).hasClass("btn_prev") ){
			if(arts_i==0){arts_i=num_arts_by_rate-1;}else{arts_i=arts_i-1;}
		}
		if ( $(this).hasClass("btn_next") ){
			if(arts_i==num_arts_by_rate-1){arts_i=0;}else{arts_i=arts_i+1;}
		}
		$("#right_articles_by_rate .title_tabs .nav .num").text(arts_i+1+"/"+num_arts_by_rate);
		$("#right_articles_by_rate .3arts:eq("+arts_i+")").show();
	});
}


this.tooltip_violet = function(){			
	xOffset = 16;
	yOffset = 16;				
	$("a.tooltip").hover(function(e){										  
		this.t = this.title;
		this.t = this.t.replace('[b]', "<b>");
		this.t = this.t.replace('[/b]', "</b>");
		this.t = this.t.replace('[br /]', "<br />");
		this.title = "";									  
		$("body").append("<div id='tooltip_violet'><div class='a'></div><div class='c'>"+ this.t +"</div><div class='c_b'></div></div>");
		$("#tooltip_violet")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip_violet").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip_violet")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};



function articleVote(vote, nId) {
  $.ajax({
	    type: 'POST',
	    url: '/ajaxPages/articleVote/vote.php5',
	    data: 'vote=' +  vote + '&id=' + nId,
	    complete: function(msg) {
        $('#rate_box').load("ajaxPages/articleVote/voteMsg.php5?id=" + nId);
       }
  });
}



function rate_box_stars() {
    // oceny art gal /////////////////
	$("#rate_box .stars a").each(function (i) {
		$(this).hover(function () {	
			$(this).addClass("hsel");
			$(this).removeClass("hdsel");
			$("#rate_box .stars a:lt("+i+")").addClass("hsel");
			$("#rate_box .stars a:lt("+i+")").removeClass("hdsel");
			$("#rate_box .stars a:gt("+i+")").addClass("hdsel");
			$("#rate_box .stars a:gt("+i+")").removeClass("hsel");	
		},function() {});
	});
	
	$("#rate_box .stars").mouseout(function() {
		$("#rate_box .stars a").removeClass("hsel");
		$("#rate_box .stars a").removeClass("hdsel");	
	});
}



function txt_zoom(size) {
	txt_size=size;
	if(txt_size == 1){
		$("#article .content").removeClass("size_n");
		$("#article .content").addClass("size_b");
	}else{
		$("#article .content").removeClass("size_b");
		$("#article .content").addClass("size_n");
	}
}



function nav_related_arts() {
	rel_art_interval = setInterval(rel_switch_art, rel_art_timer);
	$("#right_related_articles .nav a").each(function (i) {
		//$(this).attr({ href: "javascript:void(0);", rel: "nofollow"});
		$(this).click(function () {
			rel_set_current_art(i);
		});
		
	});
	/*$('#related_articles').hover(function() {
	 clearInterval(rel_art_interval);
	}, function() {
	 rel_art_interval = setInterval(rel_switch_art, rel_art_timer); 
	});*/
}
function rel_switch_art() {
	clearInterval(rel_art_interval);
	rel_current_art = (rel_old_art + 1) % rel_art_count; 
	$("div.related_box:eq(" + rel_old_art + ")").hide();
	$("div.related_box:eq(" + rel_current_art + ")").show();
	$("#right_related_articles .nav a:eq(" + rel_old_art + ")").removeClass("sel");
	$("#right_related_articles .nav a:eq(" + rel_current_art + ")").addClass("sel");
	rel_old_art = rel_current_art;
	rel_art_interval = setInterval(rel_switch_art, rel_art_timer); 
	clearInterval(rel_art_interval);
}
function rel_set_current_art(rel_set_art) {
	$("div.related_box:eq(" + rel_old_art + ")").hide();
	$("div.related_box:eq(" + rel_set_art + ")").show();
	$("#right_related_articles .nav a:eq(" + rel_old_art + ")").removeClass("sel");
	$("#right_related_articles .nav a:eq(" + rel_set_art + ")").addClass("sel");
	rel_old_art = rel_set_art;
	rel_art_interval = setInterval(rel_switch_art, rel_art_timer); 
	clearInterval(rel_art_interval);
}








