$(document).ready(function () {

    //------------------------
    // Slide show user control, startpage
    //------------------------    
    $('body.startpage #billboard').cycle({
        fx: 'fade',
        timeout: 5000,
        speed: 800,
        pager: '#billboard-pager',
        // callback fn that creates a thumbnail to use as pager anchor 
        pagerAnchorBuilder: function (idx, slide) {
            var style = $('#billboard').attr("data-style");
            if (style == 'table') {
                var title = $('.slideImg-' + idx).attr("title");
                return '<td style="width:' + 100 / $("#billboard-pager").attr("data-count") + '%;"><a href="javascript:;">' + title + '</a></td>';
            }
            else {
                return '<li><a href="javascript:;">' + idx + '</a></li>';
            }
        }
    });
    //------------------------
    // Slide show user control, normalpage
    //------------------------    
    $('body.normalpage #billboard').cycle({
        fx: 'fade',
        timeout: 3000,
        speed: 600,
        pager: '#billboard-pager',
        // callback fn that creates a thumbnail to use as pager anchor 
        pagerAnchorBuilder: function (idx, slide) {
            var style = $('#billboard').attr("data-style");
            if (style == 'table') {
                var title = $('.slideImg-' + idx).attr("title");
                return '<td style="width:' + 100 / $("#billboard-pager").attr("data-count") + '%;"><a href="javascript:;">' + title + '</a></td>';
            }
            else {
                return '<li><a href="javascript:;">' + idx + '</a></li>';
            }
        }
    });
    //------------------------
    // Slide show user control, normalpage
    //------------------------    
    $('body.newsitempage #billboard').cycle({
        fx: 'fade',
        timeout: 3000,
        speed: 600,
        pager: '#billboard-pager',
        containerResize: 1,

        // callback fn that creates a thumbnail to use as pager anchor 
        pagerAnchorBuilder: function (idx, slide) {
            var style = $('#billboard').attr("data-style");
            if (style == 'table') {
                var title = $('.slideImg-' + idx).attr("title");
                return '<td style="width:' + 100 / $("#billboard-pager").attr("data-count") + '%;"><a href="javascript:;">' + title + '</a></td>';
            }
            else {
                return '<li><a href="javascript:;">' + idx + '</a></li>';
            }
        }
    });

    //-------------
    // Social media
    //-------------
    $('#socialmediatoggle').click(function () {
        $('div.social-media div.icon-list').toggle(400);
        $(this).toggleClass('active');
        return false;
    });

    $('#mailtoggle').click(function () {
        $('#mailform').toggle(400);
        $('#mailform input').first().focus();
        //$("#mailform input:submit").animate({ scrollTo: $("#mailform input:submit").offset().top }, 1000);
        $(window).scrollTo($('#mailform'), 800);
        return false;
    });

    //---------------------------------
    //  Google maps
    //  Large map with mutiple markers
    //---------------------------------
    var stores = [];
    var map;
    var image = "/Templates/Website/Images/logotype-wurth-googlemap.png";

    // Add marker, with custom icon and click event listner, to map
    function addMarker(lat, long, name, content) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, long),
            map: map,
            draggable: false,
            icon: image,
            title: name,
            animation: google.maps.Animation.DROP
        })

        var detailswindow = new google.maps.InfoWindow({ content: content });
        google.maps.event.addListener(marker, 'click', function () {
            detailswindow.open(map, marker);
        })
    }

    // Initialize map    
    $('div.map_canvas').ready(function () {
        if ($("div.map_canvas").length > 0) {
            var jqxhr = $.getJSON(window.location + "?json=children", function (data) {
            })
            .success(function (data) { /*Successfull JSON request */

                for (var i = 0; i < data.length; i++) {
                    stores.push(data[i]);
                }

            })
            .error(function () { /*Invalid JSON */ })
            .complete(function () {/*Completed JSON request */
                var mapOptions = {
                    zoom: 5,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: new google.maps.LatLng(62.451406, 15.029297)
                };

                map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
                for (var i = 0; i < stores.length; i++) {
                    if (stores[i].Lat != null && stores[i].Long != null) {
                        var content = "<div>" + stores[i].Name + "</div>";
                        if (stores[i].Address != null) {
                            content += "<div>" + stores[i].Address + "</div>";
                        }
                        if (stores[i].Phone != null) {
                            content += "<div>" + stores[i].Phone + "</div>";
                        }
                        /*if (stores[i].Fax != null) {
                        content += "<div>" + stores[i].Fax + "</div>";
                        }*/
                        if (stores[i].Email != null) {
                            content += "<div><a href=\"mailto:" + stores[i].Email + "\">" + stores[i].Email + "</a></div>";
                        }
                        if (stores[i].PageLink != null) {
                            content += "<div><a href=\"" + stores[i].PageLink + "\">G&aring; till butikssida</a></div>";
                        }

                        addMarker(stores[i].Lat, stores[i].Long, stores[i].Name, content);
                    }

                }
            });
        }
    });
    //---------------------------------
    //  Google maps
    //  Single store map
    //---------------------------------    
    $('div.singlestore_map').each(function () {
        var long = $(this).attr("data-long");
        var lat = $(this).attr("data-lat");
        var address = $(this).attr("data-adress");
        var storeDetails = $(this).attr("data-details");
        if (long.length > 0 && lat.length > 0) {
            var latlng = new google.maps.LatLng(lat, long);
            var mapOptions = {
                zoom: 12,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            if ($("div.singlestore_map").length > 0) {
                var map = new google.maps.Map($(this).get(0), mapOptions);
                var marker = new google.maps.Marker({
                    position: latlng,
                    map: map,
                    icon: image,
                    title: address
                });

                var detailswindow = new google.maps.InfoWindow({ content: storeDetails });
                google.maps.event.addListener(marker, 'click', function () {
                    detailswindow.open(map, marker);
                });
            }
        }
    });
    //-------------------------
    // Documentlist orderform
    //-------------------------
    $('.orderFormLink').click(function () {
        var orderform = $(this).parent().next();
        $(orderform).toggle(400);
        $("input", orderform).first().focus();
    });
    //-------------------------
    // Contactpage, Clearform
    //-------------------------
    $("dd.last input:first-child").click(function () {
        $(':input', '.contactpage')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');

    });
    //-------------------------
    // Cufon Webfonts
    //-------------------------
    Cufon.now();
    Cufon.replace('.startpage div.imagetext', { fontFamily: 'Wuerth Extra Bold Cond' });
    Cufon.replace('h1', { fontFamily: 'Wuerth Extra Bold Cond' });
    Cufon.replace('h2', { fontFamily: 'Wuerth Extra Bold Cond' });
    Cufon.replace('div.content p.intro', { fontFamily: 'Wuerth Bold' });
    //-------------------------
    // Xforms, style odd rows
    //-------------------------
    $('#id_matrix tr:nth-child(odd)').addClass('odd');

    //-------------------------
    // Hide/show selected item in leftmenu on click
    //-------------------------
    $('li.accordion a.active, li.accordion a.sub-active').click(function (event) {
        event.preventDefault();
        $cont = $(this).parent();
        $('ul', $cont).toggle();
    });
   

});






















