// -------------------------------------------------------------------------------------------
// Cufon
// ------------------------------------------------------------------------------------------- 	
Cufon.replace(['.cufon'], {
    hover: true
});


// -------------------------------------------------------------------------------------------
// Primary Navigation
// ------------------------------------------------------------------------------------------- 	
$(document).ready(function(){
	$('nav#primary a').attr('title','');	
	$('nav a').click(function(){
		var thisClass = $(this).attr('class');
		if(thisClass != "active"){
			$('nav a').removeClass('active');
			$(this).addClass('active');
		}
	});		
	$('nav a').each(function(){
		if($(this).attr('href') == window.location.hash){
			$('nav a').removeClass('active');
			$(this).addClass('active');
		}
		if(window.location.hash == ""){
			//$('nav#primary a:first').addClass('active');
		}
	});	
});



// -------------------------------------------------------------------------------------------
// Scroll 
// ------------------------------------------------------------------------------------------- 	

$(window).load(function() {
	try{
	mCustomScrollbars();
	}catch(e){}
});

function mCustomScrollbars(){
	$("#mcs_container, #mcs2_container, #mcs3_container, #mcs4_container").mCustomScrollbar("vertical",500,"easeOutCirc",1,"auto","yes","yes",10); 
}

$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

function LoadNewContent(id,file){
	$("#"+id+" .customScrollBox .content").load(file,function(){
		mCustomScrollbars();
	});
}



// -------------------------------------------------------------------------------------------
// Contacts 
// ------------------------------------------------------------------------------------------- 	
$(document).ready(function(){
	
	$('#contactmap a').live('click',function(){

		var contactPage = $(this).attr('class');
		$('#contactmap a').removeClass('selected');
		$(this).addClass('selected');
		$('#contact .left').fadeIn(500).addClass('open');
		$('#contact_'+contactPage+'').siblings('.location').hide();
		$('#contact_'+contactPage+'').fadeIn(500);
	
	});	
	
});



// -------------------------------------------------------------------------------------------
// Validate
// ------------------------------------------------------------------------------------------- 	
function formValidation() {  
 
    var emailError = 0;
    var numberError = 0;

    if (validateEmail() != 1) {
        return true;
    } else {
        $('p.novisible').removeClass('novisible').addClass('error');
        return false;
    };

    function validateEmail() {
        if ($('input#email').val() == "") {
            emailError = 1;
        } else {
            var a = $('input#email').val();
            var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
            if (filter.test(a)) {
                emailError = 0;
            }
            else {
                emailError = 1;
            }
        }
        return emailError;
    }
}	



// -------------------------------------------------------------------------------------------
// Vertical Align
// ------------------------------------------------------------------------------------------- 	
$(document).ready(function(){
	var newMargin = ($(document).height() - $('#intro').height()) * 0.4;
	if(newMargin > 0){
		$('#intro').css({'margin-top':newMargin});
	}else{
		$('#intro').css({'margin-top':'20px'});
	}
});

$(window).resize(function() {
	var newMargin = ($(document).height() - $('#intro, #site').height()) * 0.4;
	if(newMargin > 0){
		$('#intro, #site').css({'margin-top':newMargin});
	}else{
		$('#intro, #site').css({'margin-top':'20px'});
	}
});

// -------------------------------------------------------------------------------------------
// Ajax 
// ------------------------------------------------------------------------------------------- 	

$(document).ready(function(){	
	$('nav a, .logo a').click(function (e){	
	
		if( $(this).parent().attr('class') == "logo" ){
			$('nav#primary a').removeClass('active');
			$('nav#primary a').eq(1).addClass('active');
		}
		
		checkURL(this.hash,"menu");		
	});			
	vault = $('#content').html();			
	setInterval("checkURL(hash='','check')",250);			
});

var lasturl = "";

function checkURL(hash,clickevent){	
	if(!hash){
		hash = window.location.hash;
	} 	
	if(hash != lasturl){
		lasturl = hash;	
		if(hash==""){		
			$('#content').html(vault);			
		}		
		if(hash!=""){	
			hash=hash.replace('#!/',''); 		
			loadPage(hash,clickevent);
		}		
	}		
	$('#primary .pt a').attr('href','http://www.fullservicesgroup.com/'+window.location.hash+'');	
	$('#primary .uk a').attr('href','http://www.fullservicesgroup.com/uk/'+window.location.hash+'');	
	
		
}

if(window.location.hash == ""){
	loadPage("about","init");
}


