﻿var IE = document.all?true:false

$(document).ready(function() {
    // Artikel item mouseover/mouseout.
    $(".articleItem").hover(function() {
        $(".articleItem-header", this).addClass("articleItem-header-active");
        $(".articleItem-content", this).addClass("articleItem-content-active");
        $("a:first", this).addClass("active");
    }, function() {
        $(".articleItem-header", this).removeClass("articleItem-header-active");
        $(".articleItem-content", this).removeClass("articleItem-content-active");
        $("a:first", this).removeClass("active");
    }).click(function() {
        location.href = $("a:first", this).attr("href");
    });

	//Menu mouseovers
	$("#menu div").hover(
		function(){
			var className = $(this).attr("class")
			var imgsrc = $('img', this).attr("src")
			if(className.indexOf('_on')==-1){
				$(this).attr("class",className + '_on');
				imgsrc = imgsrc.replace('.gif','_o.gif');
				$('img', this).attr("src",imgsrc);
			}
		},
		function(){
			if($(this).hasClass('active')==false){
				var className = $(this).attr("class").replace('_on','');
				var imgsrc = $('img', this).attr("src").replace('_o.gif','.gif');
				$(this).attr("class",className);
				$('img', this).attr("src",imgsrc);
			}
		}
	);
});

function shoppingcartAlert(isCheckout){
	scroll(0,0);
	$('.overlaymessage.shop').show();
	if(isCheckout){
		$('#overlay-checkout').hide();
		$('#addToCart-checkout').click();
	} else {
		$('#addToCart').click();
	}
	$('.dummy .added').show();
}

function ShowMailaFriend() {AnimateThis('mailafriend')}
function ShowShoppingcart() {
		//AnimateThis('shoppingcart')
		shoppingcartAlert();
		//document.location.href=PROJECT_URL + tr_shoppingcart + '/'
}
function ShowShoppingcartCheckout() {
	shoppingcartAlert(1);
}

function CloseShoppingcart(isCheckout) {
	if(isCheckout){
		location.reload(true);
	}else{
		parent.hs.close();
		$('.overlaymessage.shop').hide();
	}
}

function AnimateThis(id) {
	if(!IE){ //werkt niet in IE ???
		$('#' + id).animate({opacity: 0}, 1).animate({opacity: 1}, 500);
	}
}

function setHeightMenu(px){
	// 20100828 bvh // -26 = quick fix omdat de footer op de frontpage te laag uitkomt
	var minheight = px + 330;	
	//set hoogte menu
	$('#showMenu').css('height',px +'px');
	//set positie tipartikel
	var tipHeight = 250
	if($('#tiparticle').size()>0) {
		minheight += tipHeight
		$('#tiparticle').css('top', px + (tipHeight-40) + 'px');
		px += tipHeight
	}
	//set positie newsletter
	var newsHeight = 205
	if($('#newsletterblock').size()>0) {
		minheight += newsHeight
		$('#newsletterblock').css('top',px + newsHeight + 'px');
	}
	// 20110607 gps max plaatsen voor op de homepage
	if($('body.homepage').size()>0 && minheight > 752) minheight = 752;
	//set min height voor positie footer
	$('#content-minheight').css('height',minheight);
}

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 setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
}

function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";  
}

function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}

// 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);         
        }
    });
}

// Versie 1.3.2 (WdS)
function toggleCheckbox(id, show, hide) {       
    if ($('#' + id).attr('value') == ',1,') {
        $('#' + show).css('display', 'block');
        if (hide) $('#' + hide).css('display', 'none');
    } else {
        $('#' + show).css('display', 'none');
        if (hide) $('#' + hide).css('display', 'block');
    }
}

function RemoveViewedArticles(path) {
    $.get(path + 'xml/RemoveViewedArticles');
    $('#viewedarticles').html('');
}

// Versie 1.2 (JS)
// Versie 1.3.2 (WdS) #viewedarticles-' + id wordt verwijdert inplaats van leeggemaakt
function RemoveSingleViewedArticles(path, id) {
    $.get(path + 'xml/RemoveSingleViewedArticles?id=' + id);
    $('#viewedarticles-' + id).remove('');        
    if ($('#viewedarticles').html().indexOf('viewedarticles-') == -1) {
        $('#viewedarticles').html('');
    }
}

//functie voor verwijderen van alle artikelen uit wishlist via ajax
function RemoveWishlistArticles(path) {
    $.get(path + 'xml/RemoveWishlistArticles');
    $('#mywishlist').html('');
}

//functie voor verwijderen van een artikel uit wishlist via ajax
function RemoveSingleWishlistArticles(path, id) {
    $.get(path + 'xml/RemoveWishlistArticles?id=' + id);
    $('#wishlistarticles-' + id).remove('');        
    if ($('#mywishlist').html().indexOf('wishlistarticles-') == -1) {
        $('#mywishlist').html('');
    }
}

