$(document).ready(function(){
  var page = window.location.href;
  var selected = $('#tabs').tabs( "option", "selected" );
  var dontload = false;
  var loadcntrl = true;
  var updatelink = false;
  var hash = window.location.hash;
  var previoushash = window.location.hash; 
  var page2 = page.split('/');
  previoushash = previoushash.split('/');
  previoushash = previoushash[0];

  if (page2[3] == 'pubaccess'){
    if ( (hash=='' || hash==undefined) && (pub!=undefined || pub!='') ){ //reload to home tab if no tab is specified.
      window.location.hash = '#home';
    } 
  }

  $('div[id*="show_"]').live('click',function(){	    
    var id = $(this).attr('id');
    id = id.replace('show_','');
    window.location.hash = id;
  });

  $('#thetabs a').click(function(){ //tab is selected, go to that tab & set hash
	loadcntrl = false;
	var panel = this;
	var hashtag = panel.hash;
        var hashtag2 = hashtag.substr(1);
	window.location.hash = hashtag2;
	return false;
  });
   
  $('.archivetab').live('click',function(){ //links on 'home' panel for recent articles/updates  -- load them into archive panel and switch to that tab
     loadcntrl = false;           
     var href = $(this).attr('href');
     href = href.substr(1);
     $('#tabs').tabs('select','#archives');
     window.location.hash = href;
     return false;
  });


  $(window).hashchange( function(){
     var currenthash = window.location.hash; //grabs current hash value
     currenthash = currenthash.split('/'); 
     currenthash = currenthash[0]; //grabs the 'parent' hash (ie. #board from #board/otc/133)
     
     if(!loadcntrl){
       checkHash();
     }

     if (previoushash != currenthash){ //fix for backbutton reloading the parent page        
        updatelink = true;
     } else {
        updatelink = false; //do not load the parent tab
     }

     previoushash = currenthash; //set the previous hash value
  });
 
  $('#tabs').tabs({
    show: function(event, ui){  // only keep links inside tab divs named below
      $('#home a,#marketsense a,#videos a,#gettingstarted a,#board a,#archives a',ui.panel).live("click",function(event) {        
        var link = $(this).attr('href').replace('https://www.angelpub.com').replace('http://www.angelpub.com'); //grab url from <a> tag
        var hashes = link.split('/'); //make array of values from `link`
	loadcntrl = false;

	if ( $(this).closest('ul').attr('id') == 'updates' || $(this).closest('div').attr('id') == 'home' ){ //click an article link on the 'home' tab - redirect to the 'archives' page without loading the archives page first. (this is a fix)
          updatelink = true;
	} else {
	  updatelink = false;
	}

        if ( hashes[1]== 'pubaccess'){
	  var newhash = hashes[3];
        } else if ( hashes[1] == 'board' && isNaN(hashes[3]) && !isNaN(hashes[4]) ){ //its a message board
          var newhash = '#board/'+hashes[2]+'/'+hashes[3]+'/'+hashes[4];        
        } else if ( hashes[1] == 'board' && hashes[3]!=undefined ){
	  var newhash = '#board/'+hashes[2]+'/'+hashes[3];
        } else if ( hashes[3]==undefined ){
          var newhash = '#'+hashes[1];
        } else {	 
          var newhash = '#'+hashes[1]+'/'+hashes[2]+'/'+hashes[3];
        }	

        if ( $(this).attr('target') == '' && $(this).attr('class') != 'archivelink'){
	 window.location.hash = newhash;
	 return false;
       	}
      });
    },
    select: function(event,ui){		
	if (!loadcntrl && !updatelink){
            window.location.hash = ui.tab.hash;
        }
        loadcntrl = false;
    }
  });
  if (pub){  checkHash(); }
});

function checkHash(){	//check the hash of the url and load accordingly  
  loadcntrl = false;
  var hash1 = window.location.hash;
  var hashes = hash1.split('/'); 
  if ( hashes[2]!=undefined || hashes[3]!=undefined){	
    if ( hashes[0] == '#update'){ //archives page      
      loadHash('#update',hashes[1],hashes[2]);
    } else if ( hashes[0] == '#archives' ){ //archives pageinate
      loadHash('#archives',hashes[1],hashes[2]);
    } else if ( hashes[0] == '#board' && hashes[3]!=undefined){	
      loadHash('#board',hashes[1],hashes[2],hashes[3]);	
    } else if ( hashes[0] == '#board' ){
      loadHash('#board',hashes[1],hashes[2]);
    } else {
      loadHash(hashes[0],hashes[1],hashes[2]);
    }
  } else { //a tab selected    
    var place = window.location.hash;
    var pages = new Array();
    pages = place.split('/');
    if ( pages[0] == '#buyingstocks' ){
      $('#tabs').tabs('select','#gettingstarted');
      $('#gettingstarted').html('<img src="/images/loading.gif" />').load('/buyingstocks/'+pub);
    } else if ( pages[0] == '#faq' ){
      $('#tabs').tabs('select','#gettingstarted');
      $('#gettingstarted').html('<img src="/images/loading.gif" />').load('/faq/'+pub);
    } else if ( pages[0] == '#newsletters'){
      $('#tabs').tabs('select','#newsletters');        
      $('#newsletters').html('<img src="/images/loading.gif" />').load('/newsletters/'+pub);
    } else if ( pages[0]!=undefined){
      $('#tabs').tabs('select',pages[0]);	
      if ( hashes[1]==undefined){
        var page = pages[0].replace(/^.*#/,'');	 
	$(pages[0]).html('<img src="/images/loading.gif">').load('/'+page+'/'+pub);
      } else if ( hashes[1] != pub ){
        $(pages[0]).html('<img src="/images/loading.gif">').load('/'+hashes[1]+'/'+pub);
      } else {
	hashes[0] = hashes[0].replace(/^.*#/,'');
        $(pages[0]).html('<img src="/images/loading.gif">').load('/'+hashes[0]+'/'+pub); 
      }
    }
  }
  return false;
}

function loadHash(tab,pub,item,item2){  //loades the hash into proper panel & content
  loadcntrl = false;
  var nohash = tab.replace(/^.*#/,''); //remove the #    
  if (nohash == 'update'){ //articles post         
    $('#tabs').tabs('select','#archives');
    $('#archives').html('<img src="/images/loading.gif">').load('/update/'+pub+'/'+item);  
  } else if (nohash == 'archives'){ //archives pages
    $('#tabs').tabs('select','#archives');
    $('#archives').html('<img src="/images/loading.gif">').load('/archives/'+pub+'/'+item);
  } else if (nohash == 'board' && item2!=undefined ){ //message board view post
    $('#tabs').tabs('select','#board');
    $('#board').html('<img src="/images/loading.gif">').load('/board/'+pub+'/'+item+'/'+item2);
  } else if (nohash == 'board' && item!=undefined && item2==undefined){ //make a post and shit
    $('#tabs').tabs('select','#board');
    $('#board').html('<img src="/images/loading.gif">').load('/board/'+pub+'/'+item);
  } else if (nohash == 'buyingstocks'){          
    $('#tabs').tabs('select','#gettingstarted');
    $('#gettingstarted').html('<img src="/images/loading.gif">').load('/buyingstocks/'+pub);
  } else if (nohash == 'faq'){	
    $('#tabs').tabs('select','#gettingstarted');
    $('#gettingstarted').html('<img src="/images/loading.gif">').load('/faq/'+pub);
  } else { //everything else
    $('#tabs').tabs('select',tab);
    $(tab).html('<img src="/images/loading.gif">').load('/'+nohash+'/'+pub+'/'+item);
  }
  return false;
}
