/**
 * @author Tarjan Tamas - http://devberg.com/
 */

function allInit(){  
    loginSlide();
}

$(document).ready(function() { 
    allInit();
 });


/*-------------------------------------------    
        loginSlide
-------------------------------------------*/
function loginSlide() {
    
    var closeWidget = $("<a class='close'>Close</a>"); 
    $('#login .wrap').append(closeWidget);

    
    $("#login .close, .login_label a").click(function(){
        
        if ($('.login_label a').hasClass("open")) {
            $("#login ").slideToggle("fast","swing");
            $('.login_label a').removeClass('open');
        } else {
            $("#login ").slideDown("fast","swing");
            $('.login_label a').addClass('open');
        }
        return false;
    });
}



/*-----------------------------------------------
 			SLIDER SCRIPT 
 -----------------------------------------------*/
 
 $(document).ready(function() {      
     
    //a slideShow function
    slideShow();
 
});
 
function slideShow() {
 
    //0 -ra állítja a képek opacity-jét
    $('#head_base a').css({opacity: 0.0});
     
    //megjeleníti az első képet full opacity-vel)
    $('#head_base a:first').css({opacity: 1.0});
     
    //csinálja az animaciot
    $('#head_base .content').html($('#head_base a:first').find('img').attr('alt'))
    .animate({opacity: 0.7}, 400);
     
    //meghivja a gallery function-t, 6000 = 6 mp
    setInterval('gallery()', 5000);
     
}
 
function gallery() {
     
    //if no IMGs have the show class, grab the first image
    var current = ($('#head_base a.show')?  $('#head_base a.show') : $('#head_base a:first'));
 
    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#head_base a:first') :current.next()) : $('#head_base a:first'));   
     
    //Get next image caption
    var caption = next.find('img').attr('alt'); 
     
    //Set the fade in effect for the next image, show class has higher z-index
    next.css({opacity: 0.0})
    .addClass('show')
    .animate({opacity: 1.0}, 2000);
 
    //Hide the current image
    current.animate({opacity: 0.0}, 2000)
    .removeClass('show');
     
    //Set the opacity to 0 and height to 1px
    $('#head_base .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 }); 
     
    //Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
    $('#head_base .caption').animate({opacity: 0.7},100 ).animate({height: '100px'},500 );
     
    //Display the content
    $('#head_base .content').html(caption);
         
}



/*-----------------------------------------------
 		disable webkit autofill
 -----------------------------------------------*/
 
if ($.browser.webkit) {
    $('input[name="password"]').attr('autocomplete', 'off');
}
/*----------------------------------------------
 		placeholder attr. support
 -----------------------------------------------*/
	
	if(!Modernizr.input.placeholder){
	
		$('[placeholder]').focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		  }
		}).blur();
		$('[placeholder]').parents('form').submit(function() {
		  $(this).find('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
			  input.val('');
			}
		  })
		});
	
	};
		 

/*--------------------------------------------------
 		FORM VALIDATOR SCRIPT
 ---------------------------------------------------*/
 

	// Place ID's of all required fields here.
	required = ["email"];
	required_add = ["add"];
	// If using an ID other than #email or #error then replace it here
	add = $("#add")
	email = $("#email");
	errornotice = $("#error");
	// The text to show up within a field when it is incorrect
	emptyerror = "EZT A MEZŐT MINDENKÉPPEN TÖLTSD KI!";
	emailerror = "ADJ MEG EGY ÉRVÉNYES EMAIL CÍMET ADJ MEG!";
	addempty = "EZT IS!";
	adderror = "5+5=10";

	$("#theform").submit(function(){	
		//Validate required fields
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}

		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		//az 5+5 mezot is
		for (i=0;i<required_add.length;i++) {
			var input = $('#'+required_add[i]);
			if ((input.val() == "") || (input.val() == adderror)) {
				input.addClass("needsfilled");
				input.val(adderror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}

		//5+5 szabaly
		if (!/^.*10.*$/.test(add.val())) {
			add.addClass("needsfilled");
			add.val(adderror);
		}
	




		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();
		/*	return false;*/
		}
		
	    //---INNEN KEZDODIK AZ ELKULDES--- elhideolja a formot
	    $('#theform').hide();
	
	    //mutatja a loader-bar-t
	    $('.loader').append($('.bar'));
	    $('.bar').css({display:'block'});
	
	    //elküldi az ajax lekérést
	    $.get('mail.php',{name:$('#name').val(),
	                      email:$('#email').val(),
	                      comment:$('#message').val()},
	
	    //visszatér 
	    function(data){
	      //elhidolja a cuccokat
	      $('.bar').css({display:'none'});
	      $('.loader').append(data);
	    });
	
	  //az oldalon marad
	  return false;
		
	});
	
	// Kiuriti a mezöket, ha a user rajuk kattint
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");

	   }
	});


 
/*---------------------------------------------------
 			TOOLTIP 
 ----------------------------------------------------*/
 

    //Tooltips
    $(".tip_trigger").hover(function(){
        tip = $(this).find('.tip');
        tip.fadeIn(300); //Show tooltip
    }, function() {
        tip.fadeOut(300); //Hide tooltip
    }).mousemove(function(e) {
        var mousex = e.pageX + 20; //Get X coodrinates
        var mousey = e.pageY + 20; //Get Y coordinates
        var tipWidth = tip.width(); //Find width of tooltip
        var tipHeight = tip.height(); //Find height of tooltip

        //Distance of element from the right edge of viewport
        var tipVisX = $(window).width() - (mousex + tipWidth);
        //Distance of element from the bottom of viewport
        var tipVisY = $(window).height() - (mousey + tipHeight);

        if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
            mousex = e.pageX - tipWidth - 20;
        } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
            mousey = e.pageY - tipHeight - 20;
        }
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey, left: mousex });
    });
	$(".tip_trigger").click(function (event) {
		event.preventDefault();
	});

