/**
* @fileoverview ebiz.js: A module used for client specific functionality
*
* This module defines a single symbol named "Venda.Ebiz"
* all ebiz utility functions are stored as properties of this namespace
* functions that are spacific this site shoudl be added to this file only.
*/

//Declare namespace for ebiz
Venda.namespace("Ebiz");

/**
* Split a string so it can be displayed on multiple lines so it does not break display layout - used on order confirmation and order receipt page
* @param {string} strToSplit string that needs to be split 
* @param {Integer} rowLen length of row which will hold the string
* @param {string} displayElem the html container which will display the splitted string
*/
Venda.Ebiz.splitString = function(strToSplit, rowLen, dispElem) {
var stringlist = new Array();
while (strToSplit.length > rowLen) {
stringlist.push( strToSplit.slice(0,rowLen));
strToSplit=strToSplit.substr(rowLen);
}
if (strToSplit.length) {
stringlist.push(strToSplit);
}
document.getElementById(dispElem).innerHTML = stringlist.join('<br>');
};

/**
* A skeleton function for validating user extened fields - needs to be amended by the build team
* @param {object} frmObj HTML form containing user extended field elements
*/
Venda.Ebiz.validateUserExtendedFields = function(frmObj) {
	if(frmObj) {
	/*
		if ( (frmObj.usxtexample1.checked==false) && (frmObj.usxtexample2.checked==false) && (frmObj.usxtexample3.checked==false))  {	
			alert("Please tick at least one checkbox");
			return false;
		}*/
		return true;		
	} 
	return false;
};

//Declare namespace for ebiz
var ebiz = Venda.namespace("Ebiz");
/**
 * example function below
 * ebiz.myFunctionname = function(param1, param2) {
 *	..... Place function code here ....
 * }; 
 */

//2. declare function
ebiz.getIt = function (p1) {
	return p1 + 'TV.';
};

ebiz.clearfloat=function(){
	$(".price").each(function(){
		if($(this).html().match(/\.\d\d+/)){
			var temp = $(this).html().replace(/\.\d\d+/,"");
			$(this).html(temp);
		}
   });
};

$(document).ready(function() {
	setInterval(function(){ ebiz.clearfloat(); }, 100);
	$("#matra").attr("checked", "checked");
 });

ebiz.populate_loyaltypoints = function( divPrefix,forceUpdate ) {
        var curr_time = parseInt(new Date().getTime() / 1000);
        var age    = parseInt(document.getElementById( divPrefix + '_age' ).innerHTML);
        var points = parseInt(document.getElementById( divPrefix + '_current' ).innerHTML);
        var refresh_secs = parseInt(document.getElementById( divPrefix + '_refresh' ).innerHTML);
        if (!isNaN(points) && (age + refresh_secs > curr_time) && !forceUpdate) {
           document.getElementById(divPrefix).innerHTML = points;
        } else {
           var url = '/bin/venda?ex=co_wizr-loyaltypoints&bsref=purehmv&'+divPrefix+'_age='+curr_time;
           ajaxFunction(url, divPrefix); 
        }
};

/**
 * @fileoverview Venda.Widget.RecentlyViewedStories - cookie based recently viewed story.
 *
 * This widget enables us to set a cookie each time a user lands on the story detail.  
 * The script also uses the ajax function to write items to the page.
 * @author Dan Brook <dbrook@venda.com>
 * implement to use for story rvi
 * @editor Bombh <bombh@venda.com>
 */

//create namespace
Venda.namespace("Widget.RecentlyViewedStories");

//declare constants here
Venda.Widget.RecentlyViewedStories.SEPARATOR   = ',';
Venda.Widget.RecentlyViewedStories.COOKIE_NAME = 'RVISTORY';

//declare object property
Venda.Widget.RecentlyViewedStories.check = 1;

