if (!console) {
	var console = Object();
	console.log = function(msg) {};
}

function setTitle(title)
{
    $('title').html('Krusell - ' + title);
}

function header_search_filter(request, response) {
    ajaxUrl = header_search_autocomplete_url + "/" + request.term;

    $("#header-search-type").val('text');

    $.ajax({
        url: ajaxUrl,
        dataType: "json",
        success: function(data)
        {
            response(data);
        }
    });
}

function header_search_submit()
{
    var type = $("#header-search-type").val(),
        value = $("#header-search-text").val(),
        text = value;

    if(type != "text")
        value = $("#header-search-special").val()

    var redir_url = header_search_redirect_url + "/" + type + "/" + value + "/" + text;

    window.location = redir_url;
}

function toggle_search(focus)
{
    if (typeof focus == "undefined") {
        focus = true;
    }

    var $search_box = $("#header-search-box"),
        pos = $("#toggle-search").offset();
    var left = pos.left,
        top = 61;

    if( $search_box.is(':visible') )
        $("#header-search-arrow").delay(120).fadeOut(10);
    else
        $("#header-search-arrow").css('display', 'block').css('left', left - 10).css('top', top);

    $search_box.css("top", top).css('left', left).slideToggle({
        duration: 200
    });
    $search_box.css('display', 'inline');

    if(focus)
        $("#header-search-text").focus();
}

$(document).ready( function () {

    //Track events to google analytics
    if( typeof(_gaq) == "object" )
    {
        $("a._ga_event").click( function() {
            var _category = $(this).attr("category"),
                _action = $(this).attr("action"),
                _label = $(this).attr("label");
            
            if( typeof(_category) != "undefined" && typeof(_action) != "undefined" )
            {
                _gaq.push(['_trackEvent', _category, _action, _label]);
            }
        });
    }

    //autocomplete for the header-search
    $("#header-search-text").autocomplete({
        source: header_search_filter,
        delay: 100,
        minLength: 2,
        select: function( event, ui ) {
            if( ui.item )
            {
                //If the search is for a specific item/model, make sure it does not search by text
                switch(ui.item.type)
                {
                    case 'model':
                        $("#header-search-type").val('modelid');
                        $("#header-search-special").val(ui.item.id);
                        break;
                    case 'itemno':
                        $("#header-search-type").val('productid');
                        $("#header-search-special").val(ui.item.id);
                        break;
                    case 'category':
                        $("#header-search-type").val('categoryid');
                        $("#header-search-special").val(ui.item.id);
                        break;
                    default:
                        $("#header-search-type").val('text');
                        $("#header-search-special").val('');
                        break;
                }

                $("#header-search-text").val(ui.item.label);
                header_search_submit();
            }
        }
    });

    if (window.addEventListener) {
        var keys = [],
        keyCode = "38,38,40,40,37,39,37,39,66,65";

        window.addEventListener("keydown", function(e){
            keys.push(e.keyCode);
            if (keys.toString().indexOf(keyCode) >= 0) {
                alert('010101110111001001101001011101000111010001100101011011100010000001100010011110010010000001001001011101100110000101110010001000000100110001100001011100100111001101110011011011110110111000100000011000010110111001100100001000000100000101101110011001000111001011101001001000000100110001100001011100110111101001101100011011110010111000100000101010010010000001001011011100100111010101110011011001010110110001101100');
                keys = [];
            };
        }, true);
    };

    $(".nyroModal").nyroModal({
        sizes: {
            minW: 700
        }
    });

    $(".submenu").not('#select-region-box').not('#header-search-box').css("background-color","#000000").css({
        opacity: '0.9',
        fontFamily: 'verdana'
    });

    $("#toggle-search").click( function(e) {
        e.preventDefault();

        toggle_search();

        return false;
    })

    $("#select-region").click( function(e) {
        e.preventDefault();

        var $select_region_box = $("#select-region-box"),
            pos = $(this).offset(),
            left = pos.left - $select_region_box.width() + $(this).width(),
            top = pos.top + 25;

        $select_region_box.css("top", top).css('left', left).slideToggle({
            duration: 200
        });

        return false;
    })

    $("#header-search-submit").click( function(e) {
       e.preventDefault();

       header_search_submit();
    });

    $("#header-search-text").keyup( function(e) {
        if(e.keyCode == '13')
            header_search_submit();
    })

	// set submenus for all menus that have one
	$("#fold a").each(function () {
		submenu = $("#sub" + $(this).attr("id"));
		if (submenu.length) {
			this.submenu = submenu;
		} else {
			this.submenu = false;
		}
	});

	// position and show submenu when mouse is over
	$("#fold a").mouseenter(function () {
		if($(this)[0].submenu) {
			$('.submenu').not(this.submenu).hide();
			// position submenu
			clearTimeout(this.submenu[0].hidetimer);
                        var pos = $(this).offset(), width = 125;
			this.submenu.css("top", pos.top).css("left", pos.left + width);
			this.submenu.show();
		}
	});

	$('.submenu').mouseenter(function() {
		clearTimeout(this.hidetimer);
	});

	// hide when leaving top menu, but leave time to hit submenu
	$("#fold a").mouseleave(function () {
		clearTimeout(this.submenu[0].hidetimer);
		var hidetimer = setTimeout("$('#" + this.submenu.attr('id')+"').hide()", 400);
		this.submenu[0].hidetimer = hidetimer;
	});

        //Hide the submenu after a timeout. Except for the search box
	$('.submenu').not('#header-search-box').mouseleave(function() {
		var hidetimer = setTimeout("$('#" + $(this).attr('id') + "').hide()", 400);
		clearTimeout(this.hidetimer);
		this.hidetimer = hidetimer;
	});

    //Resize the window
    var resizeTimer = null;

    $(window).bind('resize', function() {
        if(typeof window.doContentResize == 'function') {
            if (resizeTimer) clearTimeout(resizeTimer);
            resizeTimer = setTimeout(doContentResize, 100);
        }

        $("#select-region-box").hide();
    });

    if(typeof window.doContentResize == 'function') {
        doContentResize();
    }
});

function doContentResize()
{
    var h = $(window).height();
    var w = $(window).width();
    var self = $(".content"),
        offsetTop = ($(".content").offset().top) + parseInt($(".content").css('padding-top'));
        offsetLeft = ($(".content").offset().left) + parseInt($(".content").css('padding-left'));
        
    $(".content").height(h - offsetTop).width(w - offsetLeft);
}