// 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
    }
	$('#' + htmlCont).html('<br /><br /><br /><img src="' + path + '../views/pipstudio/img/icon_loading.gif" alt="loading" />');
    $.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 AddRelatedArticleToOrder(path, id) {

	var specId = $('#specId-' + id).val();
	var amount = $('#amount-' + id).val();
	if ($('#spec1-' + id).val() == '0' || $('#spec2-' + id).val() == '0') {
		$('#marker_relatedArticles-1').html(tr_noSpecsSelectedInRelated);
		$('#marker_relatedArticles-2').html(tr_noSpecsSelectedInRelated);
		error = true;
		return false
	} else {
		$('.overlaymessage.shop').show();
		$.ajax({
			type: 'GET',
			url: path + 'xml/AddToShoppingCart',
			data: 'articleId=' + id + '&amount=' + amount + '&printingText=&specId=' + specId,
			success: function(html){
				$('#shoppingcart').html(html);
				shoppingcartAlert();
			}
		});
	}
}

//Versie 1.5 (WdS)
function UpdateSpecInShoppingcart(articleId, spec1, spec2, htmlCont, path, oldRelId, isCheckout) {
    var selectedSpec01, selectedSpec02
    if (document.getElementById(spec1)) {
        selectedSpec01 = document.getElementById(spec1).options[document.getElementById(spec1).selectedIndex].value
    } else {
        selectedSpec01 = 0
    }
    if (document.getElementById(spec2)) {
        selectedSpec02 = document.getElementById(spec2).options[document.getElementById(spec2).selectedIndex].value
    } else {
        selectedSpec02 = 0
    }

    try {
        $.ajax({
            type: 'GET',
            url: path + 'xml/ChangeSpecs',
            data: 'articleId=' + articleId + '&OldRelId=' + oldRelId + '&spec1=' + selectedSpec01 + '&spec2=' + selectedSpec02 + '&isCheckout=' + isCheckout,
            success: function(html) {
                $('#' + htmlCont).html(html);
            }
        });
    } catch (err) {
        alert("Er is iets foutgegaan. Probeer het opnieuw door deze pagina te herladen. " + err.message);
    }
}

var cntWishlistToOrder = 0
function orderWishlist(path){
	var count = $('[name=wishlist-order]').length;
	$('.overlaymessage.shop').show();
	$('[name=wishlist-order]').each(function(){
		var id = $(this).val();
		var specid = $('#' + id + '-wishlist-specid').val();
		$.ajax({
			type: 'GET',
			url: path + 'xml/AddToShoppingCart',
			data: 'articleId=' + id + '&amount=1&printingText=&specId=' + specid,
			success: function(html){
				cntWishlistToOrder += 1;
				if(cntWishlistToOrder == count) {
					$('#shoppingcart').html(html);
					shoppingcartAlert();
					cntWishlistToOrder = 0;
				}
			}
		});
	})
}

function viewURL(url){
	window.location = url;
}



//functie voor setten van 1 checkbox, geen komma's in value
//v = waarde true
//w = waarde false
function checkThis(v, w, id, input) {
	var obj = document.getElementById(input)
	if(obj.value.indexOf(v)==-1){
		obj.value=v;
		document.getElementById(id).src = document.getElementById(id).src.replace('.gif','_o.gif')
	}else{
		obj.value=w;
		document.getElementById(id).src = document.getElementById(id).src.replace('_o.gif','.gif')
	}
}

// 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.7 (JS)
function addCouponCode(val, path) {
	$('#error-coupon').hide();
	$.ajax({
		type: "POST",
		url: path + 'xml/AddCouponCode?typeid=2&code=' + val,
		data: {},
		cache: false,
		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: {},
		cache: false,
		dataType: "html",
		success: function(html) {
			if (html == 'True') {
				location.reload(true);
				
			} else {
				$('#error-coupon').show();
			}
		}
	});
}	

function gotoFilter(o, path){
	if($(o).val()!=''){
		location.href= path + $(o).val();
	}
}

function hyves_click(u, t) {
	//u = location.href;
	//t = document.title;
	//alert(t);

	window.open('http://www.hyves-share.nl/button/tip/?tipcategoryid=12&rating=5&title=' + encodeURIComponent(t) + '&body=' + encodeURIComponent(u), 'sharer', 'toolbar=0,status=0,width=626,height=500');
	return false;
}

function fbs_click(u, t) {
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u)+'&t='+encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
	return false;
}
function twitter_click(u, t, v) {
	window.open('http://twitter.com/share?url=' + encodeURIComponent(u)+'&text='+encodeURIComponent(t)+'&via='+encodeURIComponent(v), 'sharer', 'toolbar=0,status=0,width=626,height=436');
	return false;
}

