function lastElementRowList(selectorList, addClassName){
	$(selectorList).each(function(){
		$(this).find('ul li').last().addClass(addClassName);
	});
}

String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};

function loadContent(hash) {
	var home=0;
	var new_loc=hash.replace(/#/g,'/');
	new_loc=new_loc.replace(/%23/g,'/');
	
	var pattern=/div[1-9]{1}/;
	
	if(hash.match(pattern)==null){
		if(hash!=''){
			if(hash!='home'){
				var page = '/wp-content/themes/somatone/ctOnly2.php?pageurl='+base_url+'/'+new_loc+'&random_number='+Math.random()*999999;
			}else{
				var page = '/wp-content/themes/somatone/ctOnly.php?random_number='+Math.random()*999999;
				home=1;
			}
		}else{
			var page = '/wp-content/themes/somatone/ctOnly.php?random_number='+Math.random()*999999;
			home=1;
		}
		
		if(count>1){
			$.ajax({
			  url: page,
			  cache:false,
			  success: function(data) {
				$('#page').html(data);
				rewriteLinks();
				applyCssFixes();
				if(home==0){
					document.title = $('#pageCategory').text();
				}else if(home==1){
					document.title="SomaTone Interactive";
				}			
			  }
			});
			/*
			$("#page").load(page, function(){
				rewriteLinks();
				applyCssFixes();
				if(home==0){
					document.title = $('#pageCategory').text();
				}else if(home==1){
					document.title="Somatone Interactive";
				}			
			});
			*/
		}
	}
	count++;
}


	
 
function rewriteLinks() {
    // rewrite all the href attributes
	if(parent.location.hash==""){
		//window.location.hash="home";
		document.title = 'SomaTone Interactive';
	}
	
	$('#page a').each(function(){
		var this_url=$(this).attr('href');
		
		if(this_url.search(/somatone.com/i)<0 && this_url.search('#')<0){
			$(this).attr("target","_blank");
		}
		
        var arr = $(this).attr('href').split('/');
        var page = arr.pop();
        while (page == '' && arr.length > 0) {
            page = arr.pop();
        }
        if ($(this).attr('rel').indexOf('#') == -1 && !$(this).parent().hasClass('logo') && $(this).attr('target') != '_blank' && this_url.search('#')<0) {
            $(this).attr('rel','##' + page);
            $(this).attr('onClick','return false;'); 
        }
		
		
		
	});
	
	$('.logo a').click(function(event){
			event.preventDefault();
            var page1 = '/wp-content/themes/somatone/ctOnly.php?random_number='+Math.random()*999999;
			window.location.hash="home";
			document.title = 'SomaTone Interactive';
			$.ajax({
			  url: page1,
			  cache:false,
			  success: function(data) {
				$('#page').html(data);
				rewriteLinks();
                applyCssFixes();
			  }
			});
			/*
            $("#page").load(page1, function(){
                rewriteLinks();
                applyCssFixes();
            });
			*/
			return false;
    });
	
    $('#page a').click(function(event){
		
        if ($(this).attr('rel').indexOf('##') != -1) {
			var url_path=$(this).attr('href')
			url_path=url_path.split('/');
			var test='';
			for(i=3;i<url_path.length-1;i++){
				if(url_path[i]!=''){
					test+="#"+url_path[i];
				}
			}
			//window.location.hash=test;
            $.history.load(test);
			
           
        }
    });
}

function applyCssFixes() {
    $('#navigation li').last().prev().prev().prev().addClass('separatorItem');
	$('.homeNews .homeNewsBlk').last().css('marginRight', '0px');
	$('.newsHolder .newsPost').last().css('borderBottom', 'none');
	$('.aboutHolder p').last().addClass('aboutLastP');
	$('.clients li').not('.clients li ul li').addClass('rowClients');
	$('.rowClients').last().addClass('lastRow');
	$('.listProjects li').not('.listProjects li ul li').addClass('row-projects');
	$('.row-projects').last().addClass('lastRowProject');
	$('.testimonials li').first().addClass('fistTestimonial');
	$('.testimonials li').last().addClass('lastTestimonial');
	$('.realList li').not('.realList li ul li').addClass('realRow');
	$('.realRow').last().addClass('realLastRow');	
	lastElementRowList('.realList li', 'lastElementReal');
	lastElementRowList('.clients li', 'lastElement');
	lastElementRowList('.top-box ul li', 'clientRow');
	lastElementRowList('.listProjects li', 'lastElementProject');
	$('.projectContent ul li ul').addClass('rowElement');
	
	$('.rowElement').each(function(){
		$(this).find('li').last().addClass('rowElementLastInRow');
	});
	
	$('.styled-select').selectbox();
	$('#topNavigation .menu-item:first a').addClass('first-link');
	$('#topNavigation .menu-item:last a').addClass('last-link');
	if($('#page').hasClass('contactPage')){
		$('#footer').addClass('footer-contact');
	}
	
	else if($('#page').hasClass('aboutPage')){
		$('#footer').addClass('footer-about');
	}
	
	$('.rowElement li').each(function(){
    	$(this).find('p').first().addClass('gameTile');
	});
}

function custom_redirect(){
	var url_path=location.href.split('/');
	url_path=url_path[3];
	var page_path=location.href.replace(/#/gi,'/');
	page_path=page_path.replace(/%23/,'/');
	
	if(url_path!='#home'){
		var content_url='/wp-content/themes/somatone/ctOnly2.php?pageurl='+page_path+'&random_number='+Math.random()*999999;
	}else{
		var content_url='/wp-content/themes/somatone/ctOnly.php?random_number='+Math.random()*999999;
	}
	
	if(parent.location.hash!=""){
		$('#page').empty();
		$.ajax({
			  url: content_url,
			  cache:false,
			  success: function(data) {
					$('#page').html(data);
					applyCssFixes();
					rewriteLinks();	
					if(url_path!='#home'){
						document.title = $('#pageCategory').text();
					}else{
						document.title = 'SomaTone Interactive';
					}
			  }
		});
		/*
		$('#page').load(content_url, function() {
			applyCssFixes();
			rewriteLinks();	
			if(url_path!='#home'){
				document.title = $('#pageCategory').text();
			}else{
				document.title = 'Somatone Interactive';
			}
		});
		*/
	}else{
		var n_url=location.href.split('/');
		if(url_path!=''){
			var n_url1='';
			for(i=3;i<n_url.length-1;i++){
				n_url1+="#"+n_url[i];
			}
			window.location=n_url[0]+'/'+n_url[1]+'/'+n_url[2]+'/'+n_url1;
		}
	}
}



$(document).ready(function(){
	count=1;
	base_url=$('.logo').children('a').attr('href');
    $.history.init(loadContent);
	custom_redirect();
    applyCssFixes();
    rewriteLinks();
		
		var userAgent = ['Mobile', 'Android', 'webOS', 'iPod', 'iPhone'];
		var isMobile = false;			
		for( i in userAgent ){
			isMobile = isMobile || ( navigator.userAgent.indexOf(userAgent[i]) != -1 );
		}
	
		if (isMobile) {
		 $('.player-container').css('position', 'relative');
		 $('.jp-playlist-player').css('position', 'relative');
		 $('.jp-playlist-player').css('top', '15px');
		 $('#footer').css('background', '#000');
		 $('body').css('background', '#000');
		}
		
});

/*$(window).load(function(){
	$('.projectContent ul').each(function(){
    	$(this).find('li').last().addClass('listProjectsLast');
	});
});*/
