var TOL = {
	DEBUG: new Boolean(), DOMAIN: "www.cycletrader.com", Module: {}, Tile: {},
	init: function(){
		var _call = function(o){
			if (typeof(o[0]) != "undefined" && typeof(o[1]) != "undefined") {
				TOL.log(o[0] + " was initialized.");
				if (typeof(o[1].init) == "function") o[1].init();
			}
		};
		var _namespaces = ["Module", "Tile"], _delayed = [], _prioritized = [];
		for (var i=0; i<_namespaces.length; i++) {
			var _namespace = TOL[_namespaces[i]];
			for (var _item in _namespace) {
				var _alias = _namespaces[i] + "::" + _item;
				try {
					if (typeof _namespace[_item].priority != "undefined" && _namespace[_item].priority) {
						switch (_namespace[_item].priority) {
							case "first": case "1": case 1: _prioritized.unshift([_alias, _namespace[_item]]); break;
							case "last": _delayed.push([_alias, _namespace[_item]]); break;
							default: _prioritized.push([_alias, _namespace[_item]]); break;
						}
					} else if (typeof _namespace[_item].delay != "undefined" && _namespace[_item].delay) {
						_delayed.push([_alias, _namespace[_item]]);
					} else {
						_prioritized.push([_alias, _namespace[_item]]);
					}
				} catch(e) {
					console.log(_item);
				}
			}
		}
		for (var j=0;j<_prioritized.length;j++) _call(_prioritized[j]);
		for (var k=0;k<_delayed.length;k++) _call(_delayed[k]);
	},
	log: function(msg) {
		//if (TOL.DEBUG && typeof(console) != "undefined" && typeof(console.log) != "undefined")
			//console.log.apply(this, arguments);
	},
	extractTargetId: function(obj, triggerClassName){
		if($(obj).attr("class")){
		var _targetId, _classNames = $(obj).attr("class").split(" ");
		var _separator = (arguments.length > 2) ? arguments[2] : "-";
		for (var i=0; i<_classNames.length; i++)
			if (_classNames[i].search(triggerClassName) != -1)
				return _classNames[i].split(_separator)[1];}
	},
	unregister: function(namespace, item){
		var _o = TOL[namespace], _ns = "." + item.charAt(0).toLowerCase() + item.slice(1);
		delete _o[item];
		$(document).unbind(_ns).die(_ns);
	}
};
TOL.cookie = function() {
	return {
		get: function(sName) {
			var sKey = sName + "=";
			var arCookie = document.cookie.split(';');
			for (var i=0; i < arCookie.length; ++i) {
				var sRef = arCookie[i];
				while (sRef.charAt(0)==' ') {
					sRef = sRef.substring(1,sRef.length);
				}
				if (sRef.indexOf(sKey) == 0) {
					return ( unescape (sRef.substring(sKey.length,sRef.length)) );
				}
			}
		},
		set: function(sName, sVal, sDays, sPath) {
			if(!sPath) { sPath = "/"; }
			var expires = "";
			if (sDays) {
				var date = new Date();
				date.setTime(date.getTime()+(sDays*24*60*60*1000));
				expires = "; expires="+date.toGMTString();
			}
			document.cookie = sName+"="+sVal+expires+"; path=" +sPath;
		}
	};
}();
TOL.Module.OAS = function() {
	var _sgOASc, _arPos;
	return {
		priority: "last",
		init: function() {
			_sgOASc = $('#oas_c');
			if (_arPos == null) {
				var x = TOL.Module.OAS.getList();
			}
			TOL.Module.OAS.show();
			TOL.Module.OAS.fixExpandables();
		},
		fixExpandables: function(){
			if (!$.browser.mozilla) return;
			$("span[id^='oasad-Right']").each(function(i){
				var _expandableAd = $(this).find("object")[0];
				if (_expandableAd) {
					$(_expandableAd).width(160).css("overflow", "hidden");
					$(_expandableAd).mouseover(
						function(){
							$(this).css("overflow", "visible");
						}).mouseout(function(){
							$(this).css("overflow", "hidden");
						});
				}
			});
		},
		getList: function() {
			var s = '';
			$("span:[id^='oasloc-']").each(function() {
				var x = $(this).attr('id');
				x = x.split('-');
				if (x.length == 2) {
					if (s.length > 0) { s +=',';}
					s += x[1];
				}
			});
			_arPos = s.split(',');
			return s;
		},
		show: function() {
			var OAS_check = false;
			_sgOASc.children("span:[id^='oasad-']").each(function(i){
				// If advert marker has been added to the DOM
				var pos = this.id.split('-');
				if (pos.length == 2) {
					if ($.inArray(pos[1],_arPos) > -1) {
						if (this.innerHTML.indexOf('src') > -1) {
					        if (this.innerHTML.indexOf('empty.gif') == -1) {
				            	var _p = document.getElementById('oas_'+pos[1])
				            	var _o = document.getElementById('oasloc-'+pos[1]);
				            	var _n = document.getElementById('oasad-'+pos[1]);
				            	if (_p && _o && _n) {
				            		var _trash = _p.replaceChild(_n,_o);
				            	}
					        }
					    } else {
					        OAS_check = true;
					    }
					} else {
						$(this).remove();
					}
				}
			});
			// Continue to poll only if one or more adverts weren't loaded
			if (OAS_check) {
				setTimeout('TOL.Module.OAS.show()', 3000);
			}
		}
	};
}();
TOL.Module.Mask = function(){
	var _mask, _id = "tolMask", _opacity = 6, _zIndex = 20;
	return {
		init: function(){
			var _width = $(document).width(), _height = $(document).height()*1.25;
			_mask = $(document.createElement("div"));
			_mask.attr("id", _id);
			_mask.css("position", "absolute").css("z-index", _zIndex).css("overflow", "hidden");
			_mask.css("top", "0").css("left", "0");
			//do not create the mask/background when used in an iframe - (for WSN Project 10.01.10)
			if(window.location == window.parent.location)  {
        _mask.css("background", "#000000").css("filter", "alpha(opacity=" + _opacity*10 + ")").css("opacity", _opacity/10);
      }         
			else
				_mask.css("background", "#FFFFFF").css("filter", "alpha(opacity=80)").css("opacity", .8);
      _mask.css("width", (_width) + "px").css("height", _height + "px");
			_mask.hide();
			$(document.body).append(_mask);
		},
		show: function()
		{
			var _width = $(document).width(), _height = $(document).height()*1.25;
			_mask.css("width", (_width) + "px").css("height", _height + "px");
			_mask.show();
		},
		hide: function(){
			_mask.hide();
			},
		toggle: function(){
			var _width = $(document).width(), _height = $(document).height()*1.25;
			_mask.css("width", (_width) + "px").css("height", _height + "px");
			_mask.toggle();
			},
		isVisible: function(){return(_mask.css("display")!="none");}
	};
}();
TOL.Module.ModalWindow = function(){
	var _windows, _windowContainer, _windowClassName = "modalWindow", _windowControlClassName = "modalWindowControl";
	var _isVisible = false;
	return {
		toggle: function(windowId){
			_isVisible = false;
			_windows.filter("#"+windowId).each(function(i){
				if (this.id == windowId) {
					if ($(this).css("display") != "none") {
						TOL.Module.Mask.hide();
						$(this).hide();
					} else {
						var _modalWindowHeight = $(this).height();
						var _offsetTop = (window.location == window.parent.location) ? ($(window).height()-_modalWindowHeight)/4: 20;
						$(this).css("top", ($(this).scrollTop() + _offsetTop)+"px");
						if (jQuery.support.boxModel) {
							$(this).css("position", "fixed");
						} else {
							_windows.css("top", ($(window).scrollTop() + _offsetTop) + "px");
							$(window).scroll(function() {
								_windows.css("top", ($(window).scrollTop() + _offsetTop) + "px");
							});
						}
						$(this).fadeIn();
						TOL.Module.Mask.show();
						_isVisible = true;
						$(this).css("top", "75px").css("left", ($(document.body).width()-$(this).width())/2 + "px");
					}
				} else {
					TOL.Module.Mask.hide();
					$(this).hide();
				}
			});
			return false;
		},
		show: function(windowId)
		{
			if($('#'+windowId).is(":hidden"))
			{
				TOL.Module.ModalWindow.toggle(windowId);
			}
		},
		hide: function(windowId)
		{
			if(!$('#'+windowId).is(":hidden"))
			{
				TOL.Module.ModalWindow.toggle(windowId);
			}
		},
		hideAll: function(){
			TOL.Module.Mask.hide();
			_windows.hide();
		},
		registerModalWindow: function(obj){
			var _window = $(obj);
			_window.css("position", "absolute").css("z-index", "100001");
			_window.css("top", "100px").css("left", ($(document.body).width()-_window.width())/2 + "px");
			var elems = document.getElementById('modalWindowsContainer').getElementsByTagName('div');
			var found = false;
			for(var i=0; i<elems.length; i++)
			{
				if(elems[i].id==_window.attr('id'))
				{
					found = true;
					break;
				}
			}
			if(!found)
				_windowContainer.append(_window);
			_windows = _windowContainer.find("div." + _windowClassName);
		},
		registerModalWindowControl: function(obj){
			if (obj.className.search(_windowControlClassName) != -1) {
				var _windowId = TOL.extractTargetId(obj, _windowControlClassName);
				$(obj).unbind('click.modalWindow').bind("click.modalWindow", function(){
					return TOL.Module.ModalWindow.toggle(_windowId);
				});
			}
		},
		init: function(){
			if($('#modalWindowsContainer').length<1)
			{
				_windowContainer = $(document.createElement("div"));
				_windowContainer.attr("id", "modalWindowsContainer");
				$(document.body).append(_windowContainer);
			}
			else
				_windowContainer = $('#modalWindowsContainer');

			$("div." + _windowClassName).each(function(i){
				TOL.Module.ModalWindow.registerModalWindow(this);
			});
			$("*").each(function(i){
				TOL.Module.ModalWindow.registerModalWindowControl(this);
			});
			$(document).bind("keypress.modalWindow", function(e){
				if (e.keyCode == "27" && _isVisible) {
					TOL.Module.ModalWindow.hideAll();
				}
			});
		}
	};
}();
TOL.Module.ToolTips = function(){
	var _toolTipClassName = "toolTip", _toolTipControlClassName = "toolTipControl", _toolTips = $([]);
	return {
		toggle: function(obj, toolTipId){
			_toolTips.each(function(i){
				if ($(this).attr("id") == toolTipId) {
					var _toolTip = $(this);
					var _top = $(obj).position().top - ($(this).height()/2);
					var _left = $(obj).position().left + $(obj).width() + 10;
					_toolTip.css("top", _top + "px").css("left", _left + "px").toggle();
				}
			});
			return false;
		},
		registerToolTip: function(obj){
			var _obj = $(obj);
			_obj.css("position", "absolute").hide();
			_toolTips.push(_obj);
		},
		registerToolTipControl: function(obj){
			if (obj.className.search(_toolTipControlClassName) != -1) {
				var _toolTipId = TOL.extractTargetId(obj, _toolTipControlClassName);
				if (_toolTipId) {
					$(obj).bind("mouseover.toolTip mouseout.toolTip", function(){
						return TOL.Module.ToolTips.toggle(this, _toolTipId);
					});
				}
			}
		},
		init: function(){
			$("div." + _toolTipClassName).each(function(i){
				TOL.Module.ToolTips.registerToolTip(this);
			});
			$("*").each(function(i){
				TOL.Module.ToolTips.registerToolTipControl(this);
			});
		}
	};
}();
TOL.Module.Slick = function(){
	return {
		init: function(){
			$("input.inputText").bind("focus", function(){
				var _obj = $(this);
				if (_obj.val() == _obj.attr("alt") || _obj.val() == "")
					_obj.val("").addClass("black");
	    	}).bind("blur", function(){
	    		var _obj = $(this);
		        if (_obj.val()=="")
		        	_obj.val(_obj.attr("alt")).removeClass("black");
			});
		}
	};
}();
TOL.Module.ieHover = function() {
	return {
		init: function() {
      if($.browser.msie)
      {
        TOL.Module.ieHover.setHover('ul.subNav li');
      }     
		},
		setHover: function(oHoverElement,oDisplayElement) {  
      var oShowElement = (oDisplayElement != null) ? oDisplayElement : 'ul';  
      $(oHoverElement).hover(function() { $(oShowElement, this).css('display', 'block'); },function() { $(oShowElement, this).css('display', 'none'); }); 
  	  $(oHoverElement).css('cursor', 'pointer');
		}
	};
}(); 
TOL.Module.viewPortDimensions = function() {
	return {
		init: function() {
	     var viewportheight = 0, viewportwidth = 0;
		},
    getViewportHeight: function() {
       if (typeof window.innerWidth != 'undefined') {
            viewportheight = window.innerHeight;
       }
       else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
             viewportheight = document.documentElement.clientHeight;
       }
       else {
             viewportheight = document.getElementsByTagName('body')[0].clientHeight;
       }
       return viewportheight;
	  },       
	  getViewportWidth: function() {
	     if (typeof window.innerWidth != 'undefined') {
            viewportwidth = window.innerWidth;
       }
       else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
             viewportwidth = document.documentElement.clientWidth;
       }
       else {
             viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
       }
       return viewportwidth;
	  }
	};
}();
$(document).ready(function(){
	TOL.init();
});

