/************
*	Browser detectie
************/
var browser =
{
	identify : function()
	{
		var t = document.getElementsByTagName("html");
		var a = navigator.userAgent.toString().toLowerCase();
		var b;

		// todo: "opera" en de render engiens "ff gecko", "chrome webkit", "ie ie6 trident"
		switch(true)
		{
			case (a.indexOf("msie 6") != -1)	:	b = "ie ie6";		break;
			case (a.indexOf("msie 7") != -1)	:	b = "ie ie7";		break;
			case (a.indexOf("msie 8") != -1)	:	b = "ie ie8";		break;
			case (a.indexOf("firefox") != -1)	:	b = "ff";			break;
			case (a.indexOf("chrome") != -1)	:	b = "chrome";		break;	
			case (a.indexOf("safari") != -1)	:	b = "safari";		break;
			default								:	b = "undefined";
		}
		t[0].className = b;

		return b;
	}	
}

// Ajax fix voor page reload
if (document.location.href.search(/#/) != -1) {
    var url = document.location.href.split("#");
    if (url[1]) document.location.href  =  url[1];
}

//$(document).ready(function(){
//    setBehaviour();
//    initHistory();
//    externalLinks();
//});

// ja, naja ach
//browser.identify();

$(function() {
	browser.identify();
	init();
    console.log('init.');

});

function init(container)
{
	if(typeof(container) == 'undefined') {
		container = null;
	}
	
	initBehaviour(container);
	
	if(typeof(initAjaxBehaviour) !== 'undefined') {
		initAjaxBehaviour(container);
	} else {
		console.log('initAjaxBehaviour not found.');
	}
}

/**
 * Zet de eigenschappen voor een aantal elementen op basis van classes etc
 *
 */
function initBehaviour(container)
{
	
    // Ontkoppel eerst oude eigenschappen
    $('a, form, div', container).unbind();
    
    //$('a.showOptions', container).unbind('click');

	$('.downloadFormatsArea', container).click(function() {
		$('.downloadAbleFormats', container).slideUp(200);

		if($(this).children(0).css("display") != 'block') {
			$(this).children(0).slideDown(200);
		}
	});
	
    $('a.showOptions', container).click(function(){
    	$(this).parents(".boxInner").find(".options").toggle(500);
        $(this).blur();
    });
    
    $("a.active", container).each(function(){
        $(this).unbind('click');    
    });
    
    $("a.active", container).click(function(){
        notify("Dit kanaal is al geselecteerd.");
        return false;
    });
    
	// Initieer datepicker velden
    $('.datepicker', container).each(function(){
    	if (!$.isFunction($.datepicker)) {
    		includeScript("/scripts/jquery/ui/ui.datepicker.js", function(){
    			$('.datepicker', container).datepicker({dateFormat: 'dd-mm-yy'});
    		});
    		includeScript("/scripts/jquery/ui/i18n/ui.datepicker-nl.js");
    	} else {
    		$('.datepicker', container).datepicker({dateFormat: 'dd-mm-yy'});
    	}
	});
}


// Laad een javascript
function includeScript(uri, callback) {
    var script = document.createElement("script");
    script.src = uri;
    script.type = "text/javascript";
    script.language = "javascript";
    if (callback != null) {
        script.onreadystatechange = function() {
            if (script.readyState == "complete") callback();
        };
        script.onload = callback;
    }
    document.getElementsByTagName("head")[0].appendChild(script);
    return false;
}

// Laad een stylesheet
function includeStyle(uri) {
    $("head").append("<link rel='stylesheet' type='text/css' href='" + uri + "' />");
}


function initPlanner() {
    $("div.slidebox").each(function(){
        $(this).find('ul').css({width: '10000px'});
        var left  = $(this).find('.left');
        var right = $(this).find('.right');
        var mid   = $(this).find('.mid');
        
        mid.serialScroll({
            items: 'li',
            prev: left,
            next: right,
            step: 4,
            axis: 'x',
            start: 0, 
            duration:600,
            force:true,
            stop:true,
            lock: true,
            cycle: false, 
            easing:'easeOutQuart',
            jump: false 
        });
        
    });
    
    $('.profile').click(function(){
        var profile_id = $(this).attr("itemid");
        if ($(this).hasClass('selected')) {
            $('input[name=profile_id]').val('');
            $('.profile').removeClass('selected');
        } else {
            $('input[name=profile_id]').val(profile_id);
            $('.profile').removeClass('selected');
            $(this).addClass('selected');
        }
        $('form#plan.ajax').submit();
    });

    $('.platetype').click(function(){
        var platetype_id = $(this).attr("itemid");
        if ($(this).hasClass('selected')) {
            $('input[name=platetype_id]').val('');
            $('.platetype').removeClass('selected');
        } else {
            $('input[name=platetype_id]').val(platetype_id);
            $('.platetype').removeClass('selected');
            $(this).addClass('selected');
        }   
        $('form#plan.ajax').submit();
    });
    
}



var notifier = 0;
function notify(message, type) {
    notifier++;
    if (!$('#notifier').attr('id')) {
        $('#content').append("<div id='notifier'></div>");
    }
    $('#notifier').append(
    "<div id='not" + notifier + "' class='notification'>" + message
    + "</div>");
    $('#notifier').css('top', 100 + $(window).scrollTop());
    window.setTimeout("$('#not" + notifier + "').fadeOut()", 2000);
}
