var mhref = "";
var prevhref = "";
var start = true;

$(function(){
  if(ieversion>=7){
  resizePlayer();
    $('#main_menu ul li a').each(function(index) {
      $(this).click( function() { change(this); return false } );
    });

    var hashvar;

    if(location.hash) hashvar = location.hash;
  }
  else {
    $('#flash_bg').detach();
  }
})

function change(alink){
      if($('#main_menu ul li.active:first').size() || (start = $('#view').hasClass("start"))){
	$(alink).parent().addClass("next");
	if(start){
	  $('#view').removeClass("start");
	  $("#view div#enclose").slideUp(0);
	}
	$('#main_menu ul li.active').removeClass("active");

	if(!start){
	$('#submenu a').unbind("click");
	$('#submenu a').attr("href", "#");
	$('#content a').unbind("click");
	$('#content a').attr("href", "#");
	if($('#view #enclose').css('display') != 'none') $("#show_hide").click();
	}
	start = false;
	noback = false;
	

	prevhref = mhref;
	if(mhref = $(alink).attr("name"));
	else mhref = $(alink).attr("href");
	$(alink).attr("name", "");
	$("#content").ajaxError(function(event, request, settings){
	  alert("Error requesting page " + settings.url);
	  $('#main_menu ul li').toggleClass("active");
	});
	$.post('/getview',{ href : mhref }, function(data) {
	  if(data){
		  $('#main_menu ul li.next').addClass("active");
		  $('#main_menu ul li.next').removeClass("next");

		  $('#view #enclose').html($('div#enclose', data).html());
		  
		  if($('#flash_bg').css('display') != 'none' && $('div#flash param', data).val() != $('#flash_bg param').val()){
		    var flash_html = $('div#flash', data).html();

		    $('#flash_bg').hide();
		    $('#flash_bg').html(flash_html);
		    if(mute){
		     $('#flash_bg object').prepend('<param name="flashvars" value="volume=0"></param>');
		     $('#flash_bg embed').attr('flashvars', 'volume=0');
		    }
		    $('#flash_bg').show();
		    resizePlayer();
		  }
		  data = "";

		  $('#submenu a').each(function(index) {
		    $(this).click(
		     function(obj) {
		      $('#main_menu ul li.active a').attr("name", $(this).attr("href"));
		      $('#main_menu ul li.active a').click(); return false;
		     }
		    )
		  });

		  $('#content div.gal a.link').each(function(index) {
		    $(this).click(
		     function(obj) {
		      $('#main_menu ul li.active a').attr("name", $(this).attr("href"));
		      $('#main_menu ul li.active a').click(); return false;
		     }
		    )
		  });

		  /*if(prevhref){
		    $('#content').prepend("<a id='back' title='Atpakaļ/Back' href='"+prevhref+"'>&lt;&lt;&lt;</a><br /><br />");
		    $('#content a#back').each(function(index) {
		      noback = true;
		      $(this).click(
		      function() {
			$('#main_menu ul li.active a').attr("name", $(this).attr("href"));
			$('#main_menu ul li.active a').click(); return false;
		      });
		    });
		  }*/

		  $('a.content_image').lightBox({
		      imageLoading: '/wcms2GalleryModulePlugin/images/lightbox/lightbox-ico-loading.gif',
		      imageBtnClose: '/wcms2GalleryModulePlugin/images/lightbox/lightbox-btn-close.gif',
		      imageBtnPrev: '/wcms2GalleryModulePlugin/images/lightbox/lightbox-btn-prev.gif',
		      imageBtnNext: '/wcms2GalleryModulePlugin/images/lightbox/lightbox-btn-next.gif'
		  });

		  $("#show_hide").click()
		  .queue(function(){ scrollwidth = scrollbarWidth() });
	  }
	  else {
	    $('#main_menu ul li.next').addClass("active");
	    $('#main_menu ul li.next').removeClass("next");
	    $("#view div#content").show();
	    alert("Error requesting page " + settings.url);
	    $('#content').html("Failed")
	  };
	});
       }
       location.assign("#"+$(alink).attr("href"));
}

  var cache = {
    // If url is '' (no fragment), display this div's content.
    '': true
  };
  
  // Bind an event to window.onhashchange that, when the history state changes,
  // gets the url from the hash and displays either our cached content or fetches
  // new content to be displayed.
  $(window).bind( 'hashchange', function(e) {
    var url = e.fragment;
    if ( cache[ url ] ) {
      var alink = $('#main_menu ul li a[href="'+url+'"]').get(0);
      if(alink)
        change(alink);
    } else {
      cache[ url ] = true
    }
  });
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  //$(window).trigger( 'hashchange' );