function loadPage(url,clickevent){	
	$.ajax({
		type: "POST", url: "load_page.php", data: 'p='+url, dataType: "html", success: function(msg){			
			if(parseInt(msg)!=0){								
				if(clickevent == "init"){					
					loadVideo();
					$('#content').html(msg);
					$('#content').addClass('hasLoaded');									
				}	
				if(clickevent == "check"){						
					
					if( window.location.hash == "" && $('#content').attr('class') == "hasLoaded"){
						loadSite(1);	
					}
					if( window.location.hash == "" && $('#content').attr('class') != "hasLoaded"){
						loadVideo();
						$('#content').html(msg);
						$('#content').addClass('hasLoaded');							
					}																	
				}
				if( window.location.hash != "" && clickevent != "menu" && $('#content').attr('class') != "hasLoaded" ){										
					$('#intro').hide();
					$('#content').addClass('hasLoaded');
					loadSite(1);							
					$('#content').animate({ opacity: 0 }, 500, function() { 
						$('#content').html(msg).css({opacity: 0, display: 'block', visibility: 'visible'});						
						setTimeout(function(){ 
							$('#content').animate({ opacity: 1 }, 500, function() {
								$('.frame').fadeIn(500);
								mCustomScrollbars();
								Cufon('.cufon');
							}); 
						}, 500);
					});	
					
						
													
				}		
				if(clickevent == "menu"){
					$('#content').animate({ opacity: 0 }, 500, function() { 
						$('#content').html(msg).css({opacity: 0, display: 'block', visibility: 'visible'});
						setTimeout(function(){ 
							$('#content').animate({ opacity: 1 }, 500, function() {
								$('.frame').fadeIn(500);
								mCustomScrollbars();
								Cufon('.cufon');
							}); 
						}, 500);
					});
				}				
			}
		}		
	});
}


/* load video */
function loadVideo(){
	$('#player1').mediaelementplayer({
		defaultVideoWidth: 676,
		defaultVideoHeight: 386,
		videoWidth: 676,
		videoHeight: 386,
		audioWidth: 500,
		audioHeight: 30,
		startVolume: 0.8,
		timerRate: 500,
		loop: false,
		enableAutosize: false,
		features: ['playpause'],			
		success: function (mediaElement, domObject) {            	   
			mediaElement.addEventListener('timeupdate', function(e) {			
				if(mediaElement.currentTime >= 3){
					//mediaElement.stop();							
				}								
				if(mediaElement.ended == true){
					startAnimation();
				}			
			}, false);
			$('#skip').click(function(e){
				mediaElement.stop();						
				startAnimation();
				//mediaElement.load();
				//mediaElement.play();				
			});									
			mediaElement.play();
			setTimeout("$('#skip').fadeIn(400)",4000);			
			$('#intro').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500);		
		},
		error: function () { }					
	});
}

		
/* vertical align */				
function verticalAlign(selected,position){
	var newMargin = ($(document).height() - $(selected).height()) * position;
	if(newMargin > 0){
		$(selected).css({'margin-top':newMargin});
	}else{
		$(selected).css({'margin-top':'20px'});
	}	
}

			
/* start animation */			
function startAnimation(){			
	$('#intro').fadeOut('slow', function() {
		verticalAlign('#site',0.5);	
		loadSite(2);						
	});	
}


/* load site */
function loadSite(ajax){	
	verticalAlign('#site',0.5);		
	if(ajax == 1){
		setTimeout( function() { 		
			$('#site').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {							
				$('#player2').mediaelementplayer({ audioWidth: 180, startVolume: 0.8, loop: true, features: ['playpause'], translationSelector: false, success: function (media) { media.play(); }, error: function () { } })				
				$('header').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {	
					$('header nav, nav#secondary').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {
						$('footer').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {
							$('#main').addClass('preloader');								
						});								
					}); 	
				}); 
			}); 
			mCustomScrollbars();
		}, 500);				
	}
	if(ajax == 2){
		setTimeout( function() { 		
			$('#site').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {							
				$('#player2').mediaelementplayer({ audioWidth: 180, startVolume: 0.8, loop: true, features: ['playpause'], translationSelector: false, success: function (media) { media.play(); }, error: function () { } })				
				$('header').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {	
					$('header nav, nav#secondary').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {
						$('footer').css({opacity: 0, display: 'block', visibility: 'visible'}).animate({ opacity: 1 }, 500, function() {
							$('#main').addClass('preloader');	
							$('.frame').fadeIn(500);
							mCustomScrollbars();							
						});								
					}); 	
				}); 
			}); 
		}, 500);		
		Cufon('.cufon');			
	}	
}


// -------------------------------------------------------------------------------------------
// Newsletter
// ------------------------------------------------------------------------------------------- 	
function send(datastr){
	$.ajax({
		type: "POST",
		url: "teste.php",
		data: datastr,
		cache: false,
		success: function(html){
			$("#response").fadeIn("slow");
			$("#response").html(html);
			setTimeout('$("#response").fadeOut("slow")',2000);
		}
	});
}
