$(document).ready(function() {

    //Hoofdnavigatie mouseovers
    /**/
    $("#menu li:not(#menu li li)").hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);
    /**/
    $("#menu li li").hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);

    $("#searchResults tr.searchRowResult").hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);

    $("#advertentiehResults tr.searchRowResult").hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);

    $("#trafficreportResults tr.searchRowResult").hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);

    $("#orderFields TD:not(#orderFields TD.empty)").hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);

    $("#paging A.ABtn:not(#paging A.activePage)").hover(
		function() {
		    $(this).removeClass('noBG');
		},
		function() {
		    $(this).addClass('noBG');
		}
	);

    $(".scroller").scrollable({ size: 2 }).mousewheel();

    setDeleteIcons();

    $(".tt").tooltip({
        track: true,
        delay: 0,
        showURL: false,
        opacity: 1,
        fixPNG: false,
        showBody: " - ",
        extraClass: "tooltip",
        top: -80,
        left: -75
    });

    loginButtons();
    blockLastViewed();
})

function fillCompare(obj) {
    var code = $(obj).val();
    var remove = false;    
    if (!$(obj).attr('checked') != '') {
        remove = true;
    }
    $.ajax({
        type: "GET",
        url: PROJECT_URL + 'xml/addtocompare?code=' + code + '&remove='+ remove,
        dataType: "html",
        success: function(html) {
            $('#advert-range').val(html);
        }
    });
}
function filterCompare(path, code) {
    $('#compare_' + code).val('');
    var str = '';
    $('input[name=compare]').each(function() {
        if ($(this).val() != '') {
            if (str != '') str += ',';
            str += $(this).val();
        }
    });
    $('#range').val(str);
    location.href = path + '?range=' + $('#range').val()
}

function swapClassName(obj, className)
{
	obj.className = className;
}

function replaceText(obj, strReplace, strDefault, clear)
{	
	switch(clear)
	{
		case true:obj.value = (obj.val==strDefault) ? strReplace : obj.value;break;
		default:obj.value = (obj.value=="") ? strDefault : obj.value;break;
	}
}

function mOn(obj) {
	var ext = obj.src.slice(-3)
	if(obj.src.indexOf('-on.'+ ext)==-1){
		obj.src=obj.src.replace('.'+ ext,'-on.'+ ext)
	}
}
function mOut(obj) {
	var ext = obj.src.slice(-3)
	obj.src=obj.src.replace('-on.'+ext,'.'+ext)
}

function extendedSearch(show){
	if(show){
		$('#extendedsearch').slideDown();
		$('#btn-open-extended').addClass('hidden');
		$('#btn-close-extended').removeClass('hidden');
	}else{
		$('#extendedsearch').slideUp();
		$('#btn-close-extended').addClass('hidden');
		$('#btn-open-extended').removeClass('hidden');
	}
}


/* specificaties js functies */

function other(obj, type) {
    if (obj.value == '') {
        document.getElementById(type + 'Other').style.display = 'block';
        document.getElementById(type + '_other').value = '0';
        if (document.getElementById("bodgevraagd")) {
            document.getElementById("bodgevraagd").checked = false
        }
    } else {
        document.getElementById(type + 'Other').style.display = 'none';
        document.getElementById(type + '_other').value = '0';
        if (document.getElementById("bodgevraagd")) {
            document.getElementById("bodgevraagd").checked = false
        }
    }
}
function motorMerk(index) {
    if (index == 46) {
        $('#motorMerk2').css('display','inline');
    } else {
        $('#motorMerk2').css('display', 'none');
        $('#motorMerk2').val('');
    }
}
function checkOther(id, type) {
    var obj = $('#' + id);
    if (obj.val() == 'Overig') {
        $('#' + type + 'Other').css('display','block');
        //$('#' + type + '_other').val('');
        $('#' + type + '_other').attr('title','Verplicht');
        
    } else {
        $('#' + type + 'Other').css('display','none');
        $('#' + type + '_other').val('');
        $('#' + type + '_other').attr('title', '');
    }
}

