/*
******************************************
NOKIA MUSIC STORE TOPIC AND TIP SWITCHER
SATAMA INTERACTIVE

Creator: Sakari Laaksonen
******************************************

****DESCRIPTION***
Hides all tips and topics in a page, shows a layout and controlling buttons for swiching tips
Open a specific tip by  using a hash id, for example http://mypage.com/index.html#tip1


***LEGEND***
$varName = jQuery object
varname = regular variable

//

*/

//this is the window.onload event
$(function(){
   //try {
    controller = new InitLayout('',0);
    switchVisible();// added 17.-18.4.2008 for flash
    $('#flashContent').click(switchVisible);// added 17.-18.4.2008 for flash
    
  	$('#msh_ts_container').show();
  	
//   }
//   catch(e) {
//     return;
//   }


});


function InitLayout() {
  
  $('#msh_ts_content').hide(); //Hide noscript-content

  //initiation variables
  var $currentTip;
  var $scripted =  $('#msh_ts_scripted'); //reference point to scripted content container
  var $tips = $('.ms_tip_container'); //list of all tips
  var intro = document.getElementById('intro');
  
  //controller object variables
  this.currentTopicId;
  this.currentTipId;
  this.intro = 0;
  this.numOfTips = $tips.length;
  
    (intro != null) && (location.hash == '' || location.hash.match('intro')) ? //if hash is defined , get hash , else show intro
        this.intro = 1 : this.currentTipId = getHash(location.hash) ;
  
  //if nothing is found with hash or the item found is not a tip, take the first tip in tips list
   $currentTip = $(this.currentTipId);
   if($currentTip.length == 0 || !$currentTip.hasClass('ms_tip_container')) { 
      $currentTip = $tips.eq(0);
  }
  
  //put tip text into scripted tip area
  showTip($currentTip.html());
  //do some menu highlighting
  headersAndMenus($currentTip);

  //bind controlling functions to buttons and topic headers
  $('.msh_ts_navi_section ul li a').click(showThisTopic);
  $('.previous').click(prevTip);
  $('.next').click(nextTip);

  //if intro is not defined, hide intro container and show tip and menu  
  if(!this.intro) {
    $('#intro').hide();
	$scripted.show();
  }
}

function headersAndMenus($tip) {

  var $scripted = $('#msh_ts_scripted');
  var $navi = $scripted.find('#msh_ts_scripted_navi');
  //clear headers and navigation lists
  $scripted.find('h2').remove();
  $navi.empty();

  //section header
  $tip.parent().parent().find('h2').clone().addClass('msh_section_header').prependTo($scripted);
  //navi header
  $tip.parent().find('h3').clone().appendTo($navi);

  //build list
  var listHtml = "<ul>\n\r";
  $tip.parent().find('div').each( function(){
  listHtml += '<li><a ' +($tip.attr('id') == $(this).attr('id') ? 'class="ts_active"' : '' )+' href="#'
           +$(this).attr('id')
           +'">'+$(this).find('h4').text()+"</a>\n\r</li>\n\r";
  });
  //closing list markup
  listHtml += "</ul>\n\r";
  //add list
  $navi.append(listHtml).find('a').click(showThisTip);
  //highlight the current topic in topics list
  $('#msh_ts_naviContainer ul a').each( function(){
       getHash($(this).attr('href')) == '#'+$tip.parent().attr('id') ?  $(this).addClass('ts_active') : $(this).removeClass('ts_active');
  });
  
  
}
//attached to dynamic tip list
function showThisTip() {
  showTip($(getHash($(this).attr('href'))).html());
  $('#msh_ts_scripted_navi .ts_active').removeClass();
  $(this).addClass('ts_active');
  //prevent anchor's default action
//store current id's for fast retrieval
  tipId = getHash($(this).attr('href'));
  tipId = tipId.substring(1);
  controller.currentTipId = '#'+tipId;
  location.href = $(this).attr('href');
  return false;
}


