$(document).ready(function() {
	$(".mailOnly").hide();

	//concurs poze
	$("body.concurs_poze, body.concurs_poze_admin").pngFix();
	$('body.article-concurs_poze .concurs_poze_list .list-images li .image a, a.concurs_poze_viewFull_admin').lightBox({fixedNavigation:true});

});
$(function() {
	$('.tabbed').each(function() {
		var currentElement = this;
		var counter = 0;
		var hasNav = false;
		$(this).find('div.box').each(function(i, tab) {
			if(i > 0) {
				$(tab).hide();
			}
			counter = i+1;
		});
		var ul = document.createElement('ul');
		ul.className = 'slidenavigation';
		for(var i = 0; i < counter; i++) {
			var li = document.createElement('li');
			var a = document.createElement('a');
			var txt = document.createTextNode($(currentElement).find('div.box:eq('+i+')').attr("title"));
			a.href = '#tab'+i;
			if(i == 0) {
				a.className = 'selected';
			}
			a.appendChild(txt);
			a.onclick = function() {
				var idx = $(this).attr('href').replace('#tab', '');
				$(currentElement).find('ul.slidenavigation li a.selected').removeClass('selected');
				$(this).addClass('selected');
				$(currentElement).find('div.box:visible').hide();
				$(currentElement).find('div.box:eq('+idx+')').show();
				// detect if ie6 and reposition the copyright text from top because ie6 ignore an axis
				if (typeof document.body.style.maxHeight == "undefined") $('#copytext_'+(Number(idx)+1)).css('top',($('div.boxImage img').eq(idx).height()-22));
			};
			li.appendChild(a);
			ul.appendChild(li);
		}
		$(this).prepend(ul);

		if ($('ul.slidenavigation')){
			// hide empty div when you have tabbed section
			$('#main #content #primary').hide();

			// retinerea tabului apasat
			if (location.hash){
				var selectedTab = location.hash.substring(4,5);
				$('ul.slidenavigation li a.selected').removeClass('selected');
				$('ul.slidenavigation li a').eq(selectedTab).addClass('selected');
				$('div.tabbed div.box').hide();
				$('div.tabbed').find('div.pos_'+(Number(selectedTab)+1)+'').show();
			}
		}
	});

	/*$('#slideshow').cycle({ 
		fx: 'fade' ,
		speed:    300, 
		timeout:  2000 
	});*/
	
	//search
	$("#searchString").focus(function() {
		if (this.value == 'caută...') {
			this.value = '';
		}
	}).blur(function() {
		if (this.value == '') {
			this.value = 'caută...';
		}
	});
	
	// deschidere hardcodata a taburilor in "_blank"
	$('#corporate li').each(function(i,tab){
		if ((i!=0)&&(i!=2)&&(i!=3)&&(i!=6)){
			tab.firstChild.target = "_blank";
		}
	});
	
	//newsletter
	$("#nws-f-email").focus(function() {
		if (this.value == 'Adresa ta de e-mail...') {
			this.value = '';
		}
	}).blur(function() {
		if (this.value == '') {
			this.value = 'Adresa ta de e-mail...';
		}
	});

	/**
	 * Promo Slideshow
	 */
	$('div.banner-slide').each(function() {
		var slidesCount = 0;
		var lastSlide = null;
		var enableAutomaticSlide = false;
		var animationInProgress = false;
		var container =  this;
		var slideDuration = 8;//seconds
		
		// cound and mark slides
		$(this).children('div').each(function(i, currentSlide) {
			slidesCount++;
			lastSlide = 'slide'+slidesCount;
			$(this).addClass('slide'+slidesCount);
			if(i == 0) {
				$(this).addClass('current');
			}
			else if(i > 0) {
				$(this).hide();
			}
		});
		var changeSlide = function(oldSlide, newSlide) {
			//fade
			animationInProgress = true;
			$('div.articleList_slide ul li.slide').eq(oldSlide-1).removeClass('current');
			$('div.articleList_slide ul li.slide').eq(newSlide-1).addClass('current');
			$('div.articleList_slide ul li.slide div.info').eq(oldSlide-1).hide(300,function(){
				$('div.articleList_slide ul li.slide div.info').eq(newSlide-1).show(250);
			});

			
			$(container).find('div.slide'+oldSlide).removeClass('current').fadeOut(500, function() {
				$(container).find('div.slide'+newSlide).addClass('current').fadeIn(250, function() {
					animationInProgress = false;
				});
			});
		}
		
		autoSlider = {}
		autoSlider.looping = function(p){
			if(!animationInProgress) {
				var currentSlide = parseInt($('#main div.articleList_slide ul li.current').attr('rel'));
				var nextSlide = (currentSlide + 1) <= slidesCount ? currentSlide + 1 : 1;
				changeSlide(currentSlide, nextSlide);
				clearInterval(slideTimer);
				slideDuration = (currentSlide==slidesCount) ? 8 : 5 ;
				slideTimer = setInterval(autoSlider.looping, slideDuration*1000);
			}
		}

		if($(container).hasClass('automatic-slide')) {
			var slideShow = function() {
				if(enableAutomaticSlide) {
					currentSlide = parseInt($('#main div.articleList_slide ul li.current').attr('rel'));
					slideTimer = setInterval(autoSlider.looping, slideDuration*1000);
				}
			}
			slideShow();
		}
		
		var infoAnimationInProgress = false;
		var showInfo = function(li) {
			if(!infoAnimationInProgress) {
				infoAnimationInProgress = true;
				$(li).parent().children(".current").children(".info").hide(300, function() {
					$(li).children(".info").show(250, function() {
						infoAnimationInProgress = false;
					});
				});
			}
		}

		//slideshow icon navigation
		$("#main div.articleList_slide ul li").each(function(i, el) {
			if (i == 0) {
				$(this).addClass("current");
				showInfo(this);
			}
			$(this).click(function(e) {
				var currentSlide = $(this).parent().children(".current").attr("rel");
				var nextSlide = $(this).attr("rel");
				if (currentSlide != nextSlide && !animationInProgress) {
					changeSlide(currentSlide, nextSlide);
					showInfo(this);
				}
				$(this).parent().children(".current").removeClass("current");
				$(this).addClass("current");
			});
		});
	});
});

