var IE = document.all?true:false

$(document).ready(function() {

	//Hoofdnavigatie mouseovers
	$("#nav img, #language img").hover(
		function(){
			var imgsrc = $(this).attr("src")
			if(imgsrc.indexOf('_o.gif')==-1){
				imgsrc = imgsrc.replace('.gif','_o.gif');
				$(this).attr('src',imgsrc);
			}
		},
		function(){
			var thisclass = $(this).attr("class")
			if(thisclass.indexOf('active')==-1){
				var imgsrc = $(this).attr("src").replace('_o.gif','.gif');
				$(this).attr('src',imgsrc);
			}
		}
	);


	//Menu mouseovers
	$("#menu li").hover(
		function(){
			if($(this).attr('id').indexOf('activestate')==-1){
				$(this).attr('class','active hover');
				var thisclass = $(this).attr("class")
			}
		},
		function(){
			var thisid = $(this).attr("id")
			if(thisid.indexOf('activestate')==-1 && thisid.indexOf('activestatesub')==-1){
				$(this).attr('class','nonactive');
			}
		}
	);

	$("#accordion").accordion({
		event: "mouseover", animated: "easeslide"
	});
	
	//article mouseovers
	$(".normalblock, .smallblock, .tiparticle").hover(
		function(){
			$(this).children('div').attr('class','active');
			var imgsrc = $(this).find('.button').attr("src")
			if(imgsrc){
				if(imgsrc.indexOf('_o.gif')==-1){
					imgsrc = imgsrc.replace('.gif','_o.gif');
					$(this).find('.button').attr('src',imgsrc);
				}
			}
		},
		function(){
			$(this).children('div').attr('class','border');
			var imgsrc = $(this).find('.button').attr("src")
			if(imgsrc){
				imgsrc = imgsrc.replace('_o.gif','.gif');
				$(this).find('.button').attr('src',imgsrc);
			}
		}
	);

	//toplist clicks
	$(".paging a").click(
		function(){
			//actief nummer selecteren
			$(this).parent().children().attr('class','');
			$(this).attr('class','active');

			var rel = $(this).attr('rel').split(',');
			//eerst alle items uit toplist uit zetten
			$('#toplist_' + rel[0] + ' li').each(function(){
				$(this).css('display','none');
			})
			//item tonen
			$('#toplist_'+ rel[0] + '_' + rel[1]).css('display','block');

	});
	
	//loop door actie artikelen, nog niet officieel gemaakt GPS
	setTimeout("loopTop('toplist_1_1', 5000)",5000);
	setTimeout("loopTop('toplist_2_1', 5000)",5000);
	setTimeout("loopTop('toplist_3_1', 5000)",5000);

});


function loopTop(id, time){
	var o = $('#'+ id);
	var nextid;
	o.hide();
	//$('#accordion').find('[rel=' + id.replace('toplist_', '').replace('_', ',') + ']').removeClass('active');
	o.parent().find('li').each(function(){
		var pageid = $(this).attr('id').replace('toplist_', '').replace('_', ',');
		$('#accordion').find('[rel=' + pageid + ']').removeClass('active');
	});
	if(o.next().attr('id')!=undefined){
		o.next().show();
		nextid = o.next().attr('id');				
	}else{
		o.parent().find('li:first').show();
		nextid = o.parent().find('li:first').attr('id');
	}
	
	if(nextid){
		$('#accordion').find('[rel=' + nextid.replace('toplist_', '').replace('_',',')+']').addClass('active');
		setTimeout("loopTop('"+ nextid +"',"+ time +")",time);
	}
}


function ShowMailaFriend() {AnimateThis('mailafriend')}

function AnimateThis(id) {
	if(!IE){ //werkt niet in IE ???
		$('#' + id).animate({opacity: 0}, 1).animate({opacity: 1}, 500);
	}
}

function showMessage(id){
	display('item-'+ id +'-ok','item-'+ id)
	setTimeout("display('item-"+ id +"','item-"+ id +"-ok')",3000)
}

function ShowShoppingcart() {
    /*
    $('#shoppingcart').animate({
        opacity: 0
    }, 1);
    $('#shoppingcart').animate({
        opacity: 1
    }, 500);
    */
}


function increaseAmount(textid) {
    if (isNaN($('#' + textid).val())) {
        $('#' + textid).val(1);
    } else {
        if (eval($('#' + textid).val()) < 99) {
            $('#' + textid).val(eval($('#' + textid).val()) + 1);
        }
    }
    return true;
}

function decreaseAmount(textid) {
    if (isNaN($('#' + textid).val())) {
        $('#' + textid).val(1);
    } else {
        if (eval($('#' + textid).val()) > 1) {
            $('#' + textid).val(eval($('#' + textid).val()) - 1);
        }
    }
    return true;
}