function showThisTopic() {
  
  controller.intro = 0;
  $('#intro').hide();
  $('#msh_ts_scripted').show();
  $currentTopic = $(getHash($(this).attr('href')));
  $currentTip = $currentTopic.find('.ms_tip_container:first');
  $('#msh_ts_naviContainer .ts_active').removeClass();
  $(this).addClass('ts_active');
//store current id's for fast retrieval
  controller.currentTipId = '#'+$currentTip.attr('id');
  controller.currentTopicId = '#'+$currentTopic.attr('id');
//show tip and update menus and address bar  
  
  headersAndMenus($currentTip);
  showTip($currentTip.html());
  location.href = controller.currentTipId;// changed 17.-18.4.2008 for flash
  return false;// added 17.-18.4.2008 for flash
}

function getHash(s){
	if(s === undefined) return false;
  if (s.indexOf('#') != -1) {
		return s.substring(s.indexOf('#'));
	}
	else {
		return false;
	}
}

function changeTip(dir) {
  var $newTip;
  var $newTopic;  
  var $tips = $('.ms_tip_container');
  var index;
  //find the index number of current tip
  $tips.each( function(i) {
       if ( ('#'+$(this).attr('id')) == controller.currentTipId ) {
       index = i;
       }
   });
  //alert($tips[index].id);
 
  if(dir === undefined) dir = 1;
   
  switch(dir) {
 
    case 1: //next tip
    //update current tip id
    //indeces run from 0-n so substract 1 from total number of tips
      index < controller.numOfTips-1  ? index++ : index = 1;
    break;
    
    case 0: //previous tip
      //update current tip id
      index <= 1 ? index = controller.numOfTips-1 : index--;
    break;
  }
  controller.currentTipId = '#'+$tips[index].id

  $newTip = $(controller.currentTipId);  
  if( $newTip.parent().attr('id') != controller.currentTopicId ) 
      controller.currentTopicId = '#'+$newTip.parent().attr('id');
  
  location.href = controller.currentTipId;
  //location. = controller.currentTipId;
  showTip($newTip.html());
  headersAndMenus($(controller.currentTipId) );
}

//wrappers for changeTip
function nextTip() {
    changeTip(1);
    return false;
}

function prevTip() {
    changeTip(0);
    return false;
}

function showTip($html) {
  $('#msh_ts_scripted_displayArea').empty().append($html).find('a').each( function() {
      $(this).click(linkFromContent);
  });
}


function linkFromContent() {
  showTip($(get($(this).attr('href'))).html());
  headersAndMenus($(get($(this).attr('href'))));
  tipId = getHash($(this).attr('href'));
  tipId = tipId.substring(1);
  controller.currentTipId = '#'+tipId;
  location.href = $(this).attr('href');
  return false;

}
/*function to show Quickstart-flash*/
function switchVisible(){
  url = document.location.href;
//   var content = document.getElementById('msh_ts_content');
//   var scripted = document.getElementById('msh_ts_scripted');
//   var intro = document.getElementById('intro');
  var content = $('#msh_ts_content');
  var scripted = $('#msh_ts_scripted');
  var intro = $('#intro');
  
  var urLength = url.length;
  var urlEnd = 0;//a completely arbitrary value
  for(i=0;i<urLength;i++){
    if(url.charAt(i)=='#'){
      urlEnd = 1;
      break;
      return urlEnd;
    } 
  }
  if(urlEnd==0){
//     content.hide();
//     scripted.hide();
       content.hide();
        scripted.hide();
        intro.show();
        //intro.style.display='inline';
    
  }else{
    var topic = url.split('#')[1];
    switch(topic){
      case '':
      content.hide();
        scripted.hide();
        intro.show();
//       
//         content.style.display='none';
//         scripted.style.display='none';
//         intro.style.display='inline';
      break;
      case 'tip_0':
      content.hide();
        scripted.hide();
        intro.show();
//         content.style.display='none';
//         scripted.style.display='none';
//         intro.style.display='inline';
      break;
      default:
      
        
        intro.hide();
//         intro.style.display='none';
    }
  }
}
