var sEmailPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/i;

$(document).ready(function(){
  	
	// Recent news
	if (typeof(recentNews) !== 'undefined')	{
		setTimeout(switchNews, RNSwitch);
	}
	
	// Inputs with place holders
	$('.placeholder').each(function(index, el){

		var el = $(el);
		el.blur(placeholderInputBlur);
		el.focus(placeholderInputFocus);
		el.val(el.attr('placeholder'));

		el.parents('form:not(.submit-keep-placeholders)').bind('submit', placeholderFormSubmit);
	});
});

// Placeholders

function placeholderInputFocus(event){

   var el = $(event.target);

   if (el.hasClass('placeholder')){
      el.val('');
      el.removeClass('placeholder');
   }
}

function placeholderInputBlur(event){

   var el = $(event.target);

   if (!el.hasClass('placeholder') && el.val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') == ''){
      el.val(el.attr('placeholder'));
      el.addClass('placeholder');
   }
}

function placeholderFormSubmit(event){

   var form = $(event.target);
   
   form.find('input.placeholder').each(function(index, el){
      placeholderInputFocus({'target': $(el)});
   });

   return true;
}

function placeholderFormSubmitCancel(form){

   var form = $(form);
      
   form.find('input.placeholder').each(function(index, el){
      placeholderInputBlur({'target': $(el)});
   });

   return true;
}

// Recent Newss

var RNFadeout = 180;
var RNSlide = 400;
var RNSwitch = 10000;

function updateRecentNewsContent(){
    var item = recentNews[recentNewsIndex];
    $('#newsbar .contentswrapper .contents').html('<span class="date">' + item.date + '</span> | <a href="' + sBaseUrl + 'news/view/id/' + item.id + '">' + item.content + '</a>');
}

function switchNews() {

    if (recentNewsIndex < recentNews.length-1){
        recentNewsIndex += 1;
    }
    else {
        recentNewsIndex = 0;
    }

    var block = $('#newsbar .contentswrapper .contents');
    
    block.fadeOut(RNFadeout, function(){    
        updateRecentNewsContent();
        $(this).css({
            marginLeft: '750px'
            
        }).show().animate({
            marginLeft: '0px'
            
        }, RNSlide);
    });
    
    setTimeout(switchNews, RNFadeout + RNSlide + RNSwitch);
}

// Image preload

jQuery.preloadImage = function(src) {
	jQuery("<img>").attr("src", src);
}

jQuery.preloadImages = function() {
	jQuery.each (arguments,function (e) {
		jQuery("<img>").attr("src", this);
	});
}

// Check Popup Form

function checkPopupForm(eForm) {
	
	var sAlert = '';
	var aFields = $(eForm).find('.required').each(function(index, el){

		var aField = $(el);
		
		if (aField.val() == aField.attr('placeholder') ||
			(!aField.is('select') && aField.val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') == '')){
				
			sAlert = sAlert + '\n - ' + aField.attr('placeholder').replace(/\*/g, '');
		}
	});
	
	if (sAlert != '') {		
		alert('Please fullfill following fields:' + sAlert);		
		return false;
	}
	else {
		placeholderFormSubmit({'target': eForm});
		return true;
	}
}
