(function($) {
    var userAgent = navigator.userAgent.toString().toLowerCase();
    if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1) && (navigator.platform=="Win32" || navigator.platform=="Win64")) {
        $("body").addClass("safari");
    }
})(jQuery);

/*
Sidenav menu hover
 */
$("#sidenav > li").not(".selected").mouseenter(function() { 
    $(this).addClass("hover");
    $(".bg", this).fadeIn(50);
}).mouseleave(function() { 
    $(this).removeClass("hover");
    $(".bg", this).fadeOut(50);
});

/*$(".def img").each(function() { 
  if (this.alt.length>0) {
    $(this).after("<p class=\"imgdescription\">"+this.alt+"</p>");
  }
});*/

/*
Tabel
 */
{
    $(".table tr:odd").addClass("odd");
    $("#content-wrapper .table2 tbody tr").click(function() {
        document.location = $("a", this).attr("href");
    });
    /*
	tabeli rida hover ja popup
   */
    (function($) {
        var timer,
        mouse_left_the_building = true;
        //close_delay = 100;
        $("#content-wrapper .table tr").mouseenter(function() {
            $("#tablepopup_active").remove();
            $(this).addClass("hover");
            var offsets = getOffsets(this);
            var tablepopup = $(".tablepopup", this)
            .clone()
            .attr("id", "tablepopup_active")
            .css("left", offsets.x+112+"px");
            $("body").append(tablepopup);
            var height = tablepopup.height();
            tablepopup
            .css("top", offsets.y-height-6+"px")
            .show();
            $(".middle", tablepopup)
            .mouseenter(function() {
                mouse_left_the_building = false;
            }).mouseleave(function() {
                mouse_left_the_building = true;
                timer = setTimeout(hide_tablepopup, close_delay);
            });
        }).mouseleave(function() {
            $(this).removeClass("hover");
            //timer = setTimeout(hide_tablepopup, close_delay);
            hide_tablepopup();
        });
        function hide_tablepopup() {
            //if (mouse_left_the_building) {
            $("#tablepopup_active").remove();
        //}
        }
    })(jQuery);
}

function getOffsets(el) {        
    var o = {
        x : el.offsetLeft,
        y : el.offsetTop
    };
    if (el.offsetParent != null) {
        var po = getOffsets(el.offsetParent);
        o.x += po.x;
        o.y += po.y;
    }
    return o;
} 

/*
Tabs
 */
{
    $('.tab-contents div.tab-content').hide(); /*Hide the content divs */
    $('.tabs li a.default-tab').addClass('current'); // Add the class "current" to the default tab
    $('.tab-contents div.default-tab').show(); // Show the div with class "default-tab"

}

$(function() {
    /*$( ".datepicker" ).datepicker({
    inline: false,
    dateFormat: 'dd.mm.yy',
    firstDay: 1
  });*/
    });

function cboxProductAvailability() {
    $.fn.colorbox({
        inline:true,
        href:".cboxProductAvailability",
        overlayClose:false,
        close:"",
        opacity:0.74,
        transition:'elastic',
        width: "500px"
    });      
}


$(".gallery1").each(function() { 
    var root = this;

    $(".thumbs a", root).click(function() {
        var big_url = this.href.replace("_thumb", "");
        $(".bigimage img", root).attr("src", big_url);
        return false;
    });
});

// #nav popup
(function($) {
    // hover
    $("#nav li.has-popup").mouseenter(function() {
        var left = $(this).width()/2*(-1)-32+"px"
        $(".nav-popup", this).css("left", left);
        $(".nav-popup", this).removeClass("hidden");
    }).mouseleave(function() {
        $(".nav-popup", this).addClass("hidden");
    });

    // inside popup hover and click
    $(".product-category-list3").each(function() {
        var root = this;
        $(".firsttd", this).mouseenter(function() {
            $(this).addClass("hover");
        }).mouseleave(function() {
            $(this).removeClass("hover");
        }).click(function() {
            document.location = $("a", this).attr("href");
        });
    });


})(jQuery);


$(".extra-equipment").each(function() { 
    var root = this;
    $(".equipment", this).mouseenter(function() {
        $(this).addClass("hover");
    }).mouseleave(function() {
        $(this).removeClass("hover");
    });
});

$("form.search input").each(function() {
    var value = $(this).val();
    var input = $(this);
    $(this).focus(function() {
        $(this).val('');
    });
    $(this).blur(function() {
        if($(this).val() == '') {
            $(this).val(value);
        }
    });
    $(this).parents('form').submit(function() {
        if(input.val() == value) {
            input.val('');
        }
    });
});

var basehref = location.href;
if(basehref.indexOf("#") != -1) {
    basehref = basehref.substr(0,basehref.indexOf("#"));
}
$("a").each(function() {
    var url = $(this).attr("href");
    if(url) {
        if(url.indexOf("#") == 0) {
            $(this).attr("href",basehref+url);
        }
    }
});

