webshop={
    debug:false,
    //debug:true,
    callBack:function(){
        alert('No callback defined!');
    },
    setCallBack:function(callBack){
        webshop.callBack=callBack;
    },
    
    getWizardResultContent:function(categoryId, filters, step, sis, active, page){
        var requestUrl = webshop.buildRequestUrl('get' + sis + 'WizardResultResponse');

        jQuery.getJSON(requestUrl, {categoryId:categoryId, filters:filters, step:step, active:active, page: (!page ? 1 : page)}, webshop.checkError);

        webshop.showWaitBox();
    },

    getWizardContent:function(categoryId, filters, step, sis){
        var requestUrl = webshop.buildRequestUrl('get' + sis + 'WizardResponse');

        jQuery.getJSON(requestUrl, {categoryId:categoryId, filters:filters, step:step}, webshop.checkError);

        webshop.showWaitBox();
    },

    worksuggestSend:function(work_id, from_name, from_email, to_name, to_email, comments){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('worksuggest');

        jQuery.getJSON(requestUrl, {work_id:work_id, from_name:from_name, from_email:from_email, to_name:to_name, to_email:to_email, comments:comments}, webshop.checkError);

        webshop.showWaitBox();

    }, //}}}
    
    worktypesuggestSend:function(work_id, from_name, from_email, to_name, to_email, comments){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('worktypesuggest');

        jQuery.getJSON(requestUrl, {work_id:work_id, from_name:from_name, from_email:from_email, to_name:to_name, to_email:to_email, comments:comments}, webshop.checkError);

        webshop.showWaitBox();

    }, //}}}
	
    explanationSend:function(user_name, phone){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('explanation');

        jQuery.getJSON(requestUrl, {user_name:user_name,phone:phone}, webshop.checkError);

        webshop.showWaitBox();

    }, //}}}

    getCategoryProducts:function(url, categoryId, menuId){
        var requestUrl = webshop.buildRequestUrl('getCategoryProducts');

        jQuery.getJSON(requestUrl, {url:url, categoryId:categoryId, menuId:menuId}, webshop.checkError);

        webshop.showWaitBox();
    },

    saveCustomerAccount:function(customer_account){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('saveCustomerAccount');

        jQuery.getJSON(requestUrl, {customer_account_id:customer_account['customer_account_id'],first_name:customer_account['first_name'],middle_name:customer_account['middle_name'],last_name:customer_account['last_name'],gender:customer_account['gender'],email:customer_account['email'],phone:customer_account['phone'],customer_account_type_id:customer_account['customer_account_type_id'],order_amount:customer_account['order_amount']}, webshop.checkError);

        webshop.showWaitBox();

    },

    getCustomerAccount:function(customer_account_id){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('getCustomerAccount');

        jQuery.getJSON(requestUrl, {customer_account_id:customer_account_id}, webshop.checkError);

        webshop.showWaitBox();

    },

    getAddress:function(address_id){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('getAddress');

        jQuery.getJSON(requestUrl, {address_id:address_id}, webshop.checkError);

        webshop.showWaitBox();

    },
    getProject:function(project_id){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('getProject');

        jQuery.getJSON(requestUrl, {project_id:project_id}, webshop.checkError);

        webshop.showWaitBox();

    },
    addItem:function(productVariationId,quantity,details){

        // Set defaults
        if (!quantity) quantity = 1;
        if (!details) details = '';

        // Build request url
        var requestUrl = webshop.buildRequestUrl('addItem');

        jQuery.getJSON(requestUrl, {productVariationId:productVariationId,quantity:quantity,details:details}, webshop.checkError);

        webshop.showWaitBox();
    },
    setItemQuantity: function(itemId,quantity){

        // Set defaults
        //if (!quantity) quantity = 1;

        // Build request url
        var requestUrl = webshop.buildRequestUrl('setItemQuantity');

        jQuery.getJSON(requestUrl, {itemId:itemId,quantity:quantity}, webshop.checkError);

        webshop.showWaitBox();

    },
    processOrder: function(){//{{{

        // Build request url
        var requestUrl = webshop.buildRequestUrl('processOrder');

        // Send request
        new Ajax.Request(
            requestUrl,
            {
                onComplete: webshop.checkError
            }
            );
        webshop.showWaitBox();
    }, //}}}
    editOrderCustomFields: function(orderID,customFields){//{{{

        // Build request url
        var requestUrl = webshop.buildRequestUrl('editOrderCustomFields');

        // Build query string with custom fields as array
        if (0 < customFields.length){
            var queryStringArray = new Array;
            queryStringArray.push('orderID='+orderID);
            for (i=0;i<customFields.length;i++) {
                queryStringArray.push(
                    escape('customFields['+customFields[i][0]+']')
                    +'='+escape(customFields[i][1]));
            }
            var queryString = '?'+queryStringArray.join('&');
        } else {
            var queryString = '';
        }

        // Send request
        new Ajax.Request(
            requestUrl+queryString
            );

    }, //}}}
    prepareOrder: function(){//{{{

        // Build request url
        var requestUrl = webshop.buildRequestUrl('prepareOrder');

        // Alert debugging info
        /*if (webshop.debug) {
            alert('Sending AJAX request to: '+requestUrl);
            document.getElementsByTagName('body')[0].innerHTML += requestUrl;
        }*/

        // Send request
        new Ajax.Request(
            requestUrl,
            {
                onComplete: webshop.checkError
            }
            );
        webshop.showWaitBox();
    }, //}}}

    addFavoriteProducts:function(productid){
        var requestUrl = webshop.buildRequestUrl('addToFavorite');
		jQuery('#ajax_results').html('Working...');

        jQuery.getJSON(requestUrl, {identifier:productid}, webshop.checkError);

        webshop.showWaitBox();
    },

    favoriteListLogin:function(email,password,remember){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('login');

        jQuery.getJSON(requestUrl, {email:email,password:password,remember:remember}, webshop.checkError);

        webshop.showWaitBox();

    }, //}}}
    getFavoriteLists:function(){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('getFavoriteLists');

        jQuery.getJSON(requestUrl, webshop.checkError);

        webshop.showWaitBox();
    }, //}}}
    favoriteListSave:function(name,list_id){

        // Build request url
        var requestUrl = webshop.buildRequestUrl('manageFavoriteList');

        jQuery.getJSON(requestUrl, {name:name,list_id:list_id}, webshop.checkError);

        webshop.showWaitBox();
    }, //}}}
    loadSummary: function(){},
    buildRequestUrl: function(action){//{{{
        var machineHash = (Cookie.get('m')
            ? Cookie.get('m')
            : -1
            );
        var userHash = (Cookie.get('u')
            ? Cookie.get('u')
            : -1
            );
        var requestUrl = '/json/'+action
            +'/m/'+escape(machineHash)
            +'/u/'+escape(userHash)
            ;
        return requestUrl;
    }, //}}}
    checkError: function(jsonResponse){//{{{
        webshop.hideWaitBox();
        //alert(ajaxRequest.responseText);
        //alert("wwww" + jsonResponse);
        /*if (ajaxRequest.responseText.indexOf('{') != 0) {
            // Add response text to body if any
            document.getElementsByTagName('body')[0].innerHTML +=
                '<hr />'+ajaxRequest.responseText;

            // Run callback function
            if (webshop.debug) {
                alert('Running callback function...');
            }
            webshop.callBack(jsonResponse.responseData);

        } else */if (0 != jsonResponse.errorCode) {
            // AJAX request generated an error. Alert it
            /*alert('Error code '+jsonResponse.errorCode
                +'\n'+jsonResponse.errorString);*/

            alert(jsonResponse.errorString);

        } else {

            // Run callback function
            if (webshop.debug) {
                alert('Running callback function...');
            }
            webshop.callback(jsonResponse.responseData);

        }
    }, //}}}
    waitBoxTimeout: 100,
    
    hideWaitBox: function() {//{{{
			jQuery('#TB_load').remove();
			if ( !document.getElementById('TB_window') || document.getElementById('TB_window').innerHTML == '' )
			{
				jQuery('#TB_overlay').remove();
			}
			else
			{
				jQuery('#TB_overlay').css({zIndex: 100});
			}
		}, //}}}
		
		
    showWaitBox: function(){//{{{
			jQuery("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
			jQuery('#TB_load').css({zIndex: 200});
			if ( !document.getElementById('TB_overlay') ) {
				jQuery("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
    	}
			jQuery('#TB_overlay').css({zIndex: 150});
			if(tb_detectMacXFF()){
				jQuery("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
			}else{
				jQuery("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
			}
      setTimeout('webshop.displayWaitBox()', webshop.waitBoxTimeout);
      return;
    }, //}}}
    
    
    displayWaitBox: function(){//{{{
			//alert(jQuery('#TB_load'));
			jQuery('#TB_load').show();
    } //}}}
}

// Cookie extension
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};