/* hover galerie */
$(function() {
	$('div#featured > div.boxes > div.box').each(function (i, box) {
		var copyContainer = false;
		$('.boxFunction ul.pictures li.picture a', this).each(function (j, el) {
			/*if(!copyContainer) {
				copyContainer = true;
				var p = document.createElement('p');
				p.id = 'copytext_'+i;
				p.className = "p_copy";
				$('.boxVisual', box).append(p);
			}
			var copytext = $(this).children('img').attr('title');
			if(copytext.length > 1 &&  j == 0) {
				$('#copytext_'+i).text(copytext);
			}*/
			$(this).click(function(e) {
				$('#copytext_'+(i+1)).text($(this).children('img').attr('alt'));
				$('div.boxImage img').eq(i).attr('src',this.href);
				// detect if ie6 and reposition the copyright text from top because ie6 ignore an axis
				if (typeof document.body.style.maxHeight == "undefined") $('#copytext_'+(i+1)).css('top',($('div.boxImage img').eq(i).height()-22));
				return false;
			});
		});
	});
});

//* Mutare poze relationate langa vizual secundar
$(function() {
	if($('body.article #body #gallery')){
		$('body.article #body div.gallery_holder').append($('body.article #body #gallery'));
			var copyContainer = false;
			$('div.gallery_holder #gallery ul li a').each(function (j, el) {
				$(this).click(function(e) {
					$('#copytext_0').html($(this).children('img').attr('alt'));
					$('#visual2 img').eq(0).attr('src',this.href);
					return false;	
				});
			});
	}

	if ($('div.visual1 ul.pictures')){
			$('div.visual1 ul.pictures li a').each(function(i,el){
				$(this).click( function(e){
					$('div.visual1 img').eq(0).attr('src',$(this).attr('href'));
					e.preventDefault();
				});
			});
	}
});

//form doneazaOnline show donatePurpose + voluntaryForm show availability hours
$(function() {
	$("div.availability_info p input").each(function(i){
		if ($(this).attr('checked')){
			$("div.availability_info p.hiddenElement").eq(i).show();
		}
		$(this).click(function(e) {
			$("div.availability_info p.hiddenElement").eq(i).toggle();
		});
	});

	$("#howMuch div.element p input").each(function(i) {
		if ($(this).is(":checked")) {
			$("p.donatePurpose").eq(i).show();
			$("#donateFormGallery ul li").hide();
			$("#donateFormGallery ul li").eq(i).show();

			var sumaDonata = $('div.elementValues p label').eq(i).html().toLowerCase();
			$("h3.innerTitle em.donateValueTitle").html(sumaDonata);
			
			if (sumaDonata=='altă sumă') {
				$("div.elementDescription").hide();
			}
		}
		return;
		$(this).click(function(e) {
			$("h3.innerTitle em.donateValueTitle").html($('div.elementValues p label').eq(i).html().toLowerCase());
			$("div.elementDescription").show();
			$("input#otherSum, span#ron").hide();
			$("p.donatePurpose").hide();
			$("p.donatePurpose").eq(i).fadeIn();
			$("#donateFormGallery ul li").hide();
			$("#donateFormGallery ul li").eq(i).fadeIn();
			
		});
	});
	$('body.homepage #secondary ul.related p.subtitle').remove();
});

