var _pause = false;
var _running = false;

$(document).ready(function() {
    if ( $(window).height() < 676 ) {
        var diff = 676 - $(window).height();
        if ( $.browser.msie  )
            $('div#right2').css('marginTop',(158-diff));
        else
            $('div#right2').css('marginTop',(163-diff));
    }
    
    if ( $.browser.msie && ( $.browser.version == '7.0' || $.browser.version == '6.0' ) )
        $('#details').appendTo($('body'));
    
    $('#detail_sluiten').click(function() {
        $('#overlay').hide();
        $('#details').hide();
    });

    $('.companyul').mouseover(function() {
        pauseCarousel();
    });
    
    $('.companyul').mouseout(function() {
        if ( _pause == true ) {
            _pause = false;
            positionCarousel();
        }
    });
    
    $('.companyul').find('span').mouseover(function() {
        $(this).css('text-decoration','underline');
        pauseCarousel();

    });
    $('.companyul').find('span').mouseout(function() {
        $(this).css('text-decoration','none');
    });
    $('.companyul').find('li').click(function() {
        getContent($(this).attr('id'));
    });
    
    startCarousel();
});

function menuOpen(id) {
    if ( id != 20 && id != 0 && id != '/contact/contact.php' ) {
        getContent(id);
    }
    else if ( id == 20 ) {
        getHitlist(20);
    }
    else if ( id == '/contact/contact.php' ) {
        getContactForm();
    }
    else {
        $('#details').hide();
        $('#overlay').hide();
    }
}

function getHitlist(id) {
    if ( !id )
        return false;
    var params = 'id='+id;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/content/ajax/getHitlist.php",
        data: params,
        success: function(json) {
            if ( json.error )
                alert ( json.error );
            else
                showContent(json);
        },
        error: function(oRequest, sErrType) {
            alert(oRequest + '-' + sErrType);
        }
    });
}

function getContent(id) {
    if ( !id )
        return false;
    var params = 'id='+id;
    $.ajax({
		type: "POST",
		dataType: "json",
		url: "/content/ajax/getArticle.php",
		data: params,
		success: function(json) {
			if ( json.error )
				alert(json.error);
			else 
				showContent(json);
		},
		error: function(oRequest, sErrType) {
			alert(oRequest + '-' + sErrType);
		}
	});
}

function showContent(data) {
    $('#detail_content').html((data.max ? data.max : '')+data.text);
    $('#details').show();
    $('#overlay').show();
    $('.detail_middleleft').height($('.detail_middlex').height());
    $('.detail_middleright').height($('.detail_middlex').height());
    
    if ( $('#max_ajax_edit').attr('id') )
        max_ajax_quick_edit();
}

function getContactForm() {
    $.ajax({
        type: "POST",
        dataType: "json",
		url: "/contact/ajax/contact.php",
		data: '',
		success: function(json) {
			if ( json.error )
				alert(json.error);
			else 
				showContent(json);
		},
		error: function(oRequest, sErrType) {
			alert(oRequest + '-' + sErrType);
		}
	});    
}

function submitContact() {
    var error = false;
    var naam = $('#contact').find('#naam').val();
    var email = $('#contact').find('#email').val();
    var telefoon = $('#contact').find('#telefoon').val();
    var bericht = $('#contact').find('#bericht').val();
    var spam = $('#contact').find('#spam').val();
    if ( naam == '' )
        error = true;
    if ( email == '' )
        error = true;
    if ( bericht == '' )
        error = true;
    if ( spam == '' )
        error = true;
    if ( telefoon == '' )
        telefoon = 'x';
        
    if ( error != false ) {
        alert('Vul aub alle verplichte velden in');
        return false;
    }
    else {
        var ys_language = $('#contact').find('#ys_language').val();
        var ys_path = $('#contact').find('#ys_path').val();
        var ys_site = $('#contact').find('#ys_site').val();
        var spamCheck = $('#contact').find('#spamCheck').val();
        var params = 'naam='+naam+'&email='+email+'&telefoon='+telefoon+'&bericht='+bericht+'&spamCheck_answer='+spam;
        params += '&ys_language='+ys_language+'&ys_path='+ys_path+'&ys_site='+ys_site+'&spamCheck='+spamCheck;
    }
    
    $.ajax({
        type: "POST",
        dataType: "json",
		url: "/contact/ajax/processform.php",
		data: params,
		success: function(json) {
			if ( json.error )
				alert(json.error);
			else 
				showContent(json);
		},
		error: function(oRequest, sErrType) {
			alert(oRequest + '-' + sErrType);
		}
	});
}

function startCarousel() {
    $('#companylist').css('margin-left','-300px');
    $('#companies_0').css('margin-left','300px');
    animateCarousel();
}

function animateCarousel() {
    $('#companylist').find('div:first').animate({
        marginLeft: 0
    }, 1000, positionCarousel);
}

function pauseCarousel() {
    _pause = true;
}

function positionCarousel() {
    if ( _pause == false && _running == false ) {
        _running = true;
        setTimeout(function() {
            if ( _pause == false ) {
                $('#companylist').find('div:first').appendTo($('#companylist'));
                $('#companylist').find('div:first').css('margin-left','300px');
                animateCarousel();
            }
            _running = false;
        }, 4000);
    }
}
