/*!
 * Color animation jQuery-plugin
 * http://www.bitstorm.org/jquery/color-animation/
 * Based on code copyright 2007 John Resig
 * Copyright 2010 Edwin Martin <edwin@bitstorm.org>
 * Released under the MIT and GPL licenses.
 */

(function(d){function h(a,b,c){return"#"+g(a[0]+c*(b[0]-a[0]))+g(a[1]+c*(b[1]-a[1]))+g(a[2]+c*(b[2]-a[2]))}function g(a){a=parseInt(a).toString(16);return a.length==1?"0"+a:a}function e(a){var b,c;if(b=/#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(a))c=[parseInt(b[1],16),parseInt(b[2],16),parseInt(b[3],16)];else if(b=/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(a))c=[parseInt(b[1],16)*17,parseInt(b[2],16)*17,parseInt(b[3],16)*17];else if(b=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a))c=
[parseInt(b[1]),parseInt(b[2]),parseInt(b[3])];return c}var i=["color","backgroundColor","borderBottomColor","borderLeftColor","borderRightColor","borderTopColor","outlineColor"];d.each(i,function(a,b){d.fx.step[b]=function(c){if(!c.init){c.a=e(d(c.elem).css(b));c.end=e(c.end);c.init=true}c.elem.style[b]=h(c.a,c.end,c.pos)}});d.fx.step.borderColor=function(a){if(!a.init)a.end=e(a.end);var b=i.slice(2,6);d.each(b,function(c,f){a.init||(a[f]={a:e(d(a.elem).css(f))});a.elem.style[f]=h(a[f].a,a.end,a.pos)});
a.init=true}})(jQuery);





/*
 * In-Field Label jQuery Plugin
 * http://fuelyourcoding.com/scripts/infield.html
 *
 * Copyright (c) 2009 Doug Neiner
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 0.1
 */
(function($){$.InFieldLabels=function(label,field,options){var base=this;base.$label=$(label);base.$field=$(field);base.$label.data("InFieldLabels",base);base.showing=true;base.init=function(){base.options=$.extend({},$.InFieldLabels.defaultOptions,options);base.$label.css('position','absolute');var fieldPosition=base.$field.position();base.$label.css({'left':fieldPosition.left,'top':fieldPosition.top}).addClass(base.options.labelClass);if(base.$field.val()!=""){base.$label.hide();base.showing=false;};base.$field.focus(function(){base.fadeOnFocus();}).blur(function(){base.checkForEmpty(true);}).bind('keydown.infieldlabel',function(e){base.hideOnChange(e);}).change(function(e){base.checkForEmpty();}).bind('onPropertyChange',function(){base.checkForEmpty();});};base.fadeOnFocus=function(){if(base.showing){base.setOpacity(base.options.fadeOpacity);};};base.setOpacity=function(opacity){base.$label.stop().animate({opacity:opacity},base.options.fadeDuration);base.showing=(opacity>0.0);};base.checkForEmpty=function(blur){if(base.$field.val()==""){base.prepForShow();base.setOpacity(blur?1.0:base.options.fadeOpacity);}else{base.setOpacity(0.0);};};base.prepForShow=function(e){if(!base.showing){base.$label.css({opacity:0.0}).show();base.$field.bind('keydown.infieldlabel',function(e){base.hideOnChange(e);});};};base.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(base.showing){base.$label.hide();base.showing=false;};base.$field.unbind('keydown.infieldlabel');};base.init();};$.InFieldLabels.defaultOptions={fadeOpacity:0.5,fadeDuration:300,labelClass:'infield'};$.fn.inFieldLabels=function(options){return this.each(function(){var for_attr=$(this).attr('for');if(!for_attr)return;var $field=$("input#"+for_attr+"[type='text'],"+"input#"+for_attr+"[type='password'],"+"textarea#"+for_attr);if($field.length==0)return;(new $.InFieldLabels(this,$field[0],options));});};})(jQuery);





/**
 *		Csekkolja, hogy a kapott változó üres-e
 *		@param 
 *		@return bool
 */
function empty( variable ) {
	if( variable == '' )
		return true;
	return false;
}





// változók a nap biztonsági kérdéshez
var today = new Date();
var date = new Array('vasárnap', 'hétfő', 'kedd', 'szerda', 'csütörtök', 'péntek', 'szombat');
var date2 = new Array('vasarnap', 'hetfo', 'kedd', 'szerda', 'csutortok', 'pentek', 'szombat');

var default_color = '#7b7b7b';
var red_color = 'rgb(193, 27, 23)';