$('select.redirectselect').each(function() {
    $(this).change(function() {
        var selected = $(this).find('option:selected');
        if(selected.val() != '') {
            window.location = selected.val();
        }
    });
});

$(document).ready(function() {
  
    //$.showFileInfo();

    var searchtext = $('input#searchfield_text').val();

    $('input[name=query]').blur(function(){
        if($(this).val() == '') {
            $(this).val(searchtext);
        }
    });

    $('input[name=query]').focus(function(){
        if($(this).val() == searchtext) {
            $(this).val('');
        }
    });

    $('a.buttonShare').click(function(e){
        e.preventDefault();
        $.fn.colorbox({
            inline:true,
            href:".cboxShareWithFriend",
            overlayClose:false,
            close:"",
            opacity:0.74,
            transition:'elastic',
            width: "500px"
        });  
    });

    $('a.send_to_friend').click(function(e){
        e.preventDefault();

        var youremail = $('input[name=youremail]').val();
        var yourname = $('input[name=yourname]').val();
        var friendemail = $('input[name=friendemail]').val();
        var content = $('textarea[name=content]').val();

        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var error = 0;

        if(youremail == '' || reg.test(youremail) == false) {
            $('input[name=youremail]').parent().addClass('error');
            error=1;
        } else {
            $('input[name=youremail]').parent().removeClass('error');
        }

        if(yourname == '') {
            $('input[name=yourname]').parent().addClass('error');
            error = 1;
        } else {
            $('input[name=yourname]').parent().removeClass('error');
        }

        if(friendemail == '' || reg.test(friendemail) == false) {
            $('input[name=friendemail]').parent().addClass('error');
            error = 1;
        } else {
            $('input[name=friendemail]').parent().removeClass('error');
        }

        if(content == '') {
            $('textarea[name=content]').parent().addClass('error');
            error = 1;
        } else {
            $('input[name=content]').parent().removeClass('error');
        }

        if(!error) {
            $.getJSON('json.php?type=sendtofriend', {
                yourname:yourname,
                youremail:youremail,
                friendemail:friendemail,
                content:content
            }, function(j) {
                $.fn.colorbox.close();
            });
        }
    });
    $('img.lightbox').each(function(i){
        var a = document.createElement("a");
        $(a).attr('title', $(this).attr('title'));
        $(a).attr('href', $(this).attr('src'));  
        $(a).attr('rel', 'colorbox');
        $(this).removeClass('lightbox');          
        $(this).wrap(a);
    }); 
    $("a[rel='colorbox']").colorbox({
        transition:"fade",
        current:false,
        close: '',
        previous: '',
        next: ''
    });

});

(function($) { 
  
    $("#map .content a").bind({
        mouseenter: function() { 
            $("#map ."+this.id).addClass("active");
        },
        mouseleave: function() { 
            $("#map ."+this.id).removeClass("active");
        },
        click: function() { 
            open_mappopup("#mappopup_"+this.id.split("_")[1]);
        }
    });

    $("#map .header a, #map .footer a").bind({
        click: function() { 
            open_mappopup("#mappopup_"+this.className.split("_")[1]);
        }
    });
    function open_mappopup(selector) { 
        $("#map .mappopup").addClass("hidden");
        $(selector).find(".close").unbind().click(function() {
            $(selector).addClass("hidden");
        });
        $(selector).removeClass("hidden");
    }
})(jQuery);

$(document).ready(function() {

    //function set_scroll(){
    // get query string parameter with "?"
    var search = window.location.search;
    // if query string exists
    if (search){
        // find scroll parameter in query string
        var matches = /scroll=(\d+)/.exec(search);
        // jump to the scroll position if scroll parameter exists
        if (matches) window.scrollTo(0, matches[1]);
    }
    //}

    $('.maintain_scroll').click(function(){
        var scroll;
        // Netscape compliant
        if (typeof(window.pageYOffset) == 'number')
            scroll = window.pageYOffset;
        // DOM compliant
        else if (document.body && document.body.scrollTop)
            scroll = document.body.scrollTop;
        // IE6 standards compliant mode
        else if (document.documentElement && document.documentElement.scrollTop)
            scroll = document.documentElement.scrollTop;
        // needed for IE6 (when vertical scroll bar is on the top)
        else scroll = 0;
        $(this).attr('href', $(this).attr('href') + '?scroll=' + scroll);
    });
  
    $(function(){
        $.datepicker.setDefaults($.datepicker.regional['translate']);
        $('.datepicker').datepicker({
            inline: true,
            dateFormat: 'dd.mm.yy',
            firstDay: 1
        });
    });
});