function rondhouten(index) {
    if (index == 4) {
        document.getElementById('Rondhouten2').style.display = 'block';
    }
    else {
        document.getElementById('Rondhouten2').style.display = 'none';
    }
}
function verwarming(index) {
    if (index == 3) {
        document.getElementById('Verwarming3').style.display = 'block';
    }
    else {
        document.getElementById('Verwarming3').style.display = 'none';
    }
}

function updateModels(id, cont, modelId) {
    if(isNaN(id)) id = 0
    $.ajax({
        type: "GET",
        url: PROJECT_URL + 'xml/models?brandid=' + id + '&modelId=' + modelId,
        dataType: "html",
        success: function(html) {
        $('#' + cont).html(html);
        }
    });
}

function del_onclick(s) {
    return confirm(s)
}

function pub(obj, baseUrl) {
    var thisId = $(obj).attr('id');
    var pub = $(obj).attr('checked');
    var url = baseUrl + '&pub=' + pub;
    $('#' + thisId + '_url').attr('href', url);
    $('#' + thisId + '_url').click();

    //update check voor magazines
    if (thisId.indexOf('mag') > -1) {
        if (thisId.indexOf('mag_big_') > -1) {
            $('#' + thisId.replace('mag_big_', 'mag_small_')).attr('checked', '');
        } else {
            $('#' + thisId.replace('mag_small_', 'mag_big_')).attr('checked', '');
        }
    }
}

//verwijderen van foto tijdens advertentie toevoegen
function delImage(url, id) {
    $.ajax({
        type: "GET",
        url: url + '&id=' + id,
        dataType: "html",
        success: function(html) {
            $('#cont_photos').html(html);
            setDeleteIcons();
        }
    });
}

function delVideo(url, deleteFromTemp, id) {
    $.ajax({
        type: "GET",
        url: url + '&id=' + id + '&deleteFromTemp=' + deleteFromTemp,
        dataType: "html",
        success: function(html) {
            setDeleteIcons();
            document.location.href = document.location.href;
        }
    });
}

function delBrokerImage(url, nr) {
    $.ajax({
        type: "GET",
        url: url + '&nr=' + nr,
        dataType: "html",
        success: function(html) {
        setDeleteIcons();
        document.location.href = document.location.href;
        }
    });
}

function setDeleteIcons() {
    $('.deleteicons .thumb, #c_thumb_0').hover(
        function() {
            var thisId = $(this).attr('id');
            $('#' + thisId + '_delete').css('display', 'block');
        },
        function() {
            var thisId = $(this).attr('id');
            $('#' + thisId + '_delete').css('display', 'none');
        }
    );
}

function setFormBeforeSave(formId, id) {
    $('#saveAndStop').val(id);
    $('#' + formId).submit();
}

function validateUsername(username) {
    var result;
    $.ajax({
        type: "GET",
        url: PROJECT_URL + 'xml/ValidateUsername?username=' + username,
        dataType: "html",
        async: false,
        success: function(html) {            
            if (html.toLowerCase() == 'true') {
                result = true;
            } else {
                result = false;
            }
        }
    });
    return result;
}

function loginButtons() {
    $.ajax({
        type: "POST",
        url: ACCOUNT_URL + 'xml/loggedin',
        dataType: "html",
        success: function(html) {
            $('#header_login').html(html);
        },
        error: function() {
            loginButtons2();
        }
    });
}

function loginButtons2() {
    $.ajax({
        type: "POST",
        url: PROJECT_URL + 'xml/loggedin',
        dataType: "html",
        success: function(html) {
            $('#header_login').html(html);
        }
    });
}

function blockLastViewed() {
    $.ajax({
        type: "POST",
        url: ACCOUNT_URL + 'xml/lastviewed',
        dataType: "html",
        success: function(html) {
            $('#blocklastviewed').html(html);
        },
        error: function() {
            blockLastViewed2;
        }
    });
}

function blockLastViewed2() {
    $.ajax({
        type: "POST",
        url: PROJECT_URL + 'xml/lastviewed',
        dataType: "html",
        success: function(html) {
            $('#blocklastviewed').html(html);
        }
    });
}