var activeSubmenu = '';

$(document).ready(function() {

    $('.v_btn').click(function() {
        // $('.v_image').click();
        $('.v_video').click();
    });

    $('.v_image').click(function() {
        //video = $('.v_image').attr("title");
        //videoEmbed = '<iframe width="179" height="128" src="http://www.youtube.com/embed/' + video + '?rel=0&modestbranding=1?&autoplay=1&autohide=1&controls=0&amp;wmode=transparent" frameborder="0" allowfullscreen>';
        //$('.v_image').html(videoEmbed);
        $('.v_video').click();
    });

    $('.v_video').click(function() {
        return hs.htmlExpand(this, { objectType: 'iframe', width: 480, height: 395, preserveContent: false, wrapperClassName: 'video', objectLoadTime: 'after'})
    });


    $("#header li img, .optionsbg img").hover(
			function() { if ($(this).attr('class') !== 'act') mOn(this) },
			function() { if ($(this).attr('class') !== 'act') mOut(this) }
	);

    //mouseovers
    $(".button").hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);

    $("#menu li a").hover(
			function() {
			    if ($(this).attr('class') !== 'act') $(this).addClass('hover')
			},
			function() { $(this).removeClass('hover') }
	);

    $("#menu li a:not(#menu li li a)").click(
		    function() {
		        if (activeSubmenu) {
		            $(activeSubmenu).parent().find('.tertair').css('display', 'none');
		            $(activeSubmenu).parent().find('.tertair').css('z-index', '1');
		            $(activeSubmenu).removeClass('act');
		            bgActivate(0);
		            if ($(activeSubmenu).parent().attr('id') == $(this).parent().attr('id')) { activeSubmenu = ''; return false; }
		        }

		        if ($(this).parent().find('.tertair').size() > 0) {
		            $(this).addClass('act');
		            $(this).parent().find('.tertair').css('display', 'inline');
		            $(this).parent().find('.tertair').css('z-index', '10');
		            bgActivate(1);
		            activeSubmenu = $(this);
		            return false;
		        } else {
		            return true;
		        }
		    }
	)

    $(".sub li").hover(function() {
        if ($(this).parent().attr('class') === 'sub') $(this).addClass('hover');
        if ($(this).find('.subsub').size() > 0) {
            $(this).find('.subsub').show();
        }
    },
			function() {
			    $(this).removeClass('hover')
			    if ($(this).find('.subsub').size() > 0) {
			        $(this).find('.subsub').hide();
			    }
			}
	);

    $(".subsub li").hover(
			function() {
			    if ($(this).find('ul').size() > 0) {
			        var subnav = $(this).find('ul');
			        //bepaal positie mainlink
			        var cntItems = $(this).parent().find('li:not(ul.subsubsub li)').size();
			        var cntSub = $(this).find('li').size();
			        var defaultMargin = 10;
			        if (cntItems <= cntSub) { defaultMargin += ((cntSub - cntItems) + 2) * 20; }
			        $(this).parent().find('.mainlink').css('margin-top', defaultMargin);
			        $(subnav).show();
			        //hoogte bepaling .subsubsub tov .subsub li
			        var menuItem = parseInt(($(this).attr('class').replace('subsubitem', '')));
			        var calcHeight = (menuItem) * -menuHeight;
			        $('.subsubsub').css("margin-top", calcHeight);
			        $(this).find('.parent').addClass('act');
			    }
			},
			function() {
			    $(this).removeClass('hover')
			    var subnav = $(this).find('ul');
			    $(subnav).hide();
			    $(this).find('.parent').removeClass('act');
			    $(this).parent().find('.mainlink').css('margin-top', 10);
			}
	);

    $(".btn_contact img").click(function() {
        if ($(".contactbg").is(':visible')) {
            $(".contactbg").slideUp();
            var thissrc = $(this).attr("src");
            $(this).attr("src", thissrc.replace("_close.png", ".gif"));
        } else {
            $(".contactbg").slideDown();
            var thissrc = $(this).attr("src");
            if (thissrc.indexOf("_close.png") == -1) {
                $(this).attr("src", thissrc.replace(".png", "_close.png"));
            }
        }
    });

    var isSliding = false;
    $(".productlist li").hover(
			function() {
			    if ($(this).attr('class') !== 'act') $(this).find('.btn_moreinfo').show();
			    //				$('.btn_moreinfo').show();	
			},
			function() { $('.btn_moreinfo').hide(); }
	);

    $(".btn_moreinfo").parent().click(function() {
        var parent = $(this);
        if ($(parent).attr('class') !== 'act') {
            $('li .details').slideUp();
            $('li').removeClass('act');
            $(this).find('.btn_moreinfo').hide();

            $(parent).find('.details').slideDown();

            $(parent).addClass('act');
        }
        return false;
    });

    $(".sections li").hover(
			function() { $(this).find('.border').show() },
			function() { $(this).find('.border').hide() }
	);

    $(".sections li").click(function() {
        var url = $(this).find('a').attr('href')
        $(location).attr('href', url);
    });

    $(".btn_close").click(function() {
        $(this).parent().parent().parent().removeClass('act');
        $(this).parent().parent().slideUp();
        return false;
    });

    //printen
    if ($('#output').size() > 0) {
        $(".btn_print").click(function() {
            printme();
            return false;
        });
    } else {
        $(".btn_print").hide();
    }

    //zoekfunctionaliteit header
    $("#search input").focus(function() {
        $("#search").addClass('act');
        $('#searchresults').show();
    });

    $("#s").blur(function() {
        setTimeout(function() {
            $('#search').removeClass('act');
            $('#c_search').removeClass('hover');
            $('#searchresults').hide();
        }, 500);
    });

    $('.searchbutton').click(function() {
        submit_onclick('frmSearch', true); return false;
    });

    //tooltips
    $(".textimage").tooltip({
        track: true,
        delay: 0,
        showURL: false,
        opacity: 1,
        fixPNG: true,
        showBody: " - ",
        extraClass: "tooltip",
        top: 20,
        left: -75
    });

})

