
/*!
* Swedish Wood Header Script v1.0
*
* Author: Celebration
* http://www.celebrationstudios.se
* Copyright 2011, Celebration 
*
* Includes swhs-XML.xml
* Includes swhs-Style.css
* Includes swhs-Style-ielte8.css
*
*/


$(document).ready(function () {


    //Add div#swhs_wrapper directlyt after the body
    $('body').prepend('<div id="swhs_wrapper"></div>');

    //Add ul#swhs_ul to div#swhs_wrapper
    $('#swhs_wrapper').append('<ul id="swhs_ul"></<ul>');

    //Get the domain name
    var domain = 'http://' + document.domain;

    // Get the text from the XML-file and add it to betweens li
        var xml = '<?xml version="1.0" encoding="iso-8859-1"?><sites><site id="0"><title>Inom Svenskt Trä ingår:</title><url>http://www.svenskttra.se</url></site><site id="1"><title>Information om trä</title><url>http://www.svenskttra.se/web/Om_tra_1.aspx</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/Information-om-Tra.png</imgsrc><text>Läs om träets egenskaper, miljöfördelar, olika träprodukter och träbehandlingar.</text></site><site id="2"><title>Information om limträ</title><url>http://www.svensktlimtra.se</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/Information-om-Limtra.png</imgsrc><text>Teknisk information om limträ, som t.ex. produktsortiment och konstruktionsexempel.</text></site><site id="3"><title>Byggbeskrivningar</title><url>http://www.byggbeskrivningar.se</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/Byggbeskrivningar.png</imgsrc><text>Byggbeskrivningar för att snickra i hemmet, t.ex. en bastu, en altan eller en friggebod.</text></site><site id="4"><title>Snickra med barn</title><url>http://www.fixatrixa.se</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/Snickra-med-barn.png</imgsrc><text>Låt dig inspireras till att snickra leksaker som t.ex. dockhus, riddarutrustningar eller lådbilar.</text></site><site id="5"><title>Träbyggnadskansliet</title><url>http://www.trabyggnadskansliet.se/</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/Trabyggnadskansliet.png</imgsrc><text>Verkar för projekt inom storskaligt träbyggande, som flervåningshus, offentliga  byggnader och broar.</text></site><site id="6"><title>TräGuiden</title><url>http://www.traguiden.se/</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/TraGuiden.png</imgsrc><text>Sök bland 3 000 sidor med teknisk information om trä- och träbyggande.</text></site><site id="7"><title>Tidningen Trä</title><url>http://www.tidningentra.se</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/Tidningen-Tra.png</imgsrc><text>Läs tidningen Trä, som bevakar träarkitektur i Sverige och världen. Prenumerera gratis.</text></site><site id="8"><title>Träpriset</title><url>http://www.trapriset.se</url><imgsrc>http://www.svenskttra.se/scripts/ST_swhs/images/Trapriset.png</imgsrc><text>Träpriset delas ut vart fjärde år till en modern svensk träbyggnad. Se alla nominerade byggnaderna.</text></site></sites>';
        xmlDoc = $.parseXML(xml);
            $(xmlDoc).find('site').each(function () {
                var id = $(this).attr('id');
                var title = $(this).find('title').text();
                var url = $(this).find('url').text();
                var imgsrc = $(this).find('imgsrc').text();
                var imgalt = $(this).find('imgalt').text();
                var text = $(this).find('text').text();

                $('<li class="swhs_item" id="swhs_' + id + '"></li>').html('<a id="swhs_link_' + id + '" href="' + url + '">' + title + '</a><div class="swhs_hover"><div class="swhs_img"><img src="' + imgsrc + '" alt="' + imgalt + '" /></div><p class="swhs_p">' + text + '</p></div>').appendTo('#swhs_wrapper ul#swhs_ul');

            }); 
            
            //Add class="current" to li when li is current
            $('div#swhs_wrapper').children('ul').find("li").each(function (i) {

                //Get the link of the hyperlink
                var XMLUrl = ($(this).children('a').attr('href'));

                //Get the ID
                var aid = $(this).attr('id');

                //If the domain is equal to the hyperlink then the class current is added to the link
                if ((domain == XMLUrl) && (aid != 'swhs_0')) {
                    $(this).addClass('current');
                    $(this).children('a').css('color','#4A4A44');
                }
            });

            //Check if the domain is www.svenskttra.se then the logo is hidden and the text shows up. 
            if (domain == 'http://www.svenskttra.se') {
                $('div#swhs_wrapper ul li#swhs_0 a').css('text-indent', '0');
                $('div#swhs_wrapper ul li#swhs_0 a').css('background', 'none');
                $('div#swhs_wrapper ul li#swhs_0 a').css('color', '#4a4a44');
            }
                 
            //Fade in div hover on mouseenter
            $('div#swhs_wrapper ul li a').mouseenter(function () {
                //Check the id for every link
                var aid = $(this).attr('id');

                //Take the width of the link a
                var width = $(this).width();

                //Split the sum in 2 
                var halfwidth = (width / 2);

                //take off the decimals
                var halfwidthNoDecimal = Math.floor(halfwidth);

                //Count the margin change (135 is the half width of div.swhs_hover )
                var margin = 135 - halfwidthNoDecimal;

                //Gets the result and put minus
                var result = -margin

                // if the links id is not equal to the first link in the li then the box fade in 
                if (aid != 'swhs_link_0') {
                    $('div#swhs_wrapper ul li div.swhs_hover').hide();
                    $(this).parent('li').children('div.swhs_hover').show();
                    $(this).parent('li').children('div.swhs_hover').css('margin-left', result);
                }

            });
                
            //Fade out div hover on mouseleave
            $('div#swhs_wrapper ul li a').mouseleave(function () {
                $('div#swhs_wrapper ul li div.swhs_hover').hide();
            });   
});