$(function() {
	
	/*
	 *		FOOTER FIX (ha kisebb a tartalom mint a window magassága, akkor igazítsa a footert az oldal legaljára)
	 */
	if( $('.wrapper').height() + $('#footer').height() < $(window).height() )
		$('.wrapper').height( $(window).height() - $('#footer').height() );
	$(window).resize(function() {
		if( $('.wrapper').height() + $('#footer').height() < $(window).height() )
			$('.wrapper').height( $(window).height() - $('#footer').height() );
	});
	
	/*
	 *		link hover effect (...)
	 */
	$('a').hover(function() {
		
		if( $(this).parent().parent().attr('id') == 'menu' ) {
			if( $(this).attr('id') == 'selected' )
				$(this).css('color', '#a1d54e').stop().animate({ color : '#a1d54e' }, 275); // talán elég a stop is
			
			else
				$(this).css('color', '#404040').stop().animate({ color : '#a1d54e' }, 275);
		}
		
		else {
			$(this).css('color', '#81ab3f').stop().animate({ color : '#aed175' }, 275);
		}
		
	}, function() {
		
		if( $(this).parent().parent().attr('id') == 'menu' ) {
			if( $(this).attr('id') == 'selected' )
				$(this).stop().animate({ color : '#a1d54e' }, 275);
			
			else
				$(this).stop().animate({ color : '#404040' }, 275);
		}
		
		else
			$(this).stop().animate({ color : '#81ab3f' }, 275);
		
	});
	
	/*
	 *		FOOTER TOOLTIP (...)
	 */
	$('#footer #social a').hover(function() {
		
		var window_width = $(window).width();
		var tooltip_text = $(this).attr('title');
		var position = $(this).position();
		var left_pos = position.left - 57;
		
		$('<div class="tooltip">' + tooltip_text + '</div>').appendTo('#footer').hide().css('left', left_pos + 'px').stop().fadeIn(250);
		
	}, function() {
		
		$('.tooltip').stop().fadeOut(250);
		
	});
	
	// ADD NEWS LABEL (hírlevél JS fix)
	$('<label for="news_email">e-mail cím</label>').appendTo('#news p:first');
	
	/*
	 *		NEWS_EMAIL VALIDATION (üzenetküldés formnál rossz érték esetén pirosra színezés)
	 */
	$('#news label').inFieldLabels();
	$('#news_email').click(function() { // ha piros és van benne szöveg akkor fade, ha piros és üres akkor egyből megehet default színre
		if( $(this).css('color') == red_color )
			if( empty($(this).val()) )
				$(this).css('color', default_color);
			else
				$(this).stop().animate({ color: default_color }, 500);
	});
	$('#news_submit').click(function() {
		email = $('#news_email');
		news_email_send = true;
		
		if( !checkEmailAddress(email.val()) ) {
			news_email_send = false;
			email.stop().animate({ color: red_color });
		}
		else {
			if( email.css('color') == red_color )
				email.stop().animate({ color: default_color }, 500);
		}
		
		if( !news_email_send ) {
			$('#news_submit').attr('disabled', 'disabled');
			$('#news').effect('shake', { distance: 8 }, 50, function() { $('#news_submit').attr('disabled', '') });
			return false;
		}
		else {
			$('#news_submit').attr('disabled', 'disabled');
			
			$.ajax({
				url: '/news', 
				data: $('#news').serialize(),
				type: 'POST',
				
				success: function(result) {
					$('#news_email').val('');
					$('#news_submit').attr('disabled', '');
					if( result == 'ok' )
						alert('Sikeres feliratkozás!');
					else
						alert(result);
				}
			});
			
			return false;
			
		}
	});
	
	/*
	 *		OPEN NEW WINDOW strict dtd FIX
	 */
	$('.blank').click(function() {
		window.open(this.href);
		return false;
	});
	
	/*
	 *		FORM SEND JS STUFF
	 */
	$('#contact-form').submit(function() {
		name = $('#name').val();
		email = $('#email').val();
		message = $('#message').val();
		day = $('#day').val();
		
		contact_email_send = true;
		
		if( empty(name) ) {
			contact_email_send = false;
			$('.name').animate({ color: red_color }, 500);
		}
		else if( !empty(name) && $('.name').css('color') == red_color )
			$('.name').animate({ color: default_color }, 500);
		
		if( !checkEmailAddress(email) ) {
			contact_email_send = false;
			$('.email').animate({ color: red_color }, 500);
		}
		else {
			if( $('.email').css('color') == red_color )
				$('.email').animate({ color: default_color }, 500);
		}
		/*if( empty(email) ) {
			send = false;
			$('.email').animate({ color: red_color }, 500);
		}
		else if( !empty(email) ) {
			if( email.search('@') == -1 ) {
				send = false;
				$('.email').animate({ color: red_color }, 500);
			}
			
			else {
				email = email.split('@');
				
				if(  email[1].search('[.]') == -1 ) {
					send = false;
					$('.email').animate({ color: red_color }, 500);
				}
				
				else {
					var email2 = email[1].split('.');
					email[1] = email2[0];
					email[2] = email2[1];
					
					if( empty(email[0]) || empty(email[1]) || empty(email[2]) ) {
						send = false;
						$('.email').animate({ color: red_color }, 500);
					}
					
					else {
						if( $('.email').css('color') == red_color )
							$('.email').animate({ color: default_color }, 500);
					}
				}
			}
		}*/
		
		if( empty(message) )
			contact_email_send = false;
		
		if( empty(day) ) {
			contact_email_send = false;
			$('.day').animate({ color: red_color }, 500);
		}
		else if( !empty(day) ) {
			if( day == date[today.getDay()] || day == date2[today.getDay()] ) {
				$('.day').animate({ color: default_color }, 500);
			}
			else {
				send = false;
				$('.day').animate({ color: red_color }, 500);
			}
		}
		
		if( !contact_email_send ) {
			$('#submit').attr('disabled', 'disabled');
			$('.large-box').effect('shake', { distance: 8 }, 50, function() { $('#submit').attr('disabled', '') });
			return false;
		}
		else {
			$('#submit').attr('disabled', 'disabled');
			
			$.ajax({
				url: '/kapcsolat2', 
				data: $('#contact-form').serialize(),
				type: 'POST',
				
				success: function(result) {
					$('#name').val('');
					$('#email').val('');
					$('#message').val('');
					$('#day').val('');
					$('#submit').attr('disabled', '');
					$('<span>' + result + '</span>').appendTo('#contact-form').hide().fadeIn(250).delay(1000).fadeOut(250);
				}
			});
			
			return false;
			
		}
	});
	
	/*
	 *		REFERENCES MENU HOVER EFFECT
	 
	$('#menu li').hover(function() {
		//$(this).css('background-color', 'red')
		left_pos = ( ( $('#menu .index').width() + $('#menu .services').width() + ($('#menu .references').width()/2) + 110 ) );
		//console.log(left_pos)
		$(this).find('ul:first').css('left', left_pos).stop().fadeIn(500);
		
	}, function() {
		
		$('#submenu').stop().fadeOut(750);
		
	});*/
	
	/*
	 *		REFERENCES HOVER EFFECT (a referencia képeknél)
	 */
	$('#references a.fancybox').hover(function() {
		if( $(this).children('img').attr('class') != 'hidden' )
			$(this).parent('.reference').css('background-color', '#fff');
		else
			$(this).parent('.reference').css('background-color', '#ffcc66');
		$(this).children('img').stop().fadeTo(500, 0.45);
	}, function() {
		$(this).children('img').stop().fadeTo(1000, 1);
	});
	
	$('#references a.fancybox').click(function() {
		$(this).children('img').stop().fadeTo(1000, 1);
	});
	
});





