/**********************************************
 Custom JavaScript
***********************************************
 Site:    		PrashantKatwa.com		
 Author:        Prashant Katwa
 Created:       12/18/2010
 
*********** CHANGE LOG ************************
Last Edited: 12/18/2010 by Prashant Katwa

**********************************************/


/************* global definitions ************/
var _protocol = window.location.protocol;
var _currentHref = window.location.href;


/************* run page load functions ************/
$(document).ready(function(){
  siteGlobal();
  pageFunctions();
}); 


//$('head').append('<style type="text/css"> section > .gutter { display: none; } </style>');

/********** define plugins and functions **********/
function siteGlobal()
{

	// replace copyright with current year
	function currentYear()
	{
		$(".currentYear").text((new Date).getFullYear());
	};
	
 
	function fixIE()
	{
		if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
	
		}
	}
	
	function dynamicLoad() {
	    
	    
	    var base             = 'http://prashantkatwa.com',
	        $mainContent     = $("#contentPrimary").parent();
	            
	        $('.mainNav a').live('click', function(e) { 
	        
	            $el = $(this);
	    
	            var path = $(this).attr('href').replace(base, '');
	            
	            $("li.currentPage").removeClass("currentPage");
	            $el.parent().addClass("currentPage");
	            
	            $.address.value(path);
	                                
	            e.preventDefault();
	            
	        });   
	    
	    	$mainContent.hide();
	    	$mainContent.parent().prepend('<div class="loading"><p><img src="images/ajax-loader.gif" alt="Loading..." /></p></div>');
	
	        $.address.change(function(event) {  
   	            var current = location.protocol + '//' + location.hostname + location.pathname;

	            if (event.value) {
	              console.log(event.value);
	              if (event.value != '/'){
	              //if ((base + '/' != location.href)){
	              console.log(location)
	                $mainContent.fadeOut('fast', function() {
	                    $mainContent.parent().children('div.loading').fadeIn('fast');  
	                    $mainContent.children('div.error').remove();                  
	                    $(this).load(base + event.value + ' #contentPrimary', '', function(respText, textStatus, ajax) {
	                    	if( ajax.status<200 || ajax.status>304 ) {
	                    	//alert(ajax.status);
							    var errorMsg = $( '<div class="error">'+
							                        '<h2>Error '+ (ajax.status) +' - '+ (ajax.statusText) +'</h2>'+
							                        '<div>'+
							                          'An error occurred whilst trying to access the page.<br>'+
							                          'Return to <a href="/">Home</a> or '+
							                          '<a href="#" onclick="history.go(-1);">Go back</a>'+
							                        '</div>' +
							                      '</div>' );
							    $('#contentPrimary').before($(errorMsg)).hide();
							    $mainContent.children('div.error').fadeIn();
							}
							/*switch (XMLHttpRequest.status) {
							  case 200: break;
							  case 404:
							    $(mainContent).html('<p>Looks like the results have not been uploaded to the server yet. Please check back later.</p>');
							    break;
							  default:
							    $(mainContent).html('<p>' + XMLHttpRequest.status + ': ' + XMLHttpRequest.statusText + '. Please contact the club and let them know.</p>');
							    break;
						   }*/
	                    	$mainContent.parent().children('div.loading').fadeOut('fast',
	                    		function() { $mainContent.fadeIn(); }
	                    	);
	                    	pageFunctions();
	                   	    //var pageTitle = $("head", respText).html();
							var pageTitle =	(/<title>(.*?)<\/title>/m).exec(respText)[1];
						    //alert(pageTitle);
	                        document.title = pageTitle;
	                    }); 
	            	});
	              } else { $mainContent.show() }
	            } 
	    
	            console.log(current)
	            console.log(base+'/')
	            if (base + '/' != current) {
	                var diff = current.replace(base, '');
	                location = base + '/#' + diff;
	            }
	        }); 
	        
	        
   	       	$mainContent.parent().children('div.loading').hide();
			
	    
	
	};

	// Back to top Animation
	$('.backtotop').click(function(){
      $('html, body').animate({scrollTop: '0px'}, 300);
       return false;
	});



 	// execute functions - remove or comment out unused functions
	currentYear();
	fixIE();
	dynamicLoad();

};



// page specific functions
function pageFunctions()
{
	/*** Portfolio Items Hover functions ***/
	$("ul#portfolio-list li").hover(function() { //On hover...
		var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'
		//Set a background image(thumbOver) on the &lt;a&gt; tag 
		$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
		//Fade the image to 0 
		$(this).find("span").stop().fadeTo('normal', 0 , function() {
			$(this).hide() //Hide the image after fade
		}); 
	} , function() { //on hover out...
		//Fade the image to 1 
		$(this).find("span").stop().fadeTo('normal', 1).show();
	});


	/*** Portfolio Filter functions ***/
	$('ul#portfolio-filter a').click(function() {
		$(this).css('outline','none');
		$('ul#portfolio-filter .current').removeClass('current');
		$(this).parent().addClass('current');
		
		var filterVal = $(this).text().toLowerCase().replace(' ','-');
				
		if(filterVal == 'all') {
			$('ul#portfolio-list li.hidden').fadeIn('slow').removeClass('hidden');
		} else {
			
			$('ul#portfolio-list li').each(function() {
				if(!$(this).hasClass(filterVal)) {
					$(this).animate({opacity:'hide',width:'hide',height:'hide'},300)/*.fadeOut('normal')*/.addClass('hidden');
				} else {
					$(this).animate({opacity:'show',width:'show',height:'show'},300)/*.fadeIn('slow')*/.removeClass('hidden');
				}
			});
		}
		return false;
	});

	//$(function() {
		$("#portfolio-list a[rel]").overlay({
			effect: 'default',
			fixed: false,
			closeOnClick: true,
			mask: {color: '#000', opacity: 0.3 }
		});
	//});

};
		

