// JavaScript Document
/**
 * Script: eci-js.js
 * Description: This file is the root file for every ECI extended js file
 */

var Eci = Eci || {};

/**
 * Extends the current object with the parameter. Works recursively.
 */
Eci.extend = function( obj ) {
  for ( var i in obj ) {
    if ( this[i] ) {
        Eci.extend.apply( this[i], obj[i] );
    } else {
        this[i] = obj[i];
    }
  }
};


Eci.extend({
    trim: function( text ) {
        return ( text || "" ).replace( /\s+|\s+$/g, "" );
    },
    
    stripHtml: function( html ) {
        return ( html || "" ).replace( /<\S[^>]*>/g, "" );
    }   
});

/**
 * Manipulates cookie
 * @param name string
 * @param value string
 * @param options object
 * 
 */
Eci.cookie = function( name, value, options ) {
    if ( typeof value != "undefined" ) { //Write Cookie
        var options = options || {};
        if ( value === null ) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if ( options.expires && ( typeof options.expires == "number" || options.expires.toUTCString ) ) {
            var date;
            if ( typeof options.expires == "number" ) {
                date = new Date();
                date.setTime( date.getTime() + ( options.expires * 24 * 60 * 60 * 1000 ) );
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        
        var path = options.path ? ";path=" + (options.path) : "";
        var domain = options.domain ? ";domain=" + (options.domain) : "";
        var secure = options.secure ? ";secure" : "";
        document.cookie = [name, "=", encodeURIComponent(value), expires, path, domain, secure].join("");
    } else { //Get Cookie
        var cookieValue = null;
        if ( document.cookie && document.cookie != ""  ) {
            var cookies = document.cookie.split( ";" );
            for ( var i=0; i < cookies.length; i++ ) {
                var cookie = Eci.trim( cookies[i] );
                if ( cookie.substring( 0, name.length +1 ) == ( name + "=" )  ) {
                    cookieValue = decodeURIComponent( cookie.substring( name.length + 1 ) );
                    break;
                }
            }
        }
        return cookieValue;
    }
};


/**
 * Description: Handles with recently viewd items
 * Stores the recently navigated pages to the cookies
 * @param json string 
 */
Eci.setView = function( item ) { // the type of item should be json string
    var alreadyExist = false;
    
    if( Eci.cookie('RecentView') == null || Eci.cookie('RecentView') == "" ) {
        var data = '{products:[' + item + ']}';
        Eci.cookie( 'RecentView', data );
    } else {
        var itemsString = Eci.cookie('RecentView');
        
        var o_items = eval("(" + itemsString + ")");
        var o_item =  eval("(" + item + ")");
        
        //check if the item is already exist
        for ( var i=0; i < o_items.products.length; i++ ) {
            if( o_items.products[i].productid == o_item.productid ) { //already exist
                alreadyExist = true;
                break;
            }
        }
        
        if( alreadyExist == false ) { // add new item
            var temp = itemsString.split(']');
            var data = temp[0] + ',' + item + ']' + temp[1];
            Eci.cookie( 'RecentView', data );
        }
    }
}

/**
 * Outputs the recently viewed items
 */

Eci.RecentView = function() {
    if( Eci.cookie('RecentView') == null || Eci.cookie('RecentView') == "" ) {
        return; // do nothing
    } else {
        var items = eval("(" + Eci.cookie('RecentView') + ")");

        var content = '<div style="width:600px;margin-top:20px;text-align:center;background-color:#ffffff;font-family:Verdana,Tahoma,Arial;font-size:12px;">';
        content += '<div><h3 style="maring:0;padding:0; background-color:#ededed">Recently Viewed Items</h3></div>';
        
        for ( var i= (items.products.length -1) ; i >= 0 && ((items.products.length-1 -i) <= 2 ); i--) {
            // design your own style here
            content += '<div style="float:left;width:190px;border-right: 1px solid #DDDDDD;">';
            content += '<div><img src="'+ items.products[i].img + '" border="0" width="120" height="100" /></div>';
            content += '<div><a href="'+  items.products[i].productid.toLowerCase() + '.html">'+ items.products[i].name + '</a></div>';            
			content += '<div id="recentlyviewed-price">Cell Phone Retail Store Price: <s>'+ items.products[i].price + '</s></b></div>';			
			if( parseFloat(items.products[i].saleprice.replace("$", "")) > 0 )  
			content += '<div id="recentlyviewed-saleprice"><b><i>Tax Relief Sale price:</i></b><span> '+ items.products[i].saleprice + '</span></div>';			
           content += '</div>';
        }
        content += '</div>'
        document.write(content);
    }
}
