// avoid firebug console errors
if(window.console && window.console.firebug) { } // firebug is running
else {console = { log: function(m) {  } }; }

//////////////////////////////////////////////////////////////////////////////////////////////////// 
// modal
////////////////////////////////////////////////////////////////////////////////////////////////////

var page = {
////////////////////////////////////////////////////////////////////////////////////////////////////
	// load up the silo specific js file
	load_js: function(ws, callback) {
		var ts = silo[ws], tsi = undefined;
		if (ts) { tsi = silo[ws].i; }
		if (!tsi) {
			if (isArray(ws)) { for (l = 0, L = ws.length; l < L; l++) { this.load_js(ws[l]); } }
			else {
				$.ajax({
					type: "GET",
					url: "/shop/js/" + ws + to_min + ".js" + (js_version[ws] ? '?v=' + js_version[ws] : ''),
					success: function() { if (silo[ws]) { silo[ws].i = true; } if (callback) { eval(callback); } },
					dataType: "script",
					cache: (js_version[ws] ? true : false)
				});
			}
		}

	}
}

// check to see if the browser supports border-radius CSS
$(function() {
	$.support.borderRadius = false;
	$.each(['BorderRadius','MozBorderRadius','WebkitBorderRadius','OBorderRadius','KhtmlBorderRadius'], function() {
		if(document.body.style[this] !== undefined) $.support.borderRadius = true;
		return (!$.support.borderRadius);
	});
});






