var main = {

    baseUrl : '',
    sessionID : '',
    ajaxLoaderImage: '',
    shoppingBagAjaxUrl: '',
    ajaxLoaderMessage: '',

    initialize:function(baseUrl, sessionID){

        main.baseUrl    = baseUrl;
        main.sessionID  = sessionID;

	// The url we're sending our ajax requests to
	main.shoppingBagAjaxHandler = main.baseUrl + 'site/shop/inc/shoppingbag_ajax.php?';

	// The loader image for ajax requests.
	main.ajaxLoaderImage = '<img src="' + main.baseUrl + '/js/ajax-loader.gif"/>';

	// The loader message for ajax requests.
	main.ajaxLoaderMessage = 'Een moment geduld alsjeblieft<br /><br />';

        setTimeout("main.updateShoppingBag();", 30000);

        jQuery('#close_promo').click(main.hidePromo);

        //jQuery('#sizeChart').click(main.showSizeChart);

    },

    showSizeChart: function(){
        jQuery('#sizeChart').modal({overlayClose:true});
    },

    hidePromo: function(){
      jQuery('#topPromotion').hide('slow');
      jQuery.get( main.baseUrl + 'site/shop/inc/ajax_functions.php?function=hidePromo');
    },

    check: function(){
        alert('loaded');
    },

    addOneToShoppingBag:function(reservationID){

	if(reservationID != ''){
	    main.changeShoppingBagAmount(reservationID, 'add');
	}

    },

    removeOneFromShoppingBag:function(reservationID){

	if(reservationID != ''){
	    main.changeShoppingBagAmount(reservationID, 'remove');
	}

    },

    changeShoppingBagAmount:function(reservationID,type){

	// The shoppingcart url that we'll reload into the container.
	var shoppingBagUrl = main.baseUrl + '/site/shop/inc/productContainer.php';

	// The url to send the ajax request to plus the querystring.
	var url = main.shoppingBagAjaxHandler + 'function=changeShoppingBagAmount&type=' + type + '&reservationID=' + reservationID + '&rnd=' + Math.random()*99999  + '&sessionID=' + main.sessionID;

	// Element to be added as a loader.
	var ajaxLoader = '<div id="shoppingBagAjaxLoader">' + main.ajaxLoaderMessage + main.ajaxLoaderImage + '</div>';

	new Ajax.Request(url,
	  {
	    method:'get',

	    // When the ajax request starts lets show a loading message and icon
	    onCreate: function(){

		$('productContainerProducts').update();
		$('productContainerProducts').update(ajaxLoader);

	    },

	    // When the ajax function is succesfull start doing stuff.
	    onSuccess: function(transport){

		// Place the json result into a var to evaluate the results.
		var json = transport.responseText.evalJSON();

		if(json.result != 'ok'){
		    alert(json.message)
		}

		window.location.href = main.baseUrl + '/shop/shoppingbag';

	    }

	  });
    },

    deleteFromShoppingBag:function(reservationID){
        
        var url = main.baseUrl + 'site/shop/inc/removeItemFromShoppingBag.php?&item=' + reservationID + '&rnd=' + Math.random()*99999  + '&sessionID=' + main.sessionID;

        jQuery.getJSON(url, function(data) {
            if(data.result != 'ok'){
                alert(data.message);
            } else {
                window.location.href = main.baseUrl + '/shop/shoppingbag';
            }
        });

    },

    addToCart: function(){

	var queryString = $('shoppingBagForm').serialize();

	// The shoppingcart url that we'll reload into the container.
	var shoppingBagUrl = main.baseUrl + '/site/shop/inc/productContainer.php';

	// The url to send the ajax request to plus the querystring serialized from the form.
	var url = main.shoppingBagAjaxHandler + 'function=addToCart&' + queryString  + '&rnd=' + Math.random()*99999  + '&sessionID=' + main.sessionID;

	var linkElement = document.getElementById('addToCartLink');

	new Ajax.Request(url,
	  {
	    method:'get',

	    // When the ajax request starts lets remove the onclick
	    onCreate: function(){
		// Remove the onclick event so noone gets to click like crazy
		linkElement.onclick = '';
	    },

	    // When the ajax request is succesfull start handling stuff.
	    onSuccess: function(transport){

		// Place the json result into a var to evaluate the results.
		var json = transport.responseText.evalJSON();

		if(json.result != 'ok'){
		    alert(json.message);
		} else {
		    // Modalbox.show("/site/shop/popup/shoppingbag.php", {title: "Shopping Bag",width: 960,slideDownDuration: 0.1,slideUpDuration: 0.1});
                    window.location.href = main.baseUrl + '/shop/shoppingbag';
		}

		linkElement.onclick = main.addToCart;

	    },

	    // When theres a failure return the onclick to the link.
	    onFailure: function(){
		linkElement.onclick = main.addToCart;
	    }

	  });
    },

    loadZoom: function(productArticleNumber,productName,productImagePath){

	// Do we have a productArticleNumber?
	if(productArticleNumber != ''){

	    // Do we have a productName?
	    if(productName != ''){

		// Do we have a productImagePath?
		if(productImagePath != ''){

		    //Lets load the tooltip.
		    new Tip('zoom' + productArticleNumber,'<div style="padding-top:5px;margin-top:5px;"><img alt="' + productName + '" src="' + productImagePath + '" width="300" height="400" /></div>',
		      {
		       title: productName,
		       width: 'auto',
		       className: 'imageZoom'
		      });

		}
	    }
	}
    },

    servicePointLocator: function(){

        document.getElementById('kialaFrame').style.display = "none";
        document.getElementById('kialaInfo2').style.display = "none";

	var selectedServicePointID = $('dhlSelectedServicePointID').getValue();

        
	if(selectedServicePointID != ''){
	    $('dhlDeliveryAddress').show();
	}
	var status  = 'ok';
	var message = 'Er is nog geen adres ingevuld in je shopVIP account. Dit is nodig om gebruik te maken van DHL servicepoints.\n\n Wil je nu een adres toevoegen?';

	if(($('selectedAddressID') != undefined)){

	    var selectedAddressID = $('selectedAddressID').getValue();

	    if(selectedAddressID != ''){

		var town    = $('dhltown' + selectedAddressID).getValue();
		var street  = $('dhlstreet' + selectedAddressID).getValue();
		var zip	= $('dhlzip' + selectedAddressID).getValue();
		var country = 'NL';

		// The url to send the ajax request to plus the querystring serialized from the form.
		var url = main.baseUrl + 'zend/public/dhl/getservicepoints/country/' + country + '/town/' + town + '/street/' + street + '/zip/' + zip;


		// Reload the complete shoppingcart in the modal box container.
		new Ajax.Updater('dhl', url, {

		    // When the ajax request starts lets remove the onclick
		    onCreate: function(){
			$('dhlLoader').show();
		    },

		    onSuccess:function(){
			$('dhl').show();
			$('dhlLoader').hide();
		    }


		});

	    } else {
		status = 'error';		
	    }

	} else {
	    status = 'error';
	}

	if(status == 'error'){

	    if(confirm(message)){
		window.location.href= main.baseUrl + 'shop/checkout';
	    }
	}

    },

    getServicePointDetails: function(servicePointID){

	var town    = $('dsptown').getValue();
	var street  = $('dspstreet').getValue();
	var zip	    = $('dspzip').getValue();
	var country = $('dspcountry').getValue();
	var page    = $('dsppage').getValue();

	// The url to send the ajax request to plus the querystring serialized from the form.
	var url = main.baseUrl + 'zend/public/dhl/getservicepointdetails/servicePointID/' + servicePointID + '/country/' + country + '/town/' + town + '/street/' + street + '/zip/' + zip + '/page/' + page;

	// Reload the complete shoppingcart in the modal box container.
	new Ajax.Updater('dhl', url, {

 	    // When the ajax request starts lets remove the onclick
	    onCreate: function(){
		$('dhlLoaderMessage').update('Een moment geduld a.u.b het servicepoint wordt opgehaald.');
		$('dhlLoaderContainer').show();
	    },

	    onSuccess:function(){
		//alert('finished');
	    }


	});

    },

    getServicePointsByPage: function(page){

	var town    = $('dsptown').getValue();
	var street  = $('dspstreet').getValue();
	var zip	    = $('dspzip').getValue();
	var country = $('dspcountry').getValue();

	// The url to send the ajax request to plus the querystring serialized from the form.
	var url = main.baseUrl + 'zend/public/dhl/getservicepoints/country/' + country + '/town/' + town + '/street/' + street + '/zip/' + zip + '/page/' + page;

	// Reload the complete shoppingcart in the modal box container.
	new Ajax.Updater('dhl', url, {

 	    // When the ajax request starts lets remove the onclick
	    onCreate: function(){
		$('dhlLoaderMessage').update('Een moment geduld a.u.b de servicepoints worden opgehaald.');
		$('dhlLoaderContainer').show();
	    },

	    onSuccess:function(){
		//alert('finished');
	    }


	});

    },

    searchServicePoints: function(){

	var town    = $('dsptown').getValue();
	var street  = $('dspstreet').getValue();
	var zip	    = $('dspzip').getValue();
	var country = $('dspcountry').getValue();

	// The url to send the ajax request to plus the querystring serialized from the form.
	var url = main.baseUrl + 'zend/public/dhl/getservicepoints/country/' + country + '/town/' + town + '/street/' + street + '/zip/' + zip;

	// Reload the complete shoppingcart in the modal box container.
	new Ajax.Updater('dhl', url, {

 	    // When the ajax request starts lets remove the onclick
	    onCreate: function(){
		$('dhlLoaderMessage').update('Een moment geduld a.u.b de servicepoints worden gezocht');
		$('dhlLoaderContainer').show();
	    },

	    onSuccess:function(){
	    }

	});
    },

    selectServicePoint: function(servicePointID){

	$('dhl').hide();

	// The url to send the ajax request to plus the querystring serialized from the form.
	var url = main.baseUrl + 'zend/public/dhl/selectservicepoint/servicePointID/' + servicePointID;
	new Ajax.Request(url,{method:'get'});

	$('spName').update($('name' + servicePointID).getValue() + ' ' + $('servicepointid' + servicePointID).getValue());
	$('spStreet').update($('street' + servicePointID).getValue());
	$('spTown').update($('zip' + servicePointID).getValue() + ' ' + $('town' + servicePointID).getValue());

	$('dhlSelectedName').setValue($('name' + servicePointID).getValue());
	$('dhlSelectedServicePointID').setValue($('servicepointid' + servicePointID).getValue());
	$('dhlSelectedStreet').setValue($('street' + servicePointID).getValue());
	$('dhlSelectedTown').setValue($('town' + servicePointID).getValue());
	$('dhlSelectedZip').setValue($('zip' + servicePointID).getValue());
	

	$('dhlDeliveryAddress').show();

    },

    unselectDhlServicePoint: function(){
	$('dhl').hide();
	$('dhlDeliveryAddress').hide();
    },
    
    showTakeOverAddress: function(){
        $('takeOverAddress').show();
    },

    setSelectedAddress: function(addressID){

	$('selectedAddressID').setValue(addressID);

	if($('dhl').visible()){

	    if(confirm('Je hebt het DHL servicepoint venster geopend wil je de servicepoint lijst opnieuw opvragen?')){
		main.servicePointLocator();
	    }
	}

	


    },

    updateShoppingBag: function(){
	var url = main.baseUrl + '/site/shop/inc/shoppingbagAmount.php';

	new Ajax.Updater('shoppingBag', url, {

 	    // When the ajax request starts lets remove the onclick
	    onCreate: function(){

	    },

	    onSuccess:function(){

	    }


	});

        setTimeout("main.updateShoppingBag();", 30000);
    },



    setStatusMessage: function(step){
        jQuery('#step1, #step2, #step3, #step4, #step5').removeClass("active");
        var note = "";
        switch(step){
            case 1:
                note = '<span style="font-weight:bold;">1. Bevestiging</span><br />Per e-mail ontvang je een orderbevestiging.';
            break;
            case 2:
                note = '<span style="font-weight:bold;">2. Einde Sale</span><br />Wanneer de Sale is afgelopen, bestellen wij je artikelen bij onze leverancier. Vervolgens verzendt de leverancier de producten naar ons distributiecentrum.';
            break;
            case 3:
                note = '<span style="font-weight:bold;">3. Levering bij distributiecentrum</span><br />De producten zijn aangekomen bij het distributiecentrum. De order wordt ingepakt en binnenkort aangeboden aan de vervoerder.';
            break;
            case 4:
                note = '<span style="font-weight:bold;">4. Verzending</span><br />Je order wordt verzonden naar het door jou opgegeven adres. Dit kan je thuisadres zijn, je werkadres of het dichtstbijzijnde DHL-afhaalpunt.';
            break;
            case 5:
                note = '<span style="font-weight:bold;">5. Ontvangst</span><br />Je ontvangt je order op het door jou opgegeven adres, of haalt het op bij het afgesproken DHL-punt.';
            break;
            default:
                note = '<span style="font-weight:bold; color:#000;">Informatie over de levering van je bestelling</span><br /><span style="color: #000;">Beweeg je muis over de verschillende stappen voor meer informatie</span>';
            break;
        }
        // set note
        jQuery('#statusBox #note').html(note);
        // set step to active
        jQuery('#step'+step).addClass("active");
    }

};