//form companii show/hide forms, form doneazaOnline show otherSum input onClick, form voluntary show/hide whereFrom other
$(function() {
	$("#productSale").click(function() {
		$("#companyProductSale").show();
		$("#companyEvent").hide();
	});
	if ($("fieldset#companyData input#productSale").is(":checked")) {
		$("fieldset#companyProductSale").show();
	}
	$("#events").click(function() {
		$("#companyProductSale").hide();
		$("#companyEvent").show();
	});
	if ($("fieldset#companyData input#events").is(":checked")) {
		$("fieldset#companyEvent").show();
	}
	$("input#other").click(function() {
		$("div.elementDescription").hide();
		$("input#otherSum, span#ron").fadeIn();
	});
	if ($("input#other").is(":checked")) {
		$("input#otherSum, span#ron").show();
	}
	$("input#voluntary-WhereFrom1, input#voluntary-WhereFrom2").click(function() {
		$("input#voluntary-WhereFrom3Content").hide();
	});
	$("fieldset#voluntary div.element_radio p input#voluntary-WhereFrom3").click(function() {
		$("input#voluntary-WhereFrom3Content").fadeIn();
	});
	if ($("input#voluntary-WhereFrom3").is(":checked")) {
		$("input#voluntary-WhereFrom3Content").show();
	}
	
});


// VIDEO Templating
function playMovie(swfID,file){
	var me;
	if(navigator.appName.indexOf("Microsoft") != -1){
          me = window[swfID];
     }else{
          me = document[swfID];
     }
     me.PlayMovie(file);
	 /*document.getElementById("movieTitle").innerHTML = title;
	 document.getElementById('embedTxt').value = "<object type='application/x-shockwave-flash' data='http://www.europafm.ro/&amp;res/swf/ngxplayer/ngxplayerEmbed.swf' width='542' height='407' id='ngxPlayer'><param name='allowScriptAccess' value='always' /><param name='movie' value='http://www.europafm.ro/&amp;res/swf/ngxplayer/ngxplayerEmbed.swf' /><param name='quality' value='high' /><param name='wmode' value='transparent' /><param name='scale' value='noscale' /><param name='allowFullScreen' value='true' /><param name='flashVars' value='stream="+file+"'/></object>";*/
}

$(function() {
	$('body.article ul.visual_list li a').click( function (e) { 
		playMovie('ngxPlayer',this.href);
		e.preventDefault();
	});
	
	$("#primary:empty").hide();
	if ($("#secondary").children().length ==0 )	{ $("#secondary").remove();}
	

	var blocksToResize = new Array("secondary", "tertiary");
	for (var i=0; i<blocksToResize.length; i++) {
		resizeBox(blocksToResize[i]);
	}

	/*if ($("#gallery").height() != null){
		$("div.visual2 p.p_copy").css("bottom",$("#gallery").height()+4);
	}else{
		$("div.visual2 p.p_copy").css("bottom",0);
	}*/
});


function resizeBox(container){
	var maxHeightValue = 0;
	$("#"+container+" div.boxContent").each(function(){
		
		if (($.browser.msie) && ($.browser.version == "6.0")) {
			maxHeightValue = (maxHeightValue < $(this).innerHeight()) ? $(this).innerHeight()-20 : maxHeightValue;
		} else {
			maxHeightValue = (maxHeightValue < $(this).height()) ? $(this).height() : maxHeightValue;
		}
		
	});
	$("#"+container+" div.boxContent").css('height',maxHeightValue);

}

//*
//* ICONITE SIDEBAR
$(function(){
	var tempElement = {}
	$('#sitewidepromo div.boxPresentation').each(function(i,el){
		if (i <4){
			tempElement.ico = document.createElement('span');
			tempElement.ico.id= "sidebar_icon"+i;
			tempElement.ico.innerHTML = "&nbsp;";
			$(this).prepend(tempElement.ico);
			$(tempElement.ico).addClass('iconita');
		}
	});
});


//*
// Galerie LightBox 
$(function() {
	$('body.gallery #related_gallery a').lightBox({fixedNavigation:true});
});

// hover ieFix sidebar
$(function() {
	$("#sidebar div.box div.boxContent div.boxFunction ul.related li").each(function(i) {
		$(this).mouseover(function() {
			$(this).addClass("hover");

		}).mouseout(function() {
			$(this).removeClass("hover");
		});
	});
});

//mut pozele relationate din formularul de donatii online in the right place
$(function() {
	$("#donateFormGallery_holder").append($("#donateFormGallery"));
	//$("#donateFormGallery_holder li.picture:eq(0)").show();
});