var modal = {
	////////////////////////////////////////////////////////////////////////////////////////////////////
	init: function() {
		this.$_b = $("body"), this.opac_low = { border: 0 }, this.opac_med = { border: 0 }, this.opac_high = { border: 0 };
		if (!$.browser.msie) { this.opac_low.opacity = '0.1'; this.opac_med.opacity = '0.3';  this.opac_high.opacity = '1.0'; }	
		this.borderRadius();
		this.width = 902;
		this.height = 569;
		$('.modalable').click(function() { modal.launch($(this).attr('href'),$(this).attr('data-modal')); return false; });
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////
	borderRadius: function() { $.support.borderRadius ? this.css3 = true : this.css3 = false; },
	////////////////////////////////////////////////////////////////////////////////////////////////////
	launch: function(source, options) { if (typeof options == 'string') { options = $.parseJSON(options); } this.source = source; if (!options) { options = {}; } this.options = options; this.zoom(); },
	////////////////////////////////////////////////////////////////////////////////////////////////////
	clear: function() { this.$_inner.html(''); },
	////////////////////////////////////////////////////////////////////////////////////////////////////
	close: function() {
			this.blanket.hide(), this.inner.hide();
			
			// if the div had been dynamically altered, save the altered state back to the div incase the modal is relaunched
			if ($(this.source).length > 0 && this.options.update_original) { $(source).html(modal.$.inner.html()); }
			delete this.source;
			this.options = {};
			var r = ['inner','box','x'];
			if (this.$_ex_box) { r.push('$_ex_box'); }
			this.rem(r);
			this.$={};
	},
	$: {},
	options: {},
	rem: function(i) { for(ii=0, II=i.length; ii < II; ii++) { if (modal.$) { if (modal.$[i[ii]]) { modal.$[i[ii]].remove(); } } } },
	wW: function() { return ((window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth)) / 2) - 29; },
	sH: function() { return (window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight)) / 2; },
	wH: function() { return $(window).scrollTop(); },
	toW: function() { var w = modal.wW() - (modal.width / 2); if (w < 20) { w = 20; } return w; },
	toH: function() { var h = (modal.wH() + modal.sH() - (modal.height/2)) - ((modal.wH() + modal.sH() - (modal.height/2)) * .10); if (h < 20) { h = 20; } return h; },
	stW: function() { return (modal.wW() - 3); },
	stH: function() { return modal.wH() + modal.sH(); },
	//legacy_divs: ['modaltl','modal_t','modaltr','modal_l','modal_m','modal_r','modalbl','modal_b','modalbr'],
	//legacy_sizers: ['#modal_l','#modal_m','#modal_r','#modal_t','#modal_b'],
	////////////////////////////////////////////////////////////////////////////////////////////////////
	zoom: function() {
	
		if (modal.options.width) { this.width = this.options.width } 
		else { 
			if (!modal.options.async) {
				this.width = $(this.source).width();
			}
		}
		if (modal.options.height) { this.height = this.options.height }
		else { 
			if (!modal.options.async) {
				this.height = $(this.source).height();
			}
		}
	
	
		if (!($('#box').length > 0)) {			
				this.$_b.append('<div id="box"></div>');			
				this.$.box = $('#box');
				if (this.options.modalClass) { this.$.box.addClass(this.options.modalClass); }

				// legacy
				if (!this.css3) { 
				this.$.box.html('<table id="box_table" style="width: 100%;">\
<tr><td id="modaltl"></td><td id="modal_t"></td><td id="modaltr"></td></tr>\
<tr><td id="modal_l"></td><td id="modal_m"></td><td id="modal_r"></td></tr>\
<tr><td id="modalbl"></td><td id="modal_b"></td><td id="modalbr"></td></tr>\
</table>');
				
				
					//modal.$.box.append('<div id="box_liner"></div>');
					//$.each(modal.legacy_divs, function(i,v) {
					//$('#box_liner').append('<div id="'+modal.legacy_divs[i]+'"></div>');
					//if (i == 2 || i == 5) { $('#box_liner').append('<br class="clear"/>'); } 
					//});
				}
		}
		modal.blanket.show();
		if (modal.css3) { this.$.box.css({backgroundColor:'#fff'}); }
		

//if (!modal.css3) { 
//		w = modal.width, h = modal.height;
//		var bw = w + 45, bh = h + 45;
//		if (bw > $('#box').outerWidth()) { $('#box').css({width: bw}); }
//		if (bh > $('#box').outerHeight()) { $('#box').css({height: bh}); }
//		
//		$('#box').css({
//			left: modal.toW(),
//			top: modal.toH()
//		});

		
		
//														$.each(modal.legacy_sizers, function(i,v) {
//															var ani = {};
//															if (i < 3) { ani.height = h; }
//															else { ani.width = w; }

//															if (v == '#modal_m') { ani.width = w; }
//															$(v).animate(ani, 700, function() {

//															if (i == 4) { 
//																	if (bw < $('#box').outerWidth()) { $('#box').css({width: bw}); }
//																	if (bh < $('#box').outerHeight()) { $('#box').css({height: bh}); }

//																	modal.inner.load();
//																	if (!modal.options.async) { modal.x.show(); modal.inner.show(); }
//															}


//															});
//				});


//}


//else {
$('#box_table').css({height: '100%'});
var pad = (modal.css3 ? 0 : 40);
		modal.$.box.css({
			width: 3,
			height: 3,
			left: modal.stW(),
			top: modal.stH()
		}).animate({
			width: modal.width + pad,
			height: modal.height + pad,
			left: modal.toW(),
			top: modal.toH()
		}, (modal.css3 ? 400 : 800),
				function() {
				
						modal.inner.load();
						if (!modal.options.async) { modal.x.show(); modal.inner.show(); }
				});
//}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////
	resize: function(options) {
		var pad = (modal.css3 ? 0 : 40);
		if (!options) { options = {}; }
		if (options.height) { this.height = options.height; }
		if (options.width) { this.width = options.width; }
		
		if (modal.$.box) {
		modal.x.hide();
		modal.inner.hide();
		modal.$.box.animate({left: modal.toW(), top: modal.toH(), height: this.height + pad, width: this.width + pad }, function() { 
				modal.width = modal.$.box.width();
				modal.height = modal.$.box.height();
				modal.x.show();
				modal.inner.show(); 
		});
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////
	timers: {},
	////////////////////////////////////////////////////////////////////////////////////////////////////
	clear_wait_msgs: function() { for (wmt in this.timers) { clearTimeout(this.timers[wmt]); delete this.timers[wmt]; } },
	////////////////////////////////////////////////////////////////////////////////////////////////////
	wait_msg: function(wm) {
		this.clear_wait_msgs();
		this.timers[wm + '1'] = setTimeout('$(\'#' + wm + '\').html(\'Please Wait.\')', 2000);
		this.timers[wm + '2'] = setTimeout('$(\'#' + wm + '\').html(\'Still Working.\')', 9000);
		this.timers[wm + '3'] = setTimeout('$(\'#' + wm + '\').html(\'Almost There.\')', 18000);
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////
	x: {
		show: function() { 
		$('#x').css({
			left: parseInt(modal.$.box.css('left')) + parseInt(modal.$.box.outerWidth()) - 15,
			top: parseInt(modal.$.box.css('top')) - 10,
			display: 'block'
		});
		},
		hide: function() { 
			$('#x').hide();
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////
	inner: {
	////////////////////////////////////////////////////////////////////////////////////////////////////
		load: function() {
			var i = $('#inner');
			if (i.length <= 0) { modal.$_b.append('<div id="inner"></div><a href="#" onclick="modal.close(); return false;"><img src="images/x.png" id="x" alt="close" /></a>'); }
			modal.$.x = $('#x');
			modal.$.inner = $('#inner');
			
			if (modal.options.innerClass) { modal.$.inner.addClass(modal.options.innerClass); }
			if (modal.options.async) { 
				modal.inner.wait();
				modal.x.hide();
				$.ajax({
						type: "GET",
						url: modal.source,
						cache: modal.options.cache || 'true',
						success: function(r) {
							modal.inner.hide();
							$('#inner').css({width: '', height: ''});
							modal.$.inner.html(r); Cufon.refresh('.cuf'); 
							if ($('#inner img').length > 0) { 
								$('#inner img').load(function() {
								modal.width = $('#inner').outerWidth();
								modal.height = $('#inner').height();
								modal.resize(); });
							}
							else { 
							modal.width = $('#inner').outerWidth();
							modal.height = $('#inner').height();
							modal.resize();
							}
						 }
				});
			}
			else { 
				modal.inner.hide();
				modal.$.inner.html($(modal.source).html());
				if ($('#inner img').length > 0) { 
								$('#inner img').load(function() {
									modal.inner.show();
								});
				}
				else { modal.inner.show(); }
			}
		},
	////////////////////////////////////////////////////////////////////////////////////////////////////
		show: function() {
			var pad = (modal.css3 ? 0 : 40);
			if (!modal.$.box) { modal.$.box = $('#box'); }
			var blop = { display: 'block',
				left: parseInt(modal.$.box.css('left')) + (modal.css3 ? parseInt(modal.$.box.css('padding-left')) : 20),
				top: parseInt(modal.$.box.css('top')) + (modal.css3 ? parseInt(modal.$.box.css('padding-top')) : 20),
				width: modal.width + pad,
				height: modal.height + pad
			}
			
			if (modal.opac_low.opacity) { blop.opacity = modal.opac_low.opacity; }
			$('#inner').stop().css(blop).animate(modal.opac_high, 500, function() { 
				$('#inner').css('opacity', ''); 
				if (modal.options.closeOnBlanket) { $('#blanket').css({cursor: 'pointer'}).click(function() { modal.close(); }); }
			}); 
		},
	////////////////////////////////////////////////////////////////////////////////////////////////////
		hide: function() {
			if (modal.$.inner) { modal.$.inner.css({ display: 'none' }); }
		},
	////////////////////////////////////////////////////////////////////////////////////////////////////
		wait: function() {
			this.hide();
			$('#inner').html('<div class="spinner"><img src="http://www.cincinnatibell.com/shop/images/spinner.gif"><span id="inner_spinner_msg" class="spinner_msg"></span></div>');
			modal.wait_msg('inner_spinner_msg');
			this.show();
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////
	// shortcut for resize and wait
	rw: function() { modal.resize(); modal.inner.wait(); },
	////////////////////////////////////////////////////////////////////////////////////////////////////
	blanket: {
	////////////////////////////////////////////////////////////////////////////////////////////////////
		show: function(callback) {
			modal.blanket.b = $('#blanket');
			if (modal.blanket.b.length <= 0) { modal.$_b.append('<div id="blanket"></div>'); modal.blanket.b = $('#blanket'); }
			if (modal.options.blanketClass) { modal.blanket.b.addClass(modal.options.blanketClass); }
			modal.blanket.b.css({cursor: 'auto'});
			modal.blanket.adjust();
			modal.blanket.b.css({ opacity: 0.1 }).animate({ opacity: 0.6 }, 200);
			if (callback) { callback(); }
		},
	////////////////////////////////////////////////////////////////////////////////////////////////////
		hide: function() {
			modal.blanket.b = $('#blanket');
			if (modal.blanket.b.length > 0) { 
				modal.blanket.b.css({ opacity: 0.9 }).animate({ opacity: 0.0 }, 200, function() { $('#blanket').remove(); delete modal.blanket.b; });
			}
		},
	////////////////////////////////////////////////////////////////////////////////////////////////////
		adjust: function() {
			modal.blanket.b = $('#blanket');
			if (modal.blanket.b.length > 0) { modal.blanket.b.css({ width: $(document).width(), height: $(document).height() }); }
		}
	}
}
$(document).ready(function() {modal.init();});
