/**
 * 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.abcdimmer = (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 css_link = jq('<link rel="stylesheet" type="text/css" href="http://cdn.optabc.com/css/abc_style.css" />').appendTo('head'),
				// abc_settings
				settings = {"retailer":{"bg_color":"#000000","domain":"brucegweber.com","logo":"false","logo_position":null,"name":"Bruce G Weber","opacity":"1","text_color":"#ffffff","url_prefix":"brucegweber","web_id":"13f3cf8c531952d72e5847c4183e6910"},"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}}},
				// elements used
				wrapper = jq('<div id="abc_wrapper"></div>'),
				dimmer = jq('<div id="abc_dimmer"></div>'),
				frame = jq('<div id="abc_frame"></div>'),
				logo = jq('<div id="abc_logo"></div>');
				
				jq('a.opt_abc').bind('click', function(e) {
					
					var _this = jq(this),
							abc_id = this.id,
							abc_url = (settings.retailer.url_prefix !== '') ? settings.retailer.url_prefix + '.' + settings.brands[abc_id].url : settings.brands[abc_id].url;
					
					wrapper.prependTo('body').css({
						'background-color': settings.retailer.bg_color
					}).hide().click(function() {
						dimmer.fadeOut('fast', function() {
							dimmer.remove();
						});
						wrapper.fadeOut('fast', function() {
							wrapper.remove();
						});
					});
					
					dimmer.prependTo('body').css('background-color', settings.retailer.bg_color).animate({
						opacity: settings.retailer.opacity
					});
					
					frame.css({
						'height': settings.brands[abc_id].height,
						'width': settings.brands[abc_id].width,
						'margin-left': settings.brands[abc_id].width / -2,
						'margin-top': settings.brands[abc_id].height / -2,
						'background-color': 'transparent',
						'text-align': 'center'
					});
					
					frame.prependTo(wrapper).append(
						'<iframe id="' + abc_id + '_iframe"' +
						'src="http://' + abc_url + '/" ' +
						'height="' + settings.brands[abc_id].height + '" ' +
						'width="' + settings.brands[abc_id].width + '" ' +
						'scrolling="no" frameborder="0" allowtransparency="true" style="background: transparent !important;"></iframe>');
						
					// IE6 Fixed Position, Fix
					if (jq.browser.msie && jq.browser.version.substr(0,1) < 7) {	
						dimmer.height(jq(document).height());
						wrapper.css({
							'height': jq(window).height(),
							'top': jq(window).scrollTop() + 'px'
						});
						jq(window).resize(function() {
							wrapper.height(jq(window).height());
						});
						jq(window).scroll(function() {
							wrapper.css('top', jq(window).scrollTop() + 'px');
						});
					}	
						
					jq('iframe#' + abc_id + '_iframe').load(function() {
						wrapper.css('background-color', 'transparent').fadeIn('fast');
						// if (!self.get_position(logo)) {
						// 	$(logo).css({ opacity: '0' });
						// }
					});		
					
					return false;
					
				});
				
			});
			
		},
		
		// -------------------------------
		
		/**
		 * 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();
		},
		
		/**
		 * 
		 */
		get_position: function(e) {
			var os = e.offset();
			return (os.top < 10) ? false : true;	
		}
		
	};
	
	opt.init();

	return opt;

})();