function toggleCheckbox(id, show, hide){
	if($('#'+ id).attr('checked')){
		$('#'+ show).css('display','block');
		if(hide) $('#'+ hide).css('display','none');
	}else{
		$('#'+ show).css('display','none');
		if(hide) $('#'+ hide).css('display','block');
	}
}

// Versie 1.2 (WdS)
function submitNewletterForm(inputId,trueCont,falseCont,wrongCont,path) {
	if(isEmail($('#' + inputId).attr('value'))){
    $.ajax({
        type: "POST",
        url: path + "xml/SubscribeNewsletter?mail=" + $('#' + inputId).attr('value'),
        dataType: "html",
				data: "{}",
        success: function(txt) {            
            if (txt.toUpperCase() == 'TRUE') {
								$('#' + wrongCont).css("display", "none");
                $('#' + trueCont).css("display","block");
                $('#' + falseCont).css("display","none");
            }
            else {
								$('#' + wrongCont).css("display", "none");
                $('#' + trueCont).css("display", "none");
                $('#' + falseCont).css("display", "block");                
            }
        }
    });
	}else{
		$('#' + wrongCont).css("display", "block");
		$('#' + trueCont).css("display", "none");
    $('#' + falseCont).css("display", "none");
	}
}

// Versie 1.2 (JS)
function RemoveSingleViewedArticles(path,id) {
		$.get(path + 'xml/RemoveSingleViewedArticles?id='+id);
		$('#viewedarticles-'+id).html('');
}

// Versie 1.2 (JS)
function RemoveViewedArticles(path) {
    $.get(path + 'xml/RemoveViewedArticles');
    $('#viewedarticles').html('');
}

// Versie 1.3 (GPS)
function fill(dest, source, clear, predefined){
	var arr = source.split(',');
	var textToInsert = ''; var currentValue = '';
	if(predefined){
		var predefinedObj = document.getElementById(predefined);
		var predefinedArr = predefinedObj.options[predefinedObj.selectedIndex].value.split(',');
	}
	if(!clear){
		$.each(arr, function(count, item){
			if(textToInsert!='') textToInsert += ', ';
			if(predefined){
				currentValue = predefinedArr[count];
				$('#'+ item).val(predefinedArr[count])
			}else{
				currentValue = $('#'+ item).val()
			}
			textToInsert += $('#'+ item +'title').val() + currentValue.toUpperCase();
		})
	}else{ //restore
		predefinedObj.selectedIndex = 0;
		$.each(arr, function(count, item){
			$('#'+ item).val($('#'+ item +'value').val());
		});
	}
	$('#' + dest).val(textToInsert);
}

// Versie 1.2 (WdS)
function updateArticleDetail(articleId, orgSpec1, orgSpec2, htmlCont, path) {
    var selectedSpec01, selectedSpec02
    if (document.getElementById(orgSpec1)) {
        selectedSpec01 = document.getElementById(orgSpec1).options[document.getElementById(orgSpec1).selectedIndex].value
    } else {
        selectedSpec01 = 0
    }
    if (document.getElementById(orgSpec2)) {
        selectedSpec02 = document.getElementById(orgSpec2).options[document.getElementById(orgSpec2).selectedIndex].value
    } else {
        selectedSpec02 = 0
    }
    $.ajax({
        type: "POST",
        url: path + 'xml/UpdateArticleDetail?Id=' + articleId + '&spec01Id=' + selectedSpec01 + '&spec02Id=' + selectedSpec02,
        dataType: "html",
				data: "{}",
        success: function(html) {                    
            $('#' + htmlCont).html(html);
						initSIFR();
        }
    });
}

function openBig(url){
	if(url!='') $('#bigimage').attr('href',url);
	document.getElementById('bigimage').onclick();
}

// Versie 1.3.2 (JS)
function updateArticleFromAddToOrder(articleId, orgSpec1, orgSpec2, htmlCont, path) {
    var selectedSpec01, selectedSpec02
    if (document.getElementById(orgSpec1)) {
        selectedSpec01 = document.getElementById(orgSpec1).options[document.getElementById(orgSpec1).selectedIndex].value
    } else {
        selectedSpec01 = 0
    }
    if (document.getElementById(orgSpec2)) {
        selectedSpec02 = document.getElementById(orgSpec2).options[document.getElementById(orgSpec2).selectedIndex].value
    } else {
        selectedSpec02 = 0
    }
    $.ajax({
        type: "POST",
        url: path + 'xml/UpdateArticleDetail?Id=' + articleId + '&spec01Id=' + selectedSpec01 + '&spec02Id=' + selectedSpec02 + '&viewName=ArticleAddToOrder',
        data: {},
        dataType: "html",
        success: function(html) {            
            $('#' + htmlCont).html(html);
        }
    });
}


