/* GLOBAL javascript for the troygullageCom */

TroyG = {
	Navigation:{
		open:function(){
			$('#header .content').animate({
	    		top:'0px'
	  		}, 1000, function() {
	    		TroyG.Text.showH1();
	  		});
		}
	},
	Global:{
		init:function(){
			TroyG.ClientsBox.init();
			TroyG.ContactForm.init();
		}
	},
	Links:{
		rewriteExternal:function() {
			$('a.external').bind('click', function(e){
				$(this).attr('target', '_blank');
			});
		},
		rewriteInternal:function(){
			$.address.change(function(event) {
				$('#main').hide();
				$('#main-loader').show(); 
				
				if(event.value!='/'){
					$('#header .nav a').each(function() {
						if($(this).attr('href').match(/(.*)\.[^.]+$/)[1]==event.value){
							$(this).addClass('selected');
						}
						else{
							$(this).removeClass('selected');
						}
	                });
	            }
				 
                if(event.value=='/'){event.value='/index';} 
				$.ajax({
					url: event.value + '.php',
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						$.ajax({
							url:'index.php',
							success: function(data, textStatus, XMLHttpRequest) {
								TroyG.Links.handleData(data);
							}
						})
					},
					success: function(data, textStatus, XMLHttpRequest) {
						TroyG.Links.handleData(data);
					}
				});
    			
    		});
		},
		handleData:function(data){
			$('#main').html($('#main', data).html());
            $('#main-loader').hide();
            $('#main').fadeIn();
			$.address.title(/>([^<]*)<\/title/.exec(data)[1]);
			TroyG.Global.init();
		}
	},
	Text: {
		showH1:function(){
			$('#header h1').fadeIn();
		}
	},
	ClientsBox:{
		init:function(){
			$('#featured-clients-container ul.clients li img').bind('click',function(e){
				var that = $(this).parent();
				$('#tgcom #featured-clients-container').animate({
					paddingTop:'100px',
	    			height:'200px'
	  			}, 500, function(){
	  				$('#featured-clients-box').css('border', '1px solid #fff');
	    			$('#featured-clients-box').animate({
	    				width:'1022px'
	  				}, 500, function(){
	  					$('#featured-clients-container img.close').fadeIn('fast');
	  					$('#featured-clients-box .'+that.attr('class')).fadeIn();
	  				});
	  			});
			});
			$('#featured-clients-container img.close').bind('click',function(e){
				$('#featured-clients-box .client-content').hide();
				$(this).hide();
				$('#featured-clients-box').animate({
	    			width:'0px'
	  			}, 500, function(){
	  				$('#featured-clients-box').css('border', 'none');
	  				$('#tgcom #featured-clients-container').animate({
						paddingTop:'0px',
	    				height:'100px'
	  				}, 500, function(){
	    			
	  				});
	  			});
			});
		}
	},
	ContactForm:{
		init:function(){
			$("#ajax-contact-form").submit(function(){
				$('#main-loader').show(); 
				var str = $(this).serialize();
				$.ajax({
	   				type: "POST",
	  				url: "/tools/contact/validate.php",
	   				data: str,
	   				success: function(msg){
						$("#note").ajaxComplete(function(event, request, settings){
							$('#main-loader').hide(); 
							if(msg == 'OK'){
								$("#fields").hide();
								$("#notification-error").hide();
								$("#notification-ok").fadeIn('fast');
								setTimeout('$.address.value("/index")', 3000);
							}
							else{
								result = msg;
								$(this).html(result);
							}
						});
					}
				});
				return false;
			});
		}
	}
}


$(document).ready(function(){
	TroyG.Links.rewriteInternal();	
	TroyG.Links.rewriteExternal();
});

$(window).load(function(){	
	TroyG.Navigation.open();
});