// Returns string Ex: "1.3.1"
// alert("jQuery version" + $().jquery); 
jQuery.fn.delay = function(time,func){
    return this.each(function(){
        setTimeout(func,time);
    });
};


// adds an effect called "myEffect" to the overlay
$.tools.overlay.addEffect("fade", 
   function(position, done) {
      this.getOverlay().css(position).fadeIn(1500, done);
   },
   function(done) {
      this.getOverlay().fadeOut(1500, done);
   }
);

$(document).ready(function() {
	$(".show_edit_class").click(function(event)
	{
		if ($(this).hasClass("underground")){
			toggleAdminEdit(true);
			$(this).removeClass("underground");
			$(".show_edit_class a").css({"color" : "yellow"});

		}else{
			toggleAdminEdit(false);
			$(this).addClass("underground");
			$(".show_edit_class a").css({"color" : "red"});
		}
		
		return false;
	});

	repositionNiceMenu();
	dev_tabs_show();
	setAdvertCompetition();
	
});

function toggleAdminEdit(showValue){
	$(".edit").each( 
	function() {
		if (showValue && ($(this).css("display") == "none")){
			$(this).show();
		}
		if(!showValue && ($(this).css("display") != "none")){
			$(this).hide();
		}
	});
}

function repositionNiceMenu(){
//*
 $("ul#nice-menu-1>li>ul").each( 
	function() {

		var pWidth = $(this).parent().width();
		var tWidth = $(this).width();
		var diff = tWidth - pWidth;
		diff = (-1)* Math.round(diff/2);
		$(this).css("position", "absolute");
		$(this).css("left", diff+"px");
		$(this).css("top", "24px");
	});
// */
}


function dev_tabs_show(){
	$('#contentCenter ul.primary').delay(200,function(){ 
		$('#contentCenter ul.primary').fadeOut('fast');
	});
	$('#contentCenter div.messages').delay(200,function(){ 
		$('#contentCenter div.messages').fadeOut('fast');
	});
	$('#contentCenter').mouseenter(function(){
		$('#contentCenter ul.primary').fadeIn('fast');	
		$('#contentCenter div.messages').fadeIn('fast');	
	}).mouseleave(function(){
		$('#contentCenter ul.primary').fadeOut('fast');	
		$('#contentCenter div.messages').fadeOut('fast');	
	});
}



function setAdvertCompetition(){
	bookMark("#bookmark");
	closeParentClick(".closeButton", ".advert_competition");

	$(".overlay").overlay({mask: '#000', effect: 'apple'});
	
	$("#competition_form").click(function(event){
		event.preventDefault();
		var callURL = Drupal.settings.basePath + "trilobita/ajax/advert_competition";
		$('.advert_competition .result').html("");
		$.get(callURL, function(data) {
		  $('.advert_competition .result').html(data);
		  $('.advert_competition .result').overlay({mask: '#000', effect: "fade", load: true, fixed: true});
		  // Validation 2011.06.29.
		  $('#trilobita-advert-competition-form').validate({ messages: {  email: { required: "Kérjük adjon meg egy e-mail címet!", email: "Kérjük adjon meg egy valós e-mail címet!" } } });
		  
		  $(".advert_competition .result #edit-submit").click(function(event) {
		  	event.preventDefault();
		  	
		  	var action = $('#trilobita-advert-competition-form').attr('action');
		  	var email =  $('#trilobita-advert-competition-form #edit-email').val();
		  	var subject =  $('#trilobita-advert-competition-form #edit-subject').val();
		  	var messageStr =  $('#trilobita-advert-competition-form #edit-message').val();
		  	/* var attachment =  $('#trilobita-advert-competition-form #markup-attachment').html(); */
		  	var token =  $('#trilobita-advert-competition-form #edit-trilobita-advert-competition-form-form-token').val();
		  	
		  	// "attachment" : attachment,
		  	$.post(action, {'email' : email, "subject" : subject, "message" : messageStr, "token" : token}, 
		  		  function(data) {
					  //$('.advert_competition .result').html(data).overlay({mask: '#000', effect: 'apple', load: true});
					   $(".advert_competition .result").overlay().close(); 
					 //  $(".advert_competition .result").overlay().fadeOut(); 
				   });
		  });
		  //.css({"display": "block"})
		});
	});
	
	// FONTOS	
	$('.advert_competition').fadeIn(3000);
}


// Bookmark from tridoc.eu

function bookMark(target) {

	$(target).click(
	function(event){
		event.preventDefault();
/*		var url = this.href;
		var title = this.title;
*/
		var url = window.location;
		var title = window.document.title;

//		alert(title + url);
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url, "");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing
		} else { 
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually!');
		}
	});
}

function closeParentClick(target , selector){
	$(target).click(function(event){
		event.preventDefault();
		$(this).parent().parent().fadeOut();
		//alert("selector : " + selector);
		/*
			alert("selector : " + selector);

			$(this).css({"border": "3px solid red"}).fadeOut();
		*/
	});
}