function bgActivate(on) {
    var bg = $('body').css('background-image');
    if (on) {
        if (bg.indexOf('_act.gif') == -1) {
            $('body').css('background-image', bg.replace('.gif', '_act.gif'));
        }
    } else {
        $('body').css('background-image', bg.replace('_act.gif', '.gif'));
    }
}

function mOn(obj) {
	var ext = obj.src.slice(-3)
	if(obj.src.indexOf('-on.'+ ext)==-1){
		obj.src=obj.src.replace('.'+ ext,'_o.'+ ext)
	}
}
function mOut(obj) {
	var ext = obj.src.slice(-3)
	obj.src=obj.src.replace('_o.'+ext,'.'+ext)
}

var currentPage = 1;
function showPage(p, total) {
    $('.companies li').each(function() {
        $(this).removeClass('active');
    });
    $('.companies .page' + p).each(function() {
        $(this).addClass('active');
    })
    if (p > 1) {
        $('.paging .previous').addClass('active');
    } else {
        $('.paging .previous').removeClass('active');
    }
    if (p < total) {
        $('.paging .next').addClass('active');
    } else {
        $('.paging .next').removeClass('active');
    }
    currentPage = p;
}

function showPrevPage(total) {
    if (currentPage > 1) {
        showPage(currentPage - 1, total);
    }
}
function showNextPage(total) {
    if (currentPage < total) {
        showPage(currentPage + 1, total);
    }
}

function printme() {
	var objOut = document.getElementById("output");
	var out = objOut.innerHTML;

	var wh = window.open("","wh","width=640px,height=480px,scrollbars=yes");
	wh.document.open();
	var output = '<link href="' + PROJECT_URL + 'css/print.css" rel="stylesheet" type="text/css">';
	output = '<html><head>'+output+'</head><body>'+out.replace('<object','<!--object').replace('/object>','/object-->')+'</body></html>';
	output += '<'+'script type="text/javascript">';
	output += 'setTimeout("window.print()",2000);';	
	output += '</'+'script>';
	wh.document.writeln(output);
	wh.document.close();
}

var to_search = null;
function search() {
    clearTimeout(to_search);
    to_search = setTimeout("goSearch()", 750);
}

function goSearch() {
    $('#c_search').addClass('hover');
    $('#searchresults').css('display', 'block');
    $('#search_title').html('Loading...')

    $.ajax({
        type: "GET",
        url: PATH_URL + 'xml/Search?s=' + $('#s').val(),
        data: {},
        dataType: "html",
        success: function(html) {
            $('#searchresults').html(html);
            $('#loadingicon').css('display', 'none');
        }
    });
}
function showBigTeam(id) {
    var prefix = 'bigimage-'
    $('#' + prefix + id).css('display', 'block');
    $('#' + prefix + id).mouseout(function() { $(this).css('display', 'none'); })
}
