
/**
 * Base script for all implementation options.
 * The only thing that should be changed from script to script 
 * is the abc_init method.
 */
var opt = opt || {};

opt.abciframe = (function() {

	var jq,
	// SETTINGS
	jq_version = '1.6.2', // minimum jquery version
	cookie_expire = 1, // number of months the cookie is valid for
	cookie_length = 32, // length of cookie ID
	cookie_name = 'ECOM_COOKIE', // name of client cookie
	
	// -------------------------------
	
	script, cookie_id = false, wait, page_hash,

	opt = {
		
		/**
		 * Instantiate the implementation method.
		 * @returns {void} 
		 */		
		init_abc: function() {
			
			var self = this;

			jq(function() {
				
				// var view_cart = jq('a.view_cart'),
				// 		cart_url = view_cart.attr('href'),
				// 		local = window.location.href;
				// 			
				// if (view_cart.length && (cart_url.match(/sessid=([A-Za-z0-9])/gi) === null || cart_url.match(/sessid=/gi) === null)) {
				// 	if (cart_url.match(/sessid=/gi) === null) {
				// 		view_cart.attr('href', cart_url + '&sessid=' + self.cookie_id);
				// 	} 
				// 	else {
				// 		view_cart.attr('href', cart_url.replace('&sessid=', '&sessid=' + self.cookie_id));
				// 	}
				// }	
				// 
				// view_cart.attr('href', view_cart.attr('href') + '&continueShoppingLink=' + local.replace(/&/gi,'and;'));
				
				// --------------------------
				
				// Apply the ABC to the container.
				
				jq('div.opt_abc').each(function() {
				
					var _this = jq(this), abc_id = this.id, url, iframe,
							settings = {"retailer":{"bg_color":"#000000","domain":"diamondcellar.com","logo":"false","logo_position":"center","name":"Diamond Cellar","opacity":"1","text_color":"#ffffff","url_prefix":"diamondcellar","web_id":"eed5af6add95a9a6f1252739b1ad8c24"},"brands":{"mikimoto":{"display_id":"mikimoto","name":"Mikimoto","url":"shopmikimoto.com","height":535,"width":805},"kwiat":{"display_id":"kwiat","name":"Kwiat","url":"shopkwiat.com","height":535,"width":805},"roberto_coin":{"display_id":"roberto_coin","name":"Roberto Coin","url":"shoprobertocoin.com","height":550,"width":904},"marco_bicego":{"display_id":"marco_bicego","name":"Marco Bicego","url":"shopmarcobicego.com","height":544,"width":896},"pennypreville":{"display_id":"pennypreville","name":"Penny Preville","url":"shoppennypreville.com","height":545,"width":805},"christopher_designs":{"display_id":"christopher_designs","name":"Christopher Designs","url":"shopchristopherdesigns.com","height":545,"width":805},"precision_set":{"display_id":"precision_set","name":"Precision Set","url":"shopprecisionset.com","height":545,"width":805}}};
						
					if (abc_id in settings.brands) {
						url = settings.retailer.url_prefix + '.' + settings.brands[abc_id].url + '/index.jsp?';
						url = url + 'sessid=' + self.cookie_id + self.call_hash();
						_this.height(settings.brands[abc_id].height + 40).width(settings.brands[abc_id].width);
						_this.append('<iframe id="' + abc_id + '_iframe"' + 'src="http://' + url + '" ' + 'height="' + settings.brands[abc_id].height + '" ' + 'width="' + settings.brands[abc_id].width + '" ' + 'scrolling="no" frameborder="0" allowtransparency="true" style="background: transparent !important;"></iframe>');
					}
				
				});
				
			});
			
		},
		
		// -------------------------------
		
		/**
		 * Instantiates the script.
		 * @returns {void} 
		 */
		init: function() {
			var self = this;
			this.set_cookie();
			this.load_jquery();
			wait = setInterval(function() {
				if (jq) {
					self.init_abc(); 
					clearInterval(wait);
				}
			}, 150);
		},
		
		// -------------------------------
		
		/**
		 * Gets the cookie_id
		 * @returns {mixed} If present returns a string, otherwise false.
		 */
		get_cookie: function(name) {
			if (document.cookie.indexOf('ECOM_COOKIE') !== -1) {
				return document.cookie.match(/ECOM_COOKIE=([0-9,A-Z,a-z]+)/gi).toString().split('=')[1];
			} else { return false; }
		},
		
		// -------------------------------
		
		/**
		 * Sets a cookie if not already set.
		 * @returns {void} 
		 */
		set_cookie: function() {
			this.cookie_id = this.get_cookie();
			if (!this.cookie_id) {
				var date = new Date();
				date.setMonth(date.getMonth() + cookie_expire);
				document.cookie = 'ECOM_COOKIE=' + this.gen_token() + '; expires=' + date.toGMTString() + ';';
			}
		},
		
		// -------------------------------
		
		/**
		 * Generates a random token to be used as a cookie id.
		 * @returns {string} 64 character random token ([0-9,A-Z,a-z])
		 */
		gen_token: function() {

			var char_pool = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz',
					token_length = cookie_length,
					token = new String();

			for (var i = 0, len = char_pool.length; i < token_length; i++) {
				var rand = Math.floor(Math.random() * len);
				token += char_pool.substring(rand, rand + 1);
			}

			return token;

		},
		
		// -------------------------------
		
		/**
		 * If jQuery does not exist, apply it to the page.
		 * @returns {void} 
		 */
		load_jquery: function() {
			var _this = this;
			script = document.createElement('script');
			script.setAttribute('type','text/javascript');
			script.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/' + jq_version + '/jquery.min.js');
			script.onload = this.jquery_loadhandler;
			script.onreadystatechange = function () {
				if (this.readyState == 'complete' || this.readyState == 'loaded') {
					_this.jquery_loadhandler();
				}
			};
			(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(script);
		},
		
		// -------------------------------
		
		/**
		 * 
		 */
		jquery_loadhandler: function() {
			jq = (window.jQuery === undefined || window.jQuery.fn.jquery !== jq_version)
							? window.jQuery
							: window.jQuery.noConflict(true);
		},
		
		// -------------------------------
		
		/**
		 * 
		 */
		call_hash: function() {
			if (window.location.hash && window.location.hash !== '#') {
				return '&' + decodeURI(window.location.hash.split('#')[1].toString().replace(/&sessid=([A-Za-z0-9]+)/gi, ''));
			}
			return new String();
		}
		
	};
	
	opt.init();

	return opt;

})();
