

$(function(){
	
	$('input[type=text]').focus(function(){
		if($(this).attr('rel') == $(this).val()){
			$(this).val('');	
		}	
	});
	
	$('input[type=text]').blur(function(){
		if($(this).attr('rel') && !$(this).val()){
			$(this).val($(this).attr('rel'));	
		}	
	});
	
	$('.popup-btn').click(function(){
		loadPopup($(this).attr('href'));			   
	});
	
	$('#overlay, #popup .close-btn').live('click', function(){
		$('#popup').fadeOut(function(){
			$('#overlay').fadeOut('slow');	
			$('#popup > .content').html('');
			$('#popup > .loader').show();
		});						 
	});
	
	if($('.scroller').index !== 0){
		var currId = '0';
		var scrollSpeed = 600;
		var canScroll = true;
		$('.scroller .scroller-nav a').click(function(){
			var nextId = $(this).attr('rel');
			var scrollWidth = $('.scroller').width();
			if(!$('.scroller .scroll-item').is(':animated')){
				var currI =$('.scroller .scroll-item[rel='+currId+']').index();
				var nextI = $('.scroller .scroll-item[rel='+ nextId +']').index();
				var targetX = 0;
				
				if(currI < nextI){
					targetX = scrollWidth;
				} else if(currI > nextI){
					targetX = -scrollWidth;
				}
				
				if(nextId != currId){
					$('.scroller .scroller-nav li').removeClass('current');
					$(this).parent().addClass('current');
					$('.scroller .scroll-item[rel='+currId+']').animate({'left': -targetX}, scrollSpeed);
					
					$('.scroller .scroll-item[rel='+ nextId +']').css({'left': targetX}).show().animate({'left': 0}, scrollSpeed, function(){
						currId = nextId;
					});
				}
			}
		});
		
		$('.scroller').hover(function(){	
			canScroll = false;
		}, function(){
			canScroll = true;
		});
		
		setInterval(function(){
			if(canScroll){
				var nextId = ((currId) >= $('.scroller .scroll-item').size() - 1) ? 0 : parseInt(currId) + 1;
				$('.scroller .scroller-nav a[rel="'+nextId+'"]').click();
			}
		}, 5000);
	}
	
	$('#language-buttons a').hover(function(){
		$('#language-buttons a').not(this).fadeTo(0, 0.5);
	}, function(){
		$('#language-buttons a').not(this).fadeTo(0, 1);
	});
	
	//$('.dropshadow').dropshadow({left: 1, top: 1, blur: 7, color: "#333"});
	
	$('#top-image').load(function(){
		var yPos = -($(this).height() - $('#top').height()) / 2 + 10;
		$(this).css({top: yPos}).fadeIn();
	});
	$(window).resize(function(){
		onResize();					  
	});
	onResize();	
});

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = themeUrl + this;
    });
}

function loadPopup(url){
	$('html,body').animate({scrollTop: 0});
	$('#popup > .content').html('');
	$('#overlay').fadeIn('slow', function(){
		$('#popup').fadeIn();
		$('#popup > .loader').delay(200).fadeIn();
		$.get(url, function(data) {
			$('#popup > .loader').fadeOut(function(){
				$('#popup > .content').html(data);
			});
		});
	});		
}

function inputError(obj){
	obj.css("borderColor", "#F00");
}

function removeInputError(obj){
	obj.css("borderColor", "#CCC");
}

function validEmail(val){  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return emailPattern.test(val);  
}  

function onResize(){
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var docHeight = $(document).height();
	var docWidth = $(document).width();

	$('#popup').css({'left': (docWidth - $('#popup').width()) / 2});
}
