﻿/*********************************************
Copyright 2009 Solid Cactus/Web.com inc.
Last Updated: 8/7/2009
Customized: N
packer: http://dean.edwards.name/packer/
*********************************************/
// Start Features List JS/Plugins
var STORE_DOMAIN="wcamlforum.org";

// Start JCookie
jQuery.cookie=function(key,value,options){if(arguments.length>1&&(value===null||typeof value!=="object")){options=jQuery.extend({},options);if(value===null){options.expires=-1;}
if(typeof options.expires==='number'){var days=options.expires,t=options.expires=new Date();t.setDate(t.getDate()+days);}
return(document.cookie=[encodeURIComponent(key),'=',options.raw?String(value):encodeURIComponent(String(value)),options.expires?'; expires='+options.expires.toUTCString():'',options.path?'; path='+options.path:'',options.domain?'; domain='+options.domain:'',options.secure?'; secure':''].join(''));}
options=value||{};var result,decode=options.raw?function(s){return s;}:decodeURIComponent;return(result=new RegExp('(?:^|; )'+encodeURIComponent(key)+'=([^;]*)').exec(document.cookie))?decode(result[1]):null;};
// End JCookie

// Start Inscriptions

var splitter1="!~!";
var splitter2="#~#";

var sReg1=/\!\~\!/gi;
var sReg2=/\#\~\#/gi;

function placeCurrentCookie() {
    var v="";
    $("input.instext").each(function(i) {
       
        v=v + (v!="" ? splitter1 : "") + this.id + splitter2 + this.getAttribute("iname") + splitter2 + (this.value!=""?this.value:"NULL") + splitter2 + currentCode;       
        
    });
    
    //alert(v);
    
    $.cookie("current_inscription",v,{expires:7,domain:STORE_DOMAIN,path:'/'});
}

function updateInsPrice1(cartid, myelement,insname){
    if($("#"+myelement).val() != ""){
		$("option[value='"+myelement+"']").attr("selected","selected");	
		//$.cookie("ins:"+myelement+":"+insname,$("#"+myelement).val(),{expires:7,domain:'loveandcomfort.com',path:'/'});
	} else {
		$("option[value='"+myelement+"']").attr("selected",false);
		//$.cookie("ins:"+myelement+":"+insname,$("#"+myelement).val(),{expires:7,domain:'loveandcomfort.com',path:"/"});
	};
	var v=$.cookie("ins_" + cartid);
    var a=v.split(sReg1);
    var ccc="";
    for (var i=0;i<a.length;i++) {
        var b=a[i].split(sReg2);
        if (b[0]==myelement) {
            n=b[0] + splitter2 + b[1] + splitter2 + $("#"+myelement).val() + splitter2 + b[3];
        } else {
            n=a[i];
        }
        ccc+=(ccc=="" ? "" : splitter1) + n;
    }
	$.cookie("ins_" + cartid,ccc,{expires:7,domain:STORE_DOMAIN,path:'/'});
}

function updateInsPrice(myelement){

    insname=document.getElementById(myelement).getAttribute("iname");

	if($("#"+myelement).val() != ""){
		$("option[value='"+myelement+"']").attr("selected","selected");	
		//$.cookie("ins:"+myelement+":"+insname,$("#"+myelement).val(),{expires:7,domain:'loveandcomfort.com',path:'/'});
	} else {
		$("option[value='"+myelement+"']").attr("selected",false);
		//$.cookie("ins:"+myelement+":"+insname,$("#"+myelement).val(),{expires:7,domain:'loveandcomfort.com',path:"/"});
	};
}

function get_cookies_array() {
    var cookies = { };
    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
        }
    }
    return cookies;
}

// Start Simple Text Popup
(function($){
	$.fn.scSimpleText = function(options){
		var defaults = {
			popCloseTxt : ''
		};
		var settings = $.extend({},defaults,options);
		return this.each(function(){  
			var nextElm = $(this).next();
			var closeElement = $(document.createElement("a")).attr("href","").text(settings.popCloseTxt).addClass("scSimpleTextClose").click(function(e){nextElm.fadeOut("slow"); e.preventDefault();});
			$(this).click(function(e){	
				nextElm.fadeIn("slow");
				nextElm.append(closeElement);
				e.preventDefault();
			});
		});
	}
})(jQuery);
// End Simple Text Popup

