$(document).ready(function () {

    /* toggle default value - search input |.Vinter */
    $('#searchContainer .search-input').focus(function () {
        var value = $(this).attr("value");
        if (value == "Sök" || value == "Search") {
            $(this).val("").blur(function () {
                if ($(this).attr("value") == "") {
                    $(this).val(value);
                }
            });
        }
    });
    /* end toggle value */

    $('a[rel=external]').attr('target', '_blank');

    /*tellus product slideshow clone*/
    $(".TellusPrdImage").clone().prependTo(".slideshow");

    /*webcam bigpush clone*/
    /*  $("#webcam").clone().prependTo(".bigpushsub");
    $(".bigpushsub #webcam").show();*/

    /*bokadirekt header clone*/
    $("div.product-info").clone().prependTo("div.product-info-show");
    $("div.product-info-show div.product-info").show();

    // Slideshows
    $('.slideshow').cycle({
        fx: 'scrollHorz',
        speed: '300',
        timeout: 10000,
        pager: '.pager',
        pause: 1
    });
    $('.big_image').cycle({
        fx: 'fade',
        speed: 'fast',
        timeout: 0,
        pager: '#gallery .nav',
        pagerAnchorBuilder: function (idx, slide) {
            // return selector string for existing anchor 
            return '#gallery .nav li:eq(' + idx + ') a';
        }
    });

    // Hide toggle buttons if no images
    if ($(".slideshow").children().length == 0) {
        $("a.view_1").hide();
        $("a.view_2").hide();
        $(".viewbar a").removeClass("active");
        $("a.view_1").addClass("active");
        $(".slideshow").hide();
        $(".pager").hide();
        $(".movieshow").hide();

        $(".googlemap").css("visibility", "visible").show();
    }

    if ($("#mapcontent").children().length == 0 && $("#mapLarge").children().length == 0) {

        $("a.view_1").hide();
        $("a.view_2").hide();
        $("#mapcontent").hide();
    }

    // Slideshow - changer
    $("a.view_1").click(function () {
        $(".viewbar a").removeClass("active");
        $(this).addClass("active");
        $(".slideshow").hide();
        $(".movieshow").hide();
        //$(".googlemap").css("margin",0).show();
        $(".googlemap").css("visibility", "visible").show();
        $(".pager").hide();
        return false;
    });

    $("a.view_2").click(function () {
        if ($(".slideshow").children().length > 0) {
            $(".viewbar a").removeClass("active");
            $(this).addClass("active");
            $(".slideshow").show();
            $(".movieshow").hide();
            $(".googlemap").hide();
            $(".pager").show();
        }
        return false;
    });

    $("a.view_3").click(function () {
        $(".viewbar a").removeClass("active");
        $(this).addClass("active");
        $(".slideshow").hide();
        $(".movieshow").show();
        $(".googlemap").hide();
        $(".pager").hide();
        return false;
    });

    // Searchbox 
    $("a#searchme").click(function () { $("#searchbox div").animate({ top: '0' }, 500); return false; });
    $("a#closesearchme").click(function () { $("#searchbox div").animate({ top: '-300px' }, 500); return false; });

    // TellusProduct Tab-Navigation
    $(".tab:not(:first)").hide();
    $(".tab:first").show();
    $(".htabs a:first").addClass("active");

    //when we click one of the tabs
    $(".htabs a").click(function () {
        $(".htabs a").removeClass("active");
        $(this).addClass("active");
        //get the ID of the element we need to show
        stringref = $(this).attr("href").split('#')[1];
        //hide the tabs that doesn't match the ID
        $('.tab:not(#' + stringref + ')').hide();
        //fix
        if ($.browser.msie && $.browser.version.substr(0, 3) == "6.0") { $('.tab#' + stringref).show(); }
        else
        //display our tab fading it in
            $('.tab#' + stringref).fadeIn();
        //stay with me
        return false;
    });
    /*Kontrollerar URL:en om den är samma som länken (CustomParentLink)*/
    urlCheck = window.location.pathname + window.location.search;
    $(".hzmenu a[href=" + urlCheck + "]").addClass("active");

    // SOCIAL MEDIA - SHARES
    fullURL = window.location.href
    title = document.title
    // måste fixa samtliga &-tecken till &amp; samt att fullURL fungerar vid externa länkar.
    $("#sm_facebook").attr({ href: "http://www.facebook.com/share.php?u=" + fullURL + "&t=" + title });
    $("#sm_twitter").attr({ href: "http://twitter.com/home?status=" + fullURL });
    $("#sm_delicious").attr({ href: "http://delicious.com/save?url=" + fullURL + "&title=" + title });
    $("#sm_email").attr({ href: "mailto:?subject=Kolla in " + title + "&body='Länk till sidan:" + fullURL + "" });

    // Calendar startpage - Show value
    $(".calendar-menu li").each(function (i) {
        i = i + 1;
        if (i <= 8) {
            $(this).css("display", "block");
        }
    });

    $(".fancyiframe").fancybox({
        'width': '75%',
        'height': '75%',
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'titleShow': false,
        'type': 'iframe'
    });

    /* check if product has map or slideshow */
    if ($(".pushwrapper div:visible").length == 0 && $(".pushwrapper .slideshow span").length == 0 && $(".bigpushsub img").length == 0) {
        $(".grid_9.bigpushsub").hide();
    }


    // INACTIVE
    // Sorting Umbraco and Tellus submenu's alphabetical
    /* var mylist = $('ul.submenu');
    var listitems = mylist.children('li').get();
    listitems.sort(function(a, b) {
    var compA = $(a).text().toUpperCase();
    var compB = $(b).text().toUpperCase();
    return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
    })
    $.each(listitems, function(idx, itm) { mylist.append(itm); }); 
    */

    $("#tellus_submenu").change(onSelectChange);


});                              // end document.ready
function onSelectChange(){
	var selected = $("#tellus_submenu option:selected");
	location.href = selected.val();
}