/*********************************
popup for security awareness page
**********************************/
function fraudPopUp(page)
{ 
	window.open("http://www.traderonline.com/security-popup/?sDomainName=www.cycletrader.com",'mywindow','width=590,height=690,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes'); 
}

// Make an Offer Module
TOL.Module.MAO = function(){
	var _sgLinks, _sgModal;
	
	return {
		send: function(t,ajmcData) {
			$.ajax({
					type: t,
					url:'/Gettiledata/makeanoffer/ajmc/',
					dataType: 'json',
					data: ajmcData,
					cache: false,
					success: function(json) {
						TOL.Module.MAO.parseResponse(json);
					}
				});
		},
		sendForm: function(sId,sStep,bBack) {
			var _sgForm = $('#'+sId);
			if(typeof(bBack)=="undefined")
				bBack=0;
			if (_sgForm.length == 1) {
				TOL.Module.MAO.send('POST','m='+sStep+'&back='+bBack+'&'+_sgForm.serialize());
			}
		},
		parseResponse: function(data) {
			if (data.success == true) {
				// inject content
				_sgModal.find('#moahdrtxt').html(data.content.title);
				_sgModal.find('.modalContent').html(data.content.body);
				if (!_sgModal.is(':visible')) {
					TOL.Module.ModalWindow.toggle(_sgModal.attr('id'));
				}
			}
		},
		getModal: function(iNum) {
			TOL.Module.MAO.send('GET','m=create&lid='+iNum);
			
		},
		count : function(field,maxchar) {
			 if (field.value.length > maxchar) {
			  field.value = field.value.substring(0, maxchar);
			 }
		},
		init: function(){
			_sgLinks = $("a:[id^='lnkmao_']");
			_sgModal = $('#modal_mao');
			if (_sgLinks.length) {
				_sgLinks.each(function(){
					var ad = $(this).attr('id').split('_');
					if (ad.length == 2) {
						$("div:[id^='listing.id:"+ad[1]+"']").find(".resultTitle").find('a').css('width','310px');
						$(this).bind('click.mao',function(){
							TOL.Module.MAO.getModal(ad[1]); return false;
						});
					}
				});
			}
		}		
	};
}();