/*
 * jQuery SPEmail plug-in 1.0
 * (c) 2008 Richard Chiriboga
 * Inspired by David Walsh's blog on spam proofing emails using moo tools.
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * HOW TO USE
 
 THE DOM
	example 1: $('a[rel*=spemail]').spemail('|,:','linkbase');
	example 2: $('#email').spemail('|,:','mailbase');
	example 3: $('#plainemail').spemail('|,:','plainbase');
 
 THE PAGE CODE
 	Using Example 1: <a href="yourname|yourcompany:com" rel="spemail"></a>
	Using Example 2: <span id="email">yourname|yourcompany:net</span>	
	Using Example 3: <span id="plainemail">yourname|yourcompany:net</span>
 *
 *
 *
*/
$.fn.spemail = function(opt,type){
	// Added this to be able to change what the options by default the | will be used as the first parameter an initiate the '@' and the : will initiate the '.'
	var opt_in = opt.split(",");
	var type;
	
	if (type == "linkbase")
	{
		this.each(function() {
			el = $(this);
			el.each(function(){
			// Finds the href with rel: 'whatever you want' and inserts mailto: and replaces the characters for real ones.
			// Done this way to keep consistancy throughout the emails on the site.
			el.attr('href','mailto:' + el.attr('href').replace(opt_in[0],'@').replace(opt_in[1],'.'));
			// Will add a title attribute to the link like: Email: whoever@whereever.com
			el.attr('title',el.attr('href').replace(opt_in[0],'@').replace(opt_in[1],'.').replace('mailto.','Email: '));
			// Will change the html of the link itself to the email address of the person.
			el.html(el.attr('href').replace(opt_in[0],'@').replace(opt_in[1],'.').replace('mailto.','').replace('mailto:',''));
			});
		});
	}
	else if(type == "mailbase") {
		this.each(function() {
			el = $(this);
			el.each(function(){
		    // Will create the whole href link with title and everything for the text provided. (probably the best option - to me at least
			el.html('<a href="mailto:'+el.html().replace(opt_in[0],'@').replace(opt_in[1],'.')+'" title="Email: '+el.html().replace(opt_in[0],'@').replace(opt_in[1],'.')+'">'+el.html().replace(opt_in[0],'@').replace(opt_in[1],'.')+'</a>');
			});
		});
	}
	else if(type == "plainbase") {
		// WILL JUST CHANGE TEXT TO THE EMAIL ON DOM REGULAT TEXT TO REGULAR TEXT	
		this.each(function() {
			el = $(this);
			el.each(function(){
			el.html(el.html().replace(opt_in[0],'@').replace(opt_in[1],'.'));
			});
		});
	}
};

/**********************************************************************************************************/
/****************************** Place all add on JS above here, please minify *****************************/
/**********************************************************************************************************/

/************************************* Document on Load jQuery Functions **********************************/