/**
 * calls the cookie and then splits it to be displayed in the saveRecentlyViewedStories
 * @params {string} set rviCookie name and separate it using the SEPARATOR
 */
Venda.Widget.RecentlyViewedStories.getRecentlyViewedStories = function (rviCookie) {
  return rviCookie.split(Venda.Widget.RecentlyViewedStories.SEPARATOR);
};
/**
 * @constructor
 * @class get the item and set the cookie with name and value.
 * @requires CookieJar						/venda-support/js/external/cookiejar.js
 * @param {string} get the item name (story sku)
 * @param {string} cj is the instance cookieJar to set cookie
 * @param {string} numberofrvi is the amount of rvi to be displayed in the cookie
 */
Venda.Widget.RecentlyViewedStories.saveRecentlyViewedStories = function (item, cj, numberofrvi) {
  var cookieVal = '';
  var rviCookie = cj.get(Venda.Widget.RecentlyViewedStories.COOKIE_NAME);
		var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
  if(rviCookie) {
    var rvi = Venda.Widget.RecentlyViewedStories.getRecentlyViewedStories(rviCookie);
    // Don't need to set the cookie if this item is already in the list.
    for(var i = 0; i < rvi.length; i++)
      if(rvi[i] == item)
        return;
    rvi.unshift(item);
    cookieVal += rvi.slice(0,numberofrvi).join(Venda.Widget.RecentlyViewedStories.SEPARATOR);
  } else
    cookieVal += item;
		/**
		 * update the cookie
		 */
  return cj.put(Venda.Widget.RecentlyViewedStories.COOKIE_NAME, cookieVal);
};

/**
 * the following function is called on the story detail
 * template to set the item into the cookie
 * @param {string} invtref is the story sku reference
 * @param {string} numberofrvi are the amount of RVI's being set
 */
Venda.Widget.RecentlyViewedStories.setRecentlyViewedStories = function (invtref,numberofrvi) {
	var cj = new CookieJar({expires: 3600 * 24 * 7, path: '/'});
	var rviCookie = cj.get(Venda.Widget.RecentlyViewedStories.COOKIE_NAME);
		if(rviCookie) {
		var storyArray = Venda.Widget.RecentlyViewedStories.getRecentlyViewedStories(rviCookie);
		for(var i =0;i < storyArray.length;i++){
			var li = document.createElement('li');
			jQuery("#rvistorylist").append(li);
			li.id = 'rviStory-'+i;
			jQuery("#"+li.id).load("/stry/"+storyArray[i]+"&layout=blank&temp=story_rvi");
		}
		//call Slider after populating the story is done
		rviSlider.init();
		
	 }
	 //call selected tab function
	selectedTab();
	var tabView = new YAHOO.widget.TabView('contentinformationtab');
	Venda.Widget.RecentlyViewedStories.saveRecentlyViewedStories(invtref, cj,numberofrvi);
};

/*
 function sorting alphabet index for story
*/
 Venda.Ebiz.sortIndexCollate = function(indexArray){
	if(indexArray.length > 0){
		indexArray.sort(value);
		  var sortIndexArray = new Array();
			for(var i =0;i<indexArray.length;i++){
				sortIndexArray.push(indexArray[i][1]);
			}
			if(sortIndexArray.length > 0 && collateTarget!=""){
				jQuery(collateTarget.hideelement).css("display","none");
				jQuery(collateTarget.insertelement).html(sortIndexArray.join(""));
			};
	};
	function value(a,b) {
		a = a[0];
		b = b[0];
		return a == b ? 0 : (a < b ? -1 : 1);
	};
 };
 
 //add class to story locayta
 jQuery(document).ready(function(){
	jQuery("div.searchstoryresults div.prods ul li:even").addClass("even");
	jQuery("div.searchstoryresults div.prods ul li:odd").addClass("odd");
	jQuery("#col-one #storyrefinelist #collate .contstxtcontentindex p.collatedresult div:last").addClass("last");	
 });