/**
 * @author Gproxy Design Inc.
 * @copyright (c) 2011, Gproxy Design Inc. All rights reserved.
 * @version 1.0
 *
 * Gproxy doesn't allow to copy or change this code without Gproxy authorization.
 * See http://www.gproxy.com/licenses/license01.pdf for the full license governing this code.
 */


var GPR_PUP = function($){
    var objOptions = {
        fade: 500,
		winTimeOut: 7000
    };    
    return {
        /**
         * POP UP MESSAGGES
         * Init
         * @param {Object} objOptions
         */
        init: function(obj){
            if (obj !== null && obj !== undefined) {
                $.extend(objOptions, obj);
            }
        },
        /**
         * POP UP MESSAGGES
         * Show
         * @param {String} strMsg
         */
        show: function(strMsg){
            $('.pup-win').remove();
            $('body').append('<div class="pup-win">' + strMsg + '</div>');
            $('.pup-win').fadeTo(0, 0);
            $('.pup-win').append('<div class="pup-close">X Close</div>');
            $('.pup-close');
            $('.pup-win').fadeTo(objOptions.fade, 1);
            $('.pup-close').click(function(){
                $('.pup-win').fadeTo(objOptions.fade, 0, function(){
                    $('.pup-win').remove();
                });
            });
            setTimeout(function(){
                $('.pup-win').fadeTo(objOptions.fade, 0, function(){
                    $('.pup-win').remove();
                });
            }, objOptions.winTimeOut);
        }
    }
}(jQuery);

var GPR_COOKIES = function($){    
    return {        
        /**
         * COOKIES
         * Create a cookie 
         * @param {String} strName
         * @param {String} strValue
         * @param {Integer} intDays
         */
        create: function(strName, strValue, intDays){
            var strExpires = "";
	        if (intDays) {
	            var dteDate = new Date();
	            dteDate.setTime(dteDate.getTime() + (intDays * 24 * 60 * 60 * 1000));
	            strExpires = "; expires=" + dteDate.toGMTString();
	        }        
	        document.cookie = strName + "=" + escape(strValue) + strExpires + "; path=/";
        },
		/**
         * COOKIES
         * Read a cookie 
         * @param {String} strName       
         */
		read: function(strName){
			var strStart = "", strEnd = "";
	        if (document.cookie.length > 0) {
	            strStart = document.cookie.indexOf(strName + "=");
	            if (strStart != -1) {
	                strStart = strStart + strName.length + 1;
	                strEnd = document.cookie.indexOf(";", strStart);
	                if (strEnd == -1) { 
	                    strEnd = document.cookie.length;
	                }
	                return unescape(document.cookie.substring(strStart, strEnd));
	            }
	        }
	        return null;
		},
		/**
         * COOKIES
         * Erase a cookie 
         * @param {String} strName       
         */
		erase: function(strName){
			this.create(strName,"",-1);
		}
    }
}(jQuery);

var GPR_OPTIONS = function($){
    var objOptions = {        
		loginURL: document.location,
		cartURL: document.location,
		checkoutURL: document.location,
        siteNumber: 1,
        customerId: "",        
        companyId: ""
    };   
	
    return {
        /**
         * POP UP MESSAGGES
         * Init
         * @param {Object} objOptions
         */
        init: function(obj){
            if (obj !== null && obj !== undefined) {
                $.extend(objOptions, obj);
            }
        },
		        
        options: function(){
        	return objOptions;    
        }
    }
	
}(jQuery);

var GPR_AJAX_TOOLS = function($){
    var objOptions = {
		loadingImgURL: ""		
    };    
    return {
        /**
         * AJAX TOOLS
         * Init
         * @param {Object} objOptions
         */
        init: function(obj){
            if (obj !== null && obj !== undefined) {
                $.extend(objOptions, obj);
            }
        },
        /**
         * AJAX TOOLS
         * Starts Loading ajax request
         * @param {String} strCntId
         * @param {String} strMsg
         */
        startLoading: function(strCntId, strMsg){
            $("#"+strCntId+" .gpr-loading").remove();
			$("#"+strCntId+" .gpr-errors").remove();			
            $("#"+strCntId).append('<div class="gpr-loading"><img src="' + objOptions.loadingImgURL +'"/><span>' + strMsg + '</span></div>');            
        },
		
		/**
         * AJAX TOOLS
         * Show ajax request errors
         * @param {String} strCntId         
         */
		stopLoading: function(strCntId){
			$("#"+strCntId+" .gpr-loading").remove();
            $("#"+strCntId+" .gpr-errors").remove();
        },
		
		/**
         * AJAX TOOLS
         * Show ajax request errors
         * @param {String} strCntId
         * @param {String} strSource
         * @param {String} strCode
         * @param {String} strDetails
         */
		showError: function(strCntId, strSource, strCode, strDetails){
			$("#"+strCntId+" .gpr-loading").remove();
            $("#"+strCntId+" .gpr-errors").remove();			
			$("#"+strCntId).append('<div class="gpr-errors">'+ strSource + ", code: " + unescape(strCode) + ", details: " + unescape(strDetails) +'</div>');
        }	
    }
}(jQuery);

var ReferralProgram = function($){
	
    var objOptions = {
        exchangeCreditsURL: "/app/site/hosting/scriptlet.nl?script=customscript_tn_ss_rwprg_exchangecredits&deploy=customdeploy_tn_ss_rwprg_exchangecredits",
		showInfoCntId: "rep_info",       
        msgs: ["There was an error, please contact the site administrator... ", 
        "There was an error, please contact the site administrator...", 
        "There was an error, please contact the site administrator...",
        "You have 0 credits in your account",
        "You discount coupon code was sent successfuly, check your email box."]
    };
	    
    function showPopUp(strMsg){       
        GPR_PUP.show(strMsg);
    };
    
    return {        
        init: function(obj){
            if (obj !== null && obj !== undefined) {
                $.extend(objOptions, obj);
            }
			if (GPR_OPTIONS.options().customerId == "") {
				GPR_COOKIES.create("_tn_rwp_url", document.location.href);	
				window.location.href = GPR_OPTIONS.options().loginURL;		
			}			
        },        
        exchangeCredits: function(){           
            var strParams = {
                sitenumber: GPR_OPTIONS.options().siteNumber,
                customerid: escape(GPR_OPTIONS.options().customerId)                    
            };
            $.ajax({
                url: objOptions.exchangeCreditsURL + "&callback=?",
                type: "GET",
                dataType: "jsonp",
                data: strParams,
                success: function(json){
                    if (json.Errors.length > 0) {
                        $.each(json.Errors, function(i, val){
                            GPR_AJAX_TOOLS.showError(objOptions.showInfoCntId,'Exchange Credits', val.code, val.details);
                        });
                    }
                    else {
                        showPopUp(objOptions.msgs[json.Results.msgcode]);
                    }
                },
                beforeSend: function(XMLHttpRequest){
                    GPR_AJAX_TOOLS.startLoading(objOptions.showInfoCntId,'Exchanging...');
                },
                complete: function(XMLHttpRequest, textStatus){
					GPR_AJAX_TOOLS.stopLoading(objOptions.showInfoCntId);                        
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    GPR_AJAX_TOOLS.showError(objOptions.showInfoCntId,'Exchange Credits', textStatus, errorThrown);
                }
            });            
        }
    };
}(jQuery);