/*
 *		Email ellenőrző függvény
 *			@param e-mail cím
 *			@return true helyes email cím esetén, false különben
 */
function checkEmailAddress( emailStr ) {
	// The following variable tells the rest of the function whether or not to verify that the address ends in a two-letter country or well-known TLD.  1 means check it, 0 means don't.
	var checkTLD = 1;
	
	// The following is the list of known TLDs that an e-mail address must end with.
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	// The following pattern is used to check if the entered e-mail address fits the user@domain format. It also is used to separate the username from the domain.
	var emailPat = /^(.+)@(.+)$/;
	
	// The following string represents the pattern for matching all special characters. We don't want to allow special characters in the address. These characters include ( ) < > @ , ; : \ " . [ ]
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	// The following string represents the range of characters allowed in a username or domainname. It really states which chars aren't allowed.
	var validChars = "\[^\\s" + specialChars + "\]";
	
	// The following pattern applies if the "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com is a legal e-mail address.
	var quotedUser = "(\"[^\"]*\")";
	
	// The following pattern applies for domains that are IP addresses, rather than symbolic names. E.g. joe@[123.124.233.4] is a legal e-mail address. NOTE: The square brackets are required.
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	// The following string represents an atom (basically a series of non-special characters.)
	var atom=validChars + '+';
	
	// The following string represents one word in the typical username. For example, in john.doe@somewhere.com, john and doe are words. Basically, a word is either an atom or quoted string.
	var word = "(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	
	// The following pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above.
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	
	// Finally, let's start trying to figure out if the supplied address is valid.
	
	// Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze.
	var matchArray = emailStr.match(emailPat);
	
	if( matchArray == null ) {
		// Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address.
		//alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	
	var user = matchArray[1];
	var domain = matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for( i = 0; i < user.length; i++ ) {
		if( user.charCodeAt(i) > 127 ) {
			//alert("Ths username contains invalid characters.");
			return false;
		}
	}
	
	for( i = 0; i < domain.length; i++ ) {
		if( domain.charCodeAt(i) > 127 ) {
			//alert("Ths domain name contains invalid characters.");
			return false;
		}
	}
	
	// See if "user" is valid
	if( user.match(userPat) == null ) {
		// user is not valid
		//alert("The username doesn't seem to be valid.");
		return false;
	}
	
	// if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid.
	var IPArray = domain.match( ipDomainPat );
	if( IPArray != null ) {
		// this is an IP address
		for( var i = 1; i <= 4; i++ ) {
			if( IPArray[i] > 255 ) {
				//alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	
	for( i = 0; i < len; i++ ) {
		if( domArr[i].search(atomPat) == -1 ) {
			//alert("The domain name does not seem to be valid.");
			return false;
		}
	}
	
	// domain name seems valid, but now make sure that it ends in a known top-level domain (like com, edu, gov) or a two-letter word, representing country (uk, nl), and that there's a hostname preceding the domain or country.
	if( checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1 ) {
		//alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	if( len < 2 ) {
		//alert("This address is missing a hostname!");
		return false;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

