//On load page, init the timer which check if the there are anchor changes each 300 ms

$().ready(
    function(){  
    setInterval("checkAnchor()", 300);
    }
);
  
var currentAnchor = null;
var oldsect = "";  
//Function which chek if there are anchor changes, if there are, sends the ajax petition
  
function checkAnchor(){
  
  //Check if it has changes
 
    if(currentAnchor != document.location.hash){
  
       currentAnchor = document.location.hash;
  
       //if there is not anchor, the loads the default section

      if(!currentAnchor){
  
        var section = "home";
        query = "lang=SV&section=home";
      }
       else  
       {
  
           //Creates the  string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
  
           var splits = currentAnchor.substring(1).split('&');
           
           var section = splits[0];
  
           delete splits[0];
  
           //Create the params string
  
           var params = splits.join('&');
           //alert(params);
           var query = "lang=SV&section=" + section + params;
           
       }
  
        //Update top-menu styling
        $(".menu_active").removeClass("menu_active").addClass("menu_item");
        $("#"+section+"_link").removeClass("menu_item").removeClass("menu_hover").addClass("menu_active");
        $(".menu_item").hover(function(){ 
            $(this).removeClass("menu_item").addClass("menu_hover");
        },function(){
            $(this).removeClass("menu_hover").addClass("menu_item");
        }    
        );
		
		//Removes hover beaviour from active link
        $("#"+section+"_link").unbind();
        
        //Get content
        $.get("callbacks.php",query, function(data){

        //Get submenu if section has changed, prevents uneccesary loading of submenu
        //alert(section + ' och ' + oldsect);
        if(section != oldsect)
        $.get("pages/menus.php","lang=SV&section=" + section, function(mdata){
                        $("#submenu").html(mdata);                               
                        

						//fix för att gömma första li elementet i submenyn. Endast för plugnplay.
						if(section in { 'products':'','naring':'','fukt':'','kropp':'','rengoring':''}){
						$("#accordion > ul > li:first-child").hide();
						}
						$("#accordion > li").mouseover(function() {
						  // Visa submeny och göm övriga
						  $(this).next("ul").slideDown(300).siblings("ul").slideUp(300);
						  // Hämta href attribut från första child anchorn
						  fix_link = $("a:first-child", $(this).next("ul")).attr("href");
						  // och wrappa runt aktiva kategorins namn som en anchor
						  $(this).wrapInner("<a href=\""+ fix_link +"\"></a>");
						});


                        /*$("#accordion > li").mouseover(function(){

                            if(false == $(this).next().is(':visible')) {
                                $('#accordion > ul').slideUp(300);
                            }
                            $(this).next().slideToggle(300);
                            
                        });*/
                        //$('#accordion > ul:eq(0)').show(); 

        }); 
        
        //If IE then skip the fancy stuff
        if($.browser.msie) 
                   $("#content").html(data); 
        else {
        //If navigating from top menu fade "whole" page, else just fade content area
            if(params == "")
                $("#tofade").fadeTo(500, 0, function(){

                   $("#content").html(data);
                 
                   $("#tofade").fadeTo(800,1);
                   $("#content_area").fadeTo(200,0.85);

                });
            else
                $("#content_area").fadeTo(500, 0, function(){

                   $("#content").html(data);
                   
                   $("#content_area").fadeTo(200,0.85);

                }
            
            );
        }
        
        //Update header
        switch(section)
        {
                case 'news':
                $('#header').html('Nyheter');
                break;
                case 'products':
                $('#header').html('Produkter');
                break;
                case 'ingredients':
                $('#header').html('Ingredienser');
                break;
                case 'contact':
                $('#header').html('Kontakt');
                break;
                case 'about':
                $('#header').html('Om oss');
                break;
                case 'retailers':
                $('#header').html('&Aring;terf&ouml;rs&auml;ljare');
                break;
                case 'sale':
                $('#header').html('Best&auml;llning');
                break;
                case 'animal_rights':
                $('#header').html('Djurens r&auml;tt');
                break;
                default:
                $('#header').html('Rosenserien');
        }
        //save section for submenuloader
        oldsect = section; 
       });
  
   }
  
 }  

