/**
 * @author Gproxy Design Inc.
 * @copyright (c) 2009, Gproxy Design Inc. All rights reserved.
 * @version 2.5
 *
 * 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. 
 */

/**
 * RECENTLY VIEWED ITEMS
 * Show the Recently Viewed Items
 * Recives the the site number and display for show the items 
 * @param {String} siteNumber
 */
function rviGetItems(siteNumber,display){
    var items = rviReadCookie('rvi_n' + siteNumber);		
	if (items != null && items != ''){
		$j("#rvi_cnt_items").hide();
		var src = '/app/site/hosting/scriptlet.nl?script=customscript_rvi_getitems&deploy=customdeploy_rvi_getitems&items=' + items + '&sitenumber=' + siteNumber + '&callback=?';
				
		$j.ajax({
			url: src,	      
			type: "GET",		
			dataType: "jsonp",
			success: function(data){
				if (data.Errors.length > 0) {  
					$j.each(data.Errors, function(i, val){	
						rviErrorManager('Show Items', val.code, val.details);										  		 			            
					});
				}else{					
					$j.each(data.Results, function(i, val){
						$j("#rvi_cnt_items").html(unescape(val.html));
					});	
					intCntItems = data.Items.length;
					$j.each(data.Items, function(i, val){					
						$j.ajax({
							url: unescape(val.url),	      
							type: "GET",		
							dataType: "html",
							success: function(htmlData){
								var strHtmlItemTpl = htmlData;
								/*
								<!--BEGIN_GPR_ITEMOPTIONS    END_GPR_ITEMOPTIONS-->
								<!--BEGIN_GPR_ADDTOCARTITEMID    END_GPR_ADDTOCARTITEMID-->
								<!--BEGIN_GPR_SALESPRICE    END_GPR_SALESPRICE-->
								<!--BEGIN_GPR_STKMESSAGE    END_GPR_STKMESSAGE-->
								<!--BEGIN_GPR_ADDTOCARTQTY    END_GPR_ADDTOCARTQTY-->
								<!--BEGIN_GPR_ADDTOCARTCLICKSCRIPT    END_GPR_ADDTOCARTCLICKSCRIPT-->
								<!--BEGIN_GPR_ADDTOCARTSUBMITSCRIPT    END_GPR_ADDTOCARTSUBMITSCRIPT-->
								*/							
								strStart = '<!--BEGIN_GPR_SALESPRICE';
								strEnd = 'END_GPR_SALESPRICE-->';
								intI = strHtmlItemTpl.indexOf(strStart);
								intF = strHtmlItemTpl.indexOf(strEnd);
								var price = '&nbsp;';
								if (intI != -1 && intF != -1) {
									intI += strStart.length;
									price = strHtmlItemTpl.substring(intI, intF);
								}
								$j("#rvi_price_"+val.internalid).html(unescape(price));
								
								strStart = '<!--BEGIN_GPR_STKMESSAGE';
								strEnd = 'END_GPR_STKMESSAGE-->';
								intI = strHtmlItemTpl.indexOf(strStart);
								intF = strHtmlItemTpl.indexOf(strEnd);
								var stkmessage = '&nbsp;';
								if (intI != -1 && intF != -1) {
									intI += strStart.length;
									stkmessage = strHtmlItemTpl.substring(intI, intF);
								}   
								$j("#rvi_stkmessage_"+val.internalid).html(unescape(stkmessage));													
								
							},		
							complete: function (XMLHttpRequest, textStatus) {				  			
								intCntItems--;
								if(intCntItems <= 0){
									$j("#rvi_cnt_items").fadeIn(1000);	
								}
							},
							error: function (XMLHttpRequest, textStatus, errorThrown) {
								rviErrorManager('Get Item Info', textStatus, errorThrown);										
							}		  
						});					 	
					});
					/*if (intCntItems > 0){
						$j.get( document.location , function() {
							});
						}		*/	        			      	
				}
			},		
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				wlpErrorManager('Show Items', textStatus, errorThrown);														
			}		  
		});
	}    
}

/**
 * RECENTLY VIEWED ITEMS
 * Save the current item in the Recently Viewed Items
 * Recives the item internal Id
 * @param {String} itemId
 * @param {String} siteNumber
 */
function rviSaveItemCookie(itemId, siteNumber){  
    var rvi_items = '';
	var cookieValue = '';
	var exists = false;
    if (itemId != "") {
        var cookieValue = rviReadCookie('rvi_n' + siteNumber);
        if (cookieValue == null) {
			rvi_items = itemId;
        }else {
			items = cookieValue.split(',');
			for (var i=0; i<items.length; i++) {
				if (itemId == items[i]) {
					exists = true;
					break;				
				}	
			}			
			if (!exists){
				rvi_items = itemId + "," + cookieValue;	
			}else {
				rvi_items = cookieValue;
			}		            
        }                    
        rviCreateCookie('rvi_n' + siteNumber, rvi_items, 10);        
    }
}

/**
 * COOKIES
 * Creates a new cookie in the browser
 * Recives the name, the value and when expires the cookie
 * @param {Object} name
 * @param {Object} value
 * @param {Object} days
 */
function rviCreateCookie(name, value, days){
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else 
        var expires = "";
    document.cookie = name + "=" + escape(value) + expires + "; path=/";
}

/**
 * COOKIES
 * Reads a cookie
 * Recives the name for the cookie
 * @param {Object} name
 */
function rviReadCookie(name){
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(name + "=");
        if (c_start != -1) {
            c_start = c_start + name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

/**
 * COOKIES
 * Erase a cookie
 * Recives the name for the cookie
 * @param {Object} name
 */
function rviEraseCookie(name){
    rviCreateCookie(name, "", 10);
}

function rviErrorManager(strSource, strCode, strDetails) {
	$j("#rvi_cnt_msgerror").append("<li>Recently Viewed Items Error, " + strSource + ", code: " + unescape(strCode) + ", details: " + unescape(strDetails) + "</li>");	
}