jQuery(document).ready(function() {
		// add a class to the last li in the header and footerlinks.
		$("ul#secondary-links li:last").addClass("last");$("ul#primary-links li:last").addClass("last");$("ul#primary-links").removeAttr("id");$("ul#secondary-links").removeAttr("id");
		// Hide Empty containers and headers (previous element)
		$(".checkempty").each(function(i){var $thisele = $(this);if($.trim($thisele.text()).length < 1){$thisele.prev().hide();}});
		// Hide empty product info table rows
		$("#product-info-table td").each(function(i){var $thisele = $(this);if($.trim($thisele.text()).length < 1){$thisele.parent().hide();}});
		// write seperators for contents.
		var numconts = 3; // change this with number of columns
		$(".results-grid").find('.result').each(function(c){var thisresult = $(this);if((c + 1)%numconts!=0){thisresult.addClass("vertsep");} else {thisresult.parent().after("<div class='horzsep'><div></div><div></div><div></div></div>");}});
		$("#homefeatured").find('.product').each(function(c){var thisresult = $(this);if((c + 1)%numconts!=0){thisresult.addClass("vertsep");} else {thisresult.parent().after("<div class='horzsep'></div>");}});
		$("div.horzsep:last").addClass("last");
		// remove styles from image on item page.
		$("#product_image").removeAttr("style");
		// simple text popup
		$("#simpleTextLink").scSimpleText({popClose:".simpleTextRight"});
		// If product description is empty, add default text
		if ($(".itemcontain").length != 0){
		    if ($("div.description").html().length == 0){
               $("div.description").html("Product Description is currently unavailable");
            }
        }
		$('a[rel*=spemail]').spemail('|,:','linkbase');
		$('#email').spemail('|,:','mailbase');
		$('#plainemail').spemail('|,:','plainbase');
		
		$(".addtocart .action input").click(function() {
		    placeCurrentCookie();
		});
		
		if($(".option-choices option").length > 0){
	        $(".option-choices option").each(function(opnum){
	            insopt = $(this).attr('value'); 
	            if(insopt != ""){
	               findins = $(this).text().split("-",2);
	               insname=$(this).parent().parent().prev().text();
	               isins = findins[0].indexOf('inscription');
	               if(isins > -1){
		              // is an inscription
		              inslen = findins[0].split(":",2);
	               buildins = "<input type='text' maxlength='"+inslen[1]+"' class='instext' id='"+insopt+"' name='insopt' onChange=updateInsPrice('"+insopt+"') iname=\"" + insname + "\" />";
	               if(findins[1]){buildins += " " + findins[1]}
		              $(this).parent().parent().append(buildins);
		              $(this).parent()[0].selectedIndex=0;
		              $(this).parent().hide();
	               }
	            }
	        });
        }
  
  if($("#cart-contents").length > 0){
    var ccc=$.cookie("current_inscription");
    if (ccc!=null && ccc!=undefined) {
    
         var a=ccc.split(sReg1);
         var lastID=$("tr.row-item:last");
         if (lastID.length>0) {
            
            var cartid=$(".cell-quantity input:hidden",lastID).val();
            
            $.cookie("ins_" + cartid,ccc,{expires:7,domain:STORE_DOMAIN,path:'/'});
         }
    }
    var cookies = get_cookies_array();
    $.cookie("current_inscription",null,{expires:7,domain:STORE_DOMAIN,path:'/'});
	for(var name in cookies) {
	  if(name.substr(0,4)=="ins_"){
		 var cartid=name.substring(4);
         $("tr.row-option input:hidden[value=\"" + cartid + "\"]").each(function() {
            var v=$.cookie(name);
            var a=v.split(sReg1);
            for (var i=0;i<a.length;i++) {
                var b=a[i].split(sReg2);
                myins=$("option[value='"+b[0]+"']",this.parentNode.parentNode);
        	    if (myins.length>0) {
        	    
        	        if (b[2]!=null && b[2]!="" && b[2]!="NULL") {
        		        myins.parent().hide();
	        	        myins.parent().parent().append("- "+b[2]);
	        	    } else {
	        	    
	        	        var s=myins[0].innerHTML;
	        	        
	        	        var ts=s.split(":");
	        	    
	        	        var maxlength=ts[1];
	        	        var insopt=b[0];
	        	        var insname=b[1];
	        	        
	        	        var t="";
	        	        
	        	        var ttt=myins.text();
	        	        var findins=ttt.split("-");
	        	        
	        	        if(findins[1]){
	        	            t = " " + findins[1];
	        	        }
	        	        
                        var buildins = "<input type='text' maxlength='"+maxlength+"' class='instext' id='"+insopt+"' name='insopt' onChange=updateInsPrice1('" + cartid + "','"+insopt+"','"+insname+"') iname=\"" + insname + "\" />" + t;
	        	        myins.parent().parent().append(buildins);
	        	        myins.parent().hide();
	        	        //$(this.parentNode.parentNode).hide();
	        	    }
	        	    break;
                }
            }
         });
	  }
	}
	for(var name in cookies) {
	  if(name.substr(0,4)=="ins_"){
	    var cartid=name.substring(4);
	    if ($("tr.row-item input:hidden[value=\"" + cartid + "\"]").length==0) {
	        $.cookie(name,null,{expires:7,domain:STORE_DOMAIN,path:'/'});
	    }
	  }
	}
	
  }
  
  if($("#ctl00_ctl00_mainContent_scPageContent_commentsControl_commentsTextBox").length > 0){
	var cookies = get_cookies_array();
	var t="";
	for(var name in cookies) {
	  //alert( name + " : " + cookies[name] + "<br />" );
	  
	  if(name.substr(0,4)=="ins_"){
	    var v=$.cookie(name);
	    
	    if (v!=null) {
	        var a=v.split(sReg1);
            
            t=t + a[0].split(sReg2)[3] + "\r\n";
            
            for (var i=0;i<a.length;i++) {
                var b=a[i].split(sReg2);
                t=t + b[1] + "=" + b[2] + "\r\n";
            }  
            
            t=t + "\r\n";
        }
	  }
	}	
    $("#ctl00_ctl00_mainContent_scPageContent_commentsControl_commentsTextBox").text(t);
  }


});
