var objDeepLink = {};
objDeepLink.strCurrentCat = "recent";

objDeepLink.setup = function( strUrl ){
	
	$.address.init( function(event) {
		$('a').address(function() { 
			if( $(this).attr( 'target' ) != "_blank" )
				return $(this).attr('href').replace( strUrl, ''); 
			else if( $(this).attr( 'href' ) == "javascript:void(0)" )
				console.log( "topTop" )
			else
				window.open( $(this).attr('href') );
		});
		
    }).change(function(event) {
        // Sets the page title
		//console.log( $.address.title() );
        $.address.title( $.address.title().split(' | ')[0] + ' | ' + objDeepLink.splitLink( event.value  ) );
		
		objDeepLink.getPage();
		
    }).externalChange(function(event) {
        
    });


}

objDeepLink.splitLink = function( strRoute ){
	var arrParts = strRoute.split( '/' );
	var strTitle = "";
	
	for( var i=0; i< arrParts.length; i++ ){
		if( arrParts[i] != ""  && arrParts[i] != "page" && arrParts[i] != '<a href="' && arrParts[i] != '">' &&   arrParts[i] != "cat" &&   arrParts[i] != "project" ){
			var strCapitlised = arrParts[i].replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
			
			strCapitlised = strCapitlised.replace( /-/gi, " ").replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );;
			strTitle += strCapitlised + " | ";
		}
	}
	
	return strTitle;
	
}

objDeepLink.getPage = function(){
	var strPageUrl 			= $.address.path();
	var strValidateUrl 		= "/wp-content/themes/designbystudioade/scripts/getPage.php";
	var objInputs				= {};
	
	if( $.address.path() == "/" )
		$.address.path("/cat/recent/");
	objInputs[ "strUrl" ]	= strPageUrl;
	
	$.post( strValidateUrl, objInputs,
		function(data){
			data = $.evalJSON( data );
			
			switch(data.type ){
			case 'category':
				var booInit 		= true;
				var arrItemMin	 	= new Array( );
				var arrItemMax	 	= new Array( );
				var animSize = "small";
				
			  	if( $( "#source" ).length == 0)
					objDeepLink.createCatBody( );
				
				
					// see if any size animaton need to be applied to items
					$( data.items).find('li').each( function(i){
						var dataId 		= $(this).attr( 'data-id' );
						var itemClass 	= $(this).attr( 'class' );
					
						var booFound = false;
					
						$( "#source li" ).each( function(i){
							if( $( this ).attr( 'data-id' ) == dataId ){
							
								if( $( this ).attr( 'class' ) != itemClass ){
									booFound = true;
								
									if( itemClass != "small" ){ // going large to small
										animSize = "large";
									}
									
								}
								
							}
						});
						
						if( booFound ){
							if( animSize == "small" )
								arrItemMin.push( this );
							else{
								arrItemMax.push( this );
							}
								
						}
					
					});
					
					var IE7 = navigator.appVersion.indexOf("MSIE 8.") != -1;
					
					if( !IE7 ){
						$( "#source" ).quicksand( $(data.items).find('li'), {
							duration: 500,
							easing: 'easeInOutQuad',
							adjustHeight: 'false',
							enhancement: function() {
							
								if( booInit ){
									booInit = false;
								
									// apply any size animations
									for( var i=0; i<arrItemMin.length; i++ ){
										var itemId = $( arrItemMin[i] ).attr( 'id' );
									
										$( "#" + itemId + " img" ).animate({
											width: "230px",
											height: "155px"
										}, 500 );
									}
								
									for( var i=0; i<arrItemMax.length; i++ ){
										var itemId = $( arrItemMax[i] ).attr( 'id' );
									
										$( "#" + itemId + " img" ).animate({
											width: "480px",
											height: "320px"
										}, 500	, function() {
											// Animation complete.
										});
									
									}
								}
							}
					
					
						});
					}else{
						$( "#source" ).replaceWith( data.items );
					}
				objDeepLink.strCurrentCat = data.category;
				objDeepLink.setMetaDescription( data.category_description );
			break;
			case 'project':
				if( $( "#source" ).length > 0){ // animated the categoried out
					var IE7 = navigator.appVersion.indexOf("MSIE 8.") != -1;
					
					if( !IE7 ){
						var strFake = "";
						$( "#source" ).quicksand( $(strFake).find('li'), {
							
						}, function() { // callback function
						
						
							$( "#list-container" ).html( data.content );
							Cufon.refresh();
						
								$( "#list-container" ).css( 'opacity', '0' );
						
								$( '#list-container' ).animate({
								    opacity: 1
								  }, 500 );
							
						
						
						});
					}else{
						$( "#list-container" ).html( data.content );
						Cufon.refresh();
					
							$( "#list-container" ).css( 'opacity', '0' );
					
							$( '#list-container' ).animate({
							    opacity: 1
							  }, 500 );
					}
					
				}else{
					if( data.type == "page" ){
						//console.log( 'page' );
						$( "#list-container" ).html( data.content );
						Cufon.refresh();

						$( "#list-container" ).css( 'opacity', '0' );

						$( '#list-container' ).animate({
						    opacity: 1
						  }, 500 );
					}
					else if( data.type == "project" ){
						//console.log( 'here' );
						
						$( "#list-container" ).html( data.content );
						Cufon.refresh();

						$( "#list-container #col02" ).css( 'opacity', '0' );

						$( '#list-container #col02' ).animate({
						    opacity: 1
						  }, 500 );
					
					}
					
				}
				
				objDeepLink.setMetaDescription( data.project_description );
			break;
			case 'page':
				
				if( $( "#source" ).length > 0){ // animated the categoried out
					var strFake = "";
					$( "#source" ).quicksand( $(strFake).find('li'), {
							
					}, function() { // callback function
						
						
						$( "#list-container" ).html( data.content );
						Cufon.refresh();
						
							$( "#list-container" ).css( 'opacity', '0' );
						
							$( '#list-container' ).animate({
							    opacity: 1
							  }, 500 );
							
						
						
					});
					
				}else{
					$( "#list-container" ).html( data.content );
					Cufon.refresh();

					$( "#list-container" ).css( 'opacity', '0' );

					$( '#list-container' ).animate({
						opacity: 1
					}, 500 );
				}
				
				objDeepLink.setMetaDescription( data.page_description );
			break;
			}
			
			
	});
	
}

objDeepLink.preloadPrevImages = function( ){
	var strPageUrl 			= $.address.path();
	var strValidateUrl 		= "/wp-content/themes/designbystudioade/scripts/preloadImages.php";
	var objInputs			= {};
	
	
	objInputs[ "strUrl" ]	= strPageUrl;
	
	$.post( strValidateUrl, objInputs,
		function(data){
			data = $.evalJSON( data );
			var arrItems = data.strImages.split( ',');
			var args_len = arrItems.length;
			for (var i = args_len; i--;) {
				
				if( arrItems[i] != "" ){
					/*var cacheImage = document.createElement('img');
					cacheImage.src = arrItems[i];
					cache.push(cacheImage);*/
				}
			}
			
			return data.strImages;
		}
	);
}

objDeepLink.setMetaDescription = function( strDescription ){
	$('meta[name=description]').attr( 'content', strDescription );
}

objDeepLink.createCatBody = function( ){
	$( "#list-container" ).html( "<ul id='source'></ul>" );
}
