jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

$(function(){
	
	$(".button-back-to-top").live("click", function(){
		$('html,body').animate({scrollTop: 1}, 1000);
	});
	
	$("#sections-menu li").live('click', function(){
		$("#sections-menu li").removeClass("active");
		$("#sections-menu li .current-section").fadeOut();
		$(this).addClass("active");
		$(this).find(".current-section").fadeIn();
	});	

	$(".portfolio-apps li a").append("<span class='pf-selected-app'></span>");
	$(".portfolio-apps li a").live('click', function(){
		$(".pf-selected-app").fadeOut();
		$(this).find(".pf-selected-app").fadeIn();
	});
	
	
	
	/* ****************** MSB Ajax Processor: DO NOT TOUCH ANYTHING ****************** */

	
	var msbAjax = {};
	
	msbAjax._history = ['home'];
	msbAjax._index = 0;
	msbAjax._next = function() {
		if (msbAjax._history.length - msbAjax._index == 1) return;		
		msbAjax.load(0, msbAjax._history[msbAjax._index]);
		msbAjax._index++;
	}
	msbAjax._prev = function() {
		//console.log('IND: '+msbAjax._index);
		if (msbAjax._index == 0) return; 
		msbAjax._index--;
		//console.log(msbAjax._history+': '+msbAjax._index);
		var page = msbAjax._history[msbAjax._index];
		msbAjax.load(true, page);
		
	}
	msbAjax._addHistory = function(page) {
		msbAjax._index++;
		msbAjax._history.push(page);		
	}
	
	// e: event object, make sure the item id starts with 'msbpage-' followed by the page you want to load via AJAX
	// ajax_path: to load a custom page, set the ajax_path value to the page path, the value of e in this case can be anything, eg: true, 0, 1 anything
	msbAjax.load = function(e, ajax_page) {		
		
		//alert(e.id);
		// Called by the click of a navigation item, not history button
		if (ajax_page == undefined) {
			var tid = e.id;			
			var ta = tid.split('msbpage-');
			ajax_page = ta[1];			
			msbAjax._addHistory(ajax_page);
		}
		
		var la = document.location.href.split('#');
		base_location = la[0];
		
		var ajax_path = base_location +'sourcebits/'+ ajax_page;
		$.get(ajax_path, function(data) {
			data = data.replace('<p>', '');
			$('#wrapper div').remove();
			$('#wrapper').append(data);
			prepareAjaxPage(ajax_page);
			
		});
	
	}
	
	//$('.button-back').live('click', function(e) { msbAjax._prev(); });	
	$('.button-back').live('click', function() {
		history.back(-1);
	});
	
	$('.button-menu').live('click',
		function() {
			if ((this.on == undefined) || (!this.on)) {
				this.on = true;
				//$(".menu-shelf").css({"height" : "81px", "padding" : "10px 0 0", "opacity" : "1"});
				//$(".menu-shelf").css({'height':81, 'padding-top': 10});
				$("#shelf-wrap").addClass("show-menu");
				$(".content-box-hidden, .button-back-to-top").addClass("content-box-up");
				
			}
			else {
				this.on = false;
				//$(".menu-shelf").css({"height" : "0", "padding" : "0", "opacity" : "0"});
				//$(".menu-shelf").css({'height':0, 'padding-top': 0});
				$("#shelf-wrap").removeClass("show-menu");
				$(".content-box-hidden, .button-back-to-top").removeClass("content-box-up");
				
			}
		}
	);
	
	/* ****************** End of MSB Ajax Processor: DO NOT TOUCH ANYTHING ****************** */
	
	// Run necessary scripts after loading ajax result
	// page: name of the page that was loaded
	function prepareAjaxPage(page) {

		$('html, body').animate({scrollTop: 1}, 10);
		
		
		var myLinks = document.getElementsByTagName('a');
		for(var i = 0; i < myLinks.length; i++){
		   myLinks[i].addEventListener('touchstart', function(){$(this).addClass("hover");}, false);
		   myLinks[i].addEventListener('touchend', function(){$(this).removeClass("hover");}, false);
		}
		
		if (page == 'contact') {
			$("#addresss-tabs").tabs();

			//Link "View our location" button to Sourcebits USA Google Map
			$("#tab-address-usa").click(function(){
				$("#link-map").text("View our Atlanta offices").prepend("<span class='arrow-next'></span>").attr("href", "http://maps.google.com/maps?q=sourcebits&cd=4&ei=1bKES_KACc2SkQWL_rm2BQ&sig2=wUATD2LnG9r9wqSht-TA_Q&sll=12.92738,77.598528&sspn=0.151699,0.119795&ie=UTF8&hl=en&view=map&cid=161781127805456243&ved=0CBYQpQY&hq=sourcebits&hnear=&ll=33.781341,-84.273194&spn=0.007473,0.013937&t=h&z=17&iwloc=A");
				$("#phone-no").attr("href", "tel:+18666631705");
			});

			//Link "View our location" button to Sourcebits India Google Map
			$("#tab-address-india").click(function(){
				$("#link-map").text("View our Bangalore offices").prepend("<span class='arrow-next'></span>").attr("href", "http://maps.google.com/maps?q=Sourcebits&cd=3&ei=YTKCS82pH5n-jQPnv-mDCg&sig2=GbgnHd-1WYzth5361_NyzA&sll=12.92738,77.598528&sspn=0.151699,0.119795&ie=UTF8&hl=en&view=map&cid=15327850067666241866&ved=0CBYQpQY&hq=sourcebits&hnear=&ll=12.851898,77.658427&spn=0.003781,0.006968&t=h&z=18&iwloc=A");
				$("#phone-no").attr("href", "tel:+918040650802");
			});			
		}
		
		
		if (page != 'home')	{
			//alert(434);
			setTimeout(scrollTo, 0, 0, 1);
		}
		
		$(".button-quote, .button-next-step").prepend("<span class='arrow-next'></span>");
		
		if ($("#sections-menu").parent().hasClass("menu-shelf") == false) {
			$("#sections-menu li").append("<span class='current-section'></span>");
		}
		
		if (history.length > 1) $('.button-back').css('display', 'block');
		
	}
	
	
	
	// Start adding Ajax events from here
	
	$('#msbpage-about').live('click', function () { msbAjax.load(this); }); 
	
	$('#msbpage-app, #msbpage-contact, #msbpage-home, #msbpage-portfolio-design, #msbpage-portfolio-iphone, #msbpage-portfolio-mac, #msbpage-portfolio-mobile, #msbpage-portfolio-web, #msbpage-portfolio, #msbpage-services-design, #msbpage-services-iphone, #msbpage-services-mac, #msbpage-services-mobile, #msbpage-services-web, #msbpage-services').live('click', function(e) { msbAjax.load(this); });
	



	// Listen to hashchange events
	// Requires Ben Alman's jQuery hashchange plugin
	$(window).bind('hashchange', function() {
		var h = location.hash;
		hash = h.replace('#', '');
		if (hash == '') hash = 'home';
		msbAjax.load(1, hash);		
	});
	$(window).trigger('hashchange');
	
	// Load the homepage of the #hash page
	var la = document.location.href.split('#');
	if (la.length < 2) msbAjax.load(1, 'home');
	else msbAjax.load(1, la[1]);
	

	prepareAjaxPage();
	
});

