/********************************************************************

	Sbuk Base Object
	LM: 03-22-10
	@author Rafael G.

*********************************************************************/

var Sbuk = (function ($) {
	var undef,
		w = window,
		d = document,
		_t = function (_s) { return $.trim(_s); },
		PATHS = {
			JS : '/js/',
			CSS : '/css/',
			IMG : '/images/'
		},
		PLUGINS = {
			CSS : {
				JGROWL : 'jquery.jgrowl.css',
				LIGHTBOX : 'jquery.lightbox-0.5.css'
			},
			JNOTIFY : '',
			JGROWL : 'jquery.jgrowl.min.js',
			REQ_BG_COLOR : 'require-bgcolor.js',
			DD_ROUNDIES : 'DD_roundies_0.0.2a-min.js',
			JHINT : 'jquery.jhint.js',
			SESSION : 'session.js',
			LIGHTBOX : 'jquery.lightbox-0.5.min.js',
			ADD_THIS : 'http://s7.addthis.com/js/250/addthis_widget.js#pub=xa-4b1a3f7954fdf92f'
		};
		
	// the CRAP i have to do bec of W3C validation....WTF W3C!!!		   
	var __WTF_W3C = function () {		
		// NOTE: rounded corners are not supported in Opera & slow on ie8
		var pcss = '<style type="text/css">'+
	           	   '.roundies5 {'+'-moz-border-radius: 5px;'+'-webkit-border-radius: 5px;'+'border-radius: 5px;'+'}'+
			       '.roundies10 {'+'-moz-border-radius: 10px;'+'-webkit-border-radius: 10px;'+'border-radius: 10px;'+'}'+
			       ''+
			       '</style>';
		$('head').append(pcss); // wtf W3C!	
    	
		// apply rounded corners in ie on load...		
		if (Ttow.Browser.msie && !Ttow.Browser.msie8) {									
			DD_roundies.addRule('.roundies5', '5px');
			DD_roundies.addRule('.roundies10', '10px');			
		}		   
	};
	
	var __inquire = function () {
		var $inqLink = $('.get-a-quote-link'),
			p;
		if ($inqLink.length <= 0) { return; }
		$inqLink.bind('mousedown', function () {
			w.name = $(this).attr('rel');			
		});				 
	};
	
	var __applyLightBox = function () {	    
		var $thumbnailTable = $('table.bag-thumbnail');
		if ($thumbnailTable.length <= 0) { return; }
		
		$.getScript(PLUGINS.ADD_THIS);		
		$thumbnailTable.find('a:visible').each(function () {
			var $me = $(this);
			if ($.trim($me.attr('href')) === '') {
				$me.parent().addClass('hide');
			}
		});		
		if ($thumbnailTable.find('a:visible').length <= 0) { return; }		
		// get the lightbox plugin modules dynamically //
		Ttow.Util.require(PATHS.CSS + PLUGINS.CSS.LIGHTBOX, function () {			
		Ttow.Util.require(PATHS.JS + PLUGINS.LIGHTBOX, function () {
			$thumbnailTable.find('a:visible').lightBox({
				overlayBgColor: '#AFB772',
				overlayOpacity: 0.5,
				txtImage: 'Bag',
				imageLoading: '/images/lightbox-ico-loading.gif',									
				imageBtnClose:'/images/lightbox-btn-close.gif',
				imageBlank:'/images/lightbox-blank.gif'
			});
		});});				
	};
	
	var __unloads = function () {
		var transMsg = '<div align="center" class="valign-top"><h3 id="unload-message" class="roundies5">Fetching page...</h3></div>';
		$(w)
			.bind('unload', function () {
				$('#container').addClass('hide');
			})
			.bind('beforeunload', function () {
				$(this).unbind('unload');
				$('#container').addClass('hide');
				
			});
		$("a[href^='http://'], a[href^='https://']").bind('mousedown', function () {
			$(w).unbind('unload').unbind('beforeunload');
		});
	};
	
	var __fireEvents = function () {
		__unloads();
		
		if (w.location.href.indexOf('/bags/other/other-bags.htm') !== -1) {
			$('div.prod_nav:eq(2)').addClass('selected');
		}
	};
	
	return {
		init : function () {
			__WTF_W3C();
			__fireEvents();
			__applyLightBox();
			__inquire();			
		},
		
		Dialog : {
			
			error : function (_msg, _notForm) {
				
				var $dialogCon = $('#form-error');
				if ($dialogCon.length <= 0) { return; }				
				if (_notForm === undef) {
				    $dialogCon.html('').hide();
					_msg = _msg.replace('\n', ''); // under observation
					var warnings = _msg.split('-'),
						warLen = warnings.length,
						html = '';
					//*/	
					html += '<p>';
					html += '<h4 class="notice">You need to fill in these fields:</h4><br />';
					//html += '<img src="'+PATHS.IMG+'error_button.gif" alt="" style="float: left"/>';		
					html += '<ul id="warning-list">';	
					for (var i=0; i<warLen; i++) {						
						if (_t(warnings[i]) === '') {continue;}
						html += '<li>' + _t(Ttow.Util.entities(warnings[i])) + '</li>';
					}
					html += '</ul>';
					html += '</p>';	
					//*/
					
					$dialogCon.html(html)
							  .fadeIn('slow');					
				}
			}
			
		},
		
		loadRequireBGColor : function () {
			$.getScript(PATHS.JS + PLUGINS.REQ_BG_COLOR);
		}
	};
	
})(window.jQuery);

jQuery(function ($) {
	Sbuk.init();
});