$(function() {
	//init
	$("#top-news-container").css("width", $(window).width());
	$("#top-news-panel").css("width", ($(".top-news-content").length * 600));
	$(".top-news-content").css("margin-left", $(window).width());
	
	
	$("#top-news-content-1").delay(600).animate({"margin-left": Math.round( ($(window).width() / 2) - 435 )}, 800, "easeInOutCirc");
	$("#top-news-text-1").delay(600).animate({left: 0}, 800, "easeInOutCirc");
	
	for (i = 2; i <= $(".top-news-content").length; i++) {
		if (i == $(".top-news-content").length) { $("#top-news-content-" + i).delay(3 * 300).animate({"margin-left": Math.round( ($(window).width() / 2) - 435 - 390 )}, 1100, "easeInOutCirc"); }
		else { $("#top-news-content-" + i).delay(i * 300).animate({"margin-left": Math.round( ($(window).width() / 2) - 435 + ((i-1) * 600) )}, 1100, "easeInOutCirc"); }
	}
	
	
	
	// fade in controls
	if ($(window).width() > 960) { $("#top-news-controls-right").css("margin-right", "-100px"); }
	if ($(window).width() > 1280) { $("#top-news-controls-right").css("margin-right", "-580px"); }
	$("#top-news-controls-left, #top-news-controls-right").delay(1200).fadeIn(800);
	
	
	// set opacity for the following images
	for (i=2; i <= $(".top-news-content").length; i++) {
		$("#top-news-content-" + i + " div").animate({opacity: .4}, 0);
	}
	
	
	// counter display
	/*
	if (($(".top-news-content").length - 1) < 10) { var allPages = '0' + ($(".top-news-content").length - 2); }
	else { var allPages = $(".top-news-content").length - 1; }
	*/
	$("#top-news-all-pages").html("99");	//mert 99-ben határoztuk meg
});



$(window).resize(function() {
	$("#top-news-container").css("width", $(window).width());
	//get all children's div id
		var children_ids = [];
		$("#top-news-panel").contents().filter(
		  function(){
			if (this.nodeType == 1) { // nodeType 1 => div 
				try {
				  children_ids.push(this.id.substring(17));
				} catch(e){}
			}
		  }
		);
	


	for (i = 0; i < children_ids.length; i++) {
		
		$("#top-news-content-" + children_ids[i]).css("margin-left", (Math.round( ($(window).width() / 2) - 435) + ((children_ids[i]-1) * 600) )); 
	}

	
	if ($(window).width() > 960) { $("#top-news-controls-right").css("margin-right", "-100px"); }
	if ($(window).width() > 1280) { $("#top-news-controls-right").css("margin-right", "-580px"); }
});



$(document).ready(function() {
	// legelso beanimalasa (IE7)
	$("#ie-top-news-content-1").show(0);
	$(".ie-top-news-controller, #ie-top-news-date-1, #ie-top-news-image-1, #ie-top-news-text-1, #ie-top-news-small-image-1").delay(400).fadeIn(1000);
});







function topNewsSlide(topNewsCurrent, id) {	//aktuális,következő
	var topNewsContents = $(".top-news-content").length;
	topNewsCurrent = parseInt(topNewsCurrent);
	id = parseInt(id);
	var doAjax = 0;
	
	

	// A melyikre kattint az id és a topNewsCurrent -ból eldől
	if (id < 100 && id > 0) { //mert kétjegyű lehet a szám, de ez mindegy
		var firstMarginLeft = parseInt( $("#top-news-content-1").css("margin-left") );
		var newMarginLeft = (id >topNewsCurrent)? -((topNewsCurrent) * 600):-((topNewsCurrent-2) * 600);

		$.ajax({
			type: "POST",
			url: "/3.0/_includes/nyito/top_news_container_item.php",
			data: "id="+id+"&current="+topNewsCurrent+"&ajax=true",
			success: function(html_data) {
				// az új text box 
				var class_tag=(id >topNewsCurrent)? '':'style="left: -325px;"';
				$("#top-news-text-container").append('<div id="top-news-text-' + id + '" class="top-news-text" '+class_tag+'></div>').each(function() {
					
					$("#top-news-text-" + id).html(html_data);
					$("#top-news-text-" + id).animate({left: 0}, 600, "easeInOutCirc");

					// ha volt csúszkája	
					$(".scroll-pane").jScrollPane();
					$(".jspHorizontalBar, .jspVerticalBar").animate({opacity: 1}, 0, "easeInOutCirc");	
					

					// ha kiment a képből
					var left_tag=(id >topNewsCurrent)? -325:325;
					$("#top-news-text-" + topNewsCurrent).animate({left: left_tag}, 400, "easeInOutCirc", function() {
						//remove
						$(this).remove();
						
						//módosít a gombok
						var newTopNewsPrev = "topNewsSlide('" + id + "', '" + (id - 1) + "');"
						$("#top-news-prev").attr("onclick", newTopNewsPrev);
						
						var newTopNewsNext = "topNewsSlide('" + id + "', '" + (id + 1) + "');"
						$("#top-news-next").attr("onclick", newTopNewsNext);

						// módosít a számláló
						if (id < 10) { var currentPage = '0' + id; }
						else { var currentPage = id; }
						$("#top-news-current-page").html(currentPage);
					});
					
					
					// ezek a képek	
					//mozgás
					$(".top-news-content").animate({"left": newMarginLeft}, 600, "easeInOutCirc");
					//opacity a benne levő div-re
					$("#top-news-content-" + topNewsCurrent + " div").animate({opacity: .4}, 1, "easeInOutCirc");
					$("#top-news-content-" + id + " div").animate({opacity: 1}, 0.4, "easeInOutCirc");
					
					// kép divek create, remove
					if( id > 4 && id > topNewsCurrent){ //növekedés irány
						var utolso = id + 2;
						var keszitendo = id + 3;
						var vege_div = id + 4;
						var kepfile = document.getElementById("kep_load_container").value;
						// a végét törölni, a lezáró van ezen a néven
						$("#top-news-content-" + keszitendo).remove();
						var torlendo = id - 4;
						// a készítés
						// hová kerül? az előtte lévő +600
						marginleft=parseInt( $("#top-news-content-" + utolso).css("margin-left") ) + 600;
						marginleft2 = marginleft + 600;
						$("#top-news-panel").append('<div id="top-news-content-' + keszitendo + '" class="top-news-content" style="margin-left:' + marginleft + 'px; left: ' + newMarginLeft + 'px;"><div class="" style="opacity: 0.4;"><img src="' + kepfile + '" alt="" style="visibility: visible; opacity: 1;"></div></div>');
						//és újra a végét
						$("#top-news-panel").append('<div id="top-news-content-' + vege_div + '" class="top-news-content top-news-content-end"  style="margin-left:' + marginleft2 + 'px; left: ' + newMarginLeft + 'px;"><div class="top-news-content-end-indicator"></div></div>');
						// a törlés
						$("#top-news-content-" + torlendo).remove();

					} else  if (id > 3 && id < topNewsCurrent){
						var keszitendo = id - 3;
						var elotte = id - 2;
						var torlendo = id + 4;
						var vege_div = id + 5;	
						var kepfile = document.getElementById("kep_load_container").value;
						// a készítés
						// hová kerül?
						marginleft=parseInt( $("#top-news-content-" + elotte).css("margin-left") ) - 600;
						marginleft2 = marginleft + 600;
						$("#top-news-panel").prepend('<div id="top-news-content-' + keszitendo + '" class="top-news-content" style="margin-left:' + marginleft + 'px; left: ' + newMarginLeft + 'px;"><div class="" style="opacity: 0.4;"><img src="' + kepfile + '" alt="" style="visibility: visible; opacity: 1;"></div></div>');
						// a törlés
						$("#top-news-content-" + torlendo).remove();
						$("#top-news-content-" + vege_div).remove();
						//és újra a végét
						$("#top-news-panel").append('<div id="top-news-content-' + torlendo + '" class="top-news-content top-news-content-end"  style="margin-left:' + marginleft2 + 'px; left: ' + newMarginLeft + 'px;"><div class="top-news-content-end-indicator"></div></div>');
					}
						
				});
			}
		});
	}
}







function topNewsFade(topNewsCurrent, id) {
	clearTimeout(t);
	
	
	
	if (id != topNewsCurrent) {
		var fadeSpeed = 1000;
		
		
		var containerCurrent = "#ie-top-news-content-"  + topNewsCurrent;
		var dateCurrent = "#ie-top-news-date-" + topNewsCurrent;
		var imageCurrent = "#ie-top-news-image-" + topNewsCurrent;
		var textCurrent = "#ie-top-news-text-" + topNewsCurrent;
		var smallImageCurrent = "#ie-top-news-small-image-" + topNewsCurrent;
		
		
		
		var container = "#ie-top-news-content-" + id;
		var date = "#ie-top-news-date-" + id;
		var image = "#ie-top-news-image-" + id;
		var text = "#ie-top-news-text-" + id;
		var smallImage = "#ie-top-news-small-image-" + id;
		
		
		
		$(dateCurrent).delay(0).fadeOut(fadeSpeed);
		$(imageCurrent).delay(0).fadeOut(fadeSpeed);
		$(textCurrent).delay(0).fadeOutfadeSpeed;
		$(smallImageCurrent).delay(0).fadeOut(fadeSpeed);
		
		
		$(container).delay(0).show(0, function() {
			$(containerCurrent).delay(0).hide(0);
		});
		
		
		$(date).delay(0).fadeIn((fadeSpeed));
		$(image).delay(0).fadeIn(fadeSpeed);
		$(text).delay(0).fadeIn((fadeSpeed));
		$(smallImage).delay(0).fadeIn(fadeSpeed);
		
	} // id != topNewsCurrent
}