// Versie 1.3 (JS)
function AddRelatedArticlesToOrder(path, checkout_url, related) {
    try {
        var timeout = 0;
        var error = false;
				var id = 0;
				var tmpStr = '';

				//check of alle specs ingevuld zijn...
				$("[name=addToOrder]:checked").each(function() {
					tmpStr = $(this).attr('id');
				 	id = tmpStr.substr(tmpStr.indexOf('_') + 1);
					if ($('#spec1-' + id).val() == '0' || $('#spec2-' + id).val() == '0') {

						display('related_error_specs','related_button');
						//alert('Specificatie is niet ingevuld')
						error = true;
					}
				});

				// Alle aangevinkte artikelen opvragen en in winkelwagentje plaatsen.
        if ($("[name=addToOrder]:checked").size() == 0 && related) {
            display('related_error_selected','related_button');
            error = true;
				} else {
					if(!error) {
            
            $("[name=addToOrder]:checked").each(function() {
                tmpStr = $(this).attr('id');
                id = tmpStr.substr(tmpStr.indexOf('_') + 1);
                var specId = $('#specId-' + id).val();
                var amount = $('#amount-' + id).val();
                if ($('#spec1-' + id).val() == '0' || $('#spec2-' + id).val() == '0') {
				            display('related_error_specs','related_button');
                    //alert('Specificatie is niet ingevuld')
                    error = true;
                } else {
                    timeout += 600;
                    $.ajax({
                        type: 'GET',
                        url: path + 'xml/AddToShoppingCart',
                        data: 'articleId=' + id + '&amount=' + amount + '&printingText=&specId=' + specId
                    });
                }
            });

						//Haal ook de gegevens op van het huidige product
						var articleId = $('#articleId').val();
						var articleSpecId = $('#specId').val();
						var articleAmount = $('#amount-' + articleId).val();
						var articlePrintingText = $('#printingText').val();
						if(!articlePrintingText) articlePrintingText = '';
						timeout += 600;
						$.ajax({
								type: 'GET',
								url: path + 'xml/AddToShoppingCart',
								data: 'articleId=' + articleId + '&amount=' + articleAmount + '&printingText='+ articlePrintingText +'&specId=' + articleSpecId,
								success: function(html) {
									showMessage(articleId)
									document.getElementById('shoppingcart').innerHTML = html;
								}
						});
						//einde ophalen gegevens van het huidige product

					}
        }
        if (!error){
            //setTimeout(function() { location.href = checkout_url; }, timeout);
            return false
        }else{
						setTimeout("display('related_button','related_error_selected,related_error_specs')", 2000);
            return false;
        }
    } catch (err) {
        alert("Er is iets foutgegaan. Probeer het opnieuw door deze pagina te herladen. " + err.message);
    }
}

function expandFlash(bool){
	if(bool){
		$('#contArticleDetail').css('display','none');
		$('#container_tellafriend').css('display','none');
		$('#container_image').attr('class','articleimage_wide')
	}else{
		$('#container_image').attr('class','articleimage')
		$('#contArticleDetail').css('display','block')
		$('#container_tellafriend').css('display','block');
	}
}

// Versie 1.7 (JS)
function addCouponCode(val, path) {
	$('#error-coupon').hide();
	$.ajax({
		type: "POST",
		url: path + 'xml/AddCouponCode?typeid=3&code=' + val,
		data: {},
		dataType: "html",
		success: function(html) {
			if (html == 'True') {
				location.reload(true);
			} else {
				$('#error-coupon').show();
			}
		}
	});
}

// Versie 1.7 (JS)
function removeCouponCode(val, path) {
	$('#error-coupon').hide();
	$.ajax({
		type: "POST",
		url: path + 'xml/RemoveCouponCode?code=' + val,
		data: {},
		dataType: "html",
		success: function(html) {
			if (html == 'True') {
				location.reload(true);
				
			} else {
				$('#error-coupon').show();
			}
		}
	});
}	

function setDateOfBirth() {
    if (document.getElementById("dob_dd").options[document.getElementById("dob_dd").selectedIndex].value == "" || document.getElementById("dob_mm").options[document.getElementById("dob_mm").selectedIndex].value == "" || document.getElementById("dob_yy").options[document.getElementById("dob_yy").selectedIndex].value == "") {
        document.getElementById("dateofbirth_DD").value = '';
    } else {
        document.getElementById("dateofbirth_DD").value = document.getElementById("dob_dd").options[document.getElementById("dob_dd").selectedIndex].value + '-' + document.getElementById("dob_mm").options[document.getElementById("dob_mm").selectedIndex].value + '-' + document.getElementById("dob_yy").options[document.getElementById("dob_yy").selectedIndex].value
    }
}