
// opt_abc 
// Dimmer script

(function() {
	var jQuery;
	if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
		var jquery_tag = document.createElement('script');
		jquery_tag.setAttribute("type","text/javascript");
		jquery_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
		jquery_tag.onload = script_load_handler;
		jquery_tag.onreadystatechange = function () {
			if (this.readyState == 'complete' || this.readyState == 'loaded') {
				script_load_handler();
			}
		};
		(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(jquery_tag);
	} 
	else {
		jQuery = window.jQuery;
		opt_abc();
	}
	function script_load_handler() {
		jQuery = window.jQuery.noConflict(true);
		opt_abc();
	}
	function get_position(e) {
		os = e.offset();
		return (os.top < 10) ? false : true;	
	}
	function opt_abc() { 
		
		jQuery(document).ready(function($) { 
				
				var css_link = $("<link>", { 
			    rel: "stylesheet", 
			    type: "text/css", 
			    href: "http://cdn.optabc.com/css/abc_style.css" 
			});
			
			css_link.appendTo('head');

			
			jQuery.fn._opt_abc_dimmer = function() {
			
				this.each(function(i) {
					
					var abc_link = $(this);
					var abc = abc_link.attr('id');
					var abc_settings = {"retailer":{"web_id":"","name":"","domain":"","logo":"","logo_position":null,"text_color":"","bg_color":"","url_prefix":"","opacity":""},"brands":{"cartier":{"display_id":"cartier","name":"Cartier","url":"cdn.optabc.com/external/cartier/index.html","height":533,"width":980}}};
						
					abc_link.click(function() {
						
						if (abc in abc_settings.brands) {
							
							var wrapper = $('<div id="_abc_wrapper"></div>');
							var dimmer = $('<div id="_abc_dimmer"></div>');
							var frame = $('<div id="_abc_frame"></div>');
							var logo = $('<div id="_abc_logo"></div>');
							
							var expand_height = (abc_settings.retailer.logo === '') ? 86 : 0;
							//var expand_height = (abc_settings.retailer.logo === 'true') ? 86 : 0; // change back
							var abc_url = (abc_settings.retailer.url_prefix !== '') ? abc_settings.retailer.url_prefix + abc_settings.brands[abc].url : abc_settings.brands[abc].url;
							//alert(abc_url);
							wrapper.prependTo('body').css({
								'background-color': '#000'
							}).hide().click(function() {
								dimmer.fadeOut('fast', function() {
									dimmer.remove();
								});
								wrapper.fadeOut('fast', function() {
									wrapper.remove();
								});
							});
							
							/*dimmer.prependTo('body').css('background-color', '#000').animate({
								opacity: 0.9
							});*/
							
							dimmer.prependTo('body').css('background-color', '#000').animate({
								opacity: 0.8
							});
							
							frame.css({
								'height': abc_settings.brands[abc].height + expand_height,
								'width': abc_settings.brands[abc].width,
								'margin-left': abc_settings.brands[abc].width / -2,
								'margin-top': (abc_settings.brands[abc].height + expand_height) / -2,
								'background-color': 'transparent',
								'text-align': 'center'
							});
							
							frame.prependTo(wrapper);
							
							if (expand_height > 0) {
								logo.appendTo(frame).css({
									'background-image': 'url(http://cdn.optabc.com/images/logos/' + abc_settings.retailer.web_id + '.png)',
									'background-position': 'center',
									'background-repeat': 'no-repeat'
								});
							}
							frame.append(
								'<iframe id="' + abc + '_iframe"' +
								'src="http://' + abc_url + '" ' +
								'height="' + abc_settings.brands[abc].height + '" ' +
								'width="' + abc_settings.brands[abc].width + '" ' +
								'scrolling="no" frameborder="0" allowtransparency="true" style="background: transparent !important;"></iframe>');
								
							// IE6 Fixed Position, Fix
							if ($.browser.msie && $.browser.version.substr(0,1) < 7) {	
								dimmer.height($(document).height());
								wrapper.css({
									'height': $(window).height(),
									'top': $(window).scrollTop() + 'px'
								});

								$(window).resize(function() {
									wrapper.height($(window).height());
								});

								$(window).scroll(function() {
									wrapper.css('top', $(window).scrollTop() + 'px');
								});
							}	
								
							$('iframe#' + abc + '_iframe').load(function() {
								wrapper.css('background-color', 'transparent').fadeIn('fast');
								
								if (!get_position(logo)) {
									$(logo).css({ opacity: '0' });
								}
								/*$(window).resize(function() {
									if (get_position(frame)) {
										$(logo).animate({ height: '50px' });
										$(frame).animate({ height: abc_settings.brands[abc].height + expand_height });
									} 
									else {
										$(logo).animate({ height: '1px' });
										$(frame).animate({ height: abc_settings.brands[abc].height });
									}
								});*/
							});
						}
						return false;
					}); 
				});
			}
			$('a.opt_dimmer')._opt_abc_dimmer();
		});
	}
})();
