// Initialization

$(document).ready(function(){    
	  
	// 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);
	});
	
	// Recent News
	if (recentNews.length > 0) {
	    updateRecentNewsContent(recentNewsIndex);
	    setTimeout(switchNews, RNSwitch);
	}
	
	// Tabs
	showTab('tabs', 1);
});
 
 
/* 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('[placeholder]').each(function(index, el){
      placeholderInputFocus({'target': $(el)});
   });

   return true;
}

function placeholderFormSubmitCancel(form){

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

   return true;
}

/* Ajax contact form */

function ajaxSubmitContactForm(eForm){
	eForm = $(eForm);

	placeholderFormSubmit({'target': eForm}); 

	$.post(eForm.attr('action') + '?ajax',
		eForm.serialize(),
		function(data){

			if (data.result) {
				eForm.parent().html('<p>' + data.message + '</p>');
			}
			else {         	
				alert(data.message);
				placeholderFormSubmitCancel(eForm); 
			}
		},
		"json"
	);

	return false;
}
   
// Tabs

function showTab(id, n){
    $('#'+id+' .tabcontent:not(#'+id+' #tabcontent-'+n+')').removeClass('active');
    $('#'+id+' #tabcontent-'+n).addClass('active');
    
    $('#'+id+' .tablink:not(#'+id+' #tablink-'+n+')').removeClass('active');
    $('#'+id+' #tablink-'+n).addClass('active');
}

// Recent Newss

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

function updateRecentNewsContent(){
    var item = recentNews[recentNewsIndex];
    $('#recentnews .contentwrapper .content').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 = $('#recentnews .contentwrapper .content');
                                      
    block.fadeOut(RNFadeout, function(){    
        updateRecentNewsContent();
        $(this).css({
            marginLeft: '550px'
            
        }).show().animate({
            marginLeft: '0px'
            
        }, RNSlide);
    });
    
    setTimeout(switchNews, RNFadeout + RNSlide + RNSwitch);
}
