﻿Vehix$Web$UI = function() { }

Vehix$Web$UI.prototype = 
{
	span : function(style, id, txt)
	{
		var s = document.createElement("span");
		if(typeof(style) == "string")
			s.className = style;
		if(typeof(id) == "string")
			s.id = id;
		if(typeof(txt) == "string")
			s.appendChild(this.textNode(txt));
		return s;
	},

	br : function()
	{
		return document.createElement("br");
	},

	textNode : function(s, f)
	{
		var t = s;
		if(f == "c")
			t = VehixWidgetsUtils.FormatCurrency(s);
		return document.createTextNode(t);
	},

	div : function(style, id, txt)
	{
		var d = document.createElement("div");
		if(typeof(style) == "string")
			d.className = style;
		if(typeof(id) == "string")
			d.id = id;
		if(typeof(txt) == "string")
			d.appendChild(this.textNode(txt));
		return d;
	},

	anchor : function(href, text, style, containshtml, target)
	{
		var a = document.createElement("a");
		if(typeof(href) == "string" && href != "javascript:null")
			a.href = href;
		else if(href == "javascript:null")
			a.href = "javascript:void(0)";
		if(typeof(style) == "string")
			a.className = style;
		if(target)
			a.target = target;
		if(typeof(text) == "string")
		{
			containshtml ? a.appendChild(this.textNode(text)) : a.innerHTML = text;
		}
		else if(text && text.src)
			a.appendChild(text);
		return a;
	},
	
	image : function(style, src, alt)
	{
		var img = new Image();
		if(src && src.length)
		{
			img.src = src;
		}
		else
		{
			img.src = "/_images/!.gif";
		}
		if(alt)
			img.alt = alt;
		if(style) img.className = style;
		return img;
	},
	
	iframe : function(name, style, scroll, src, width, height, brdr)
	{
		var f = document.createElement("iframe");
		if(style) f.className = style;
		if(scroll) f.scrolling = scroll;
		if(src) f.src = src;
		if(brdr) f.frameBorder = brdr;
		if(width) f.width = width;
		if(height) f.height = height;
		if(name)
		{
			f.id = name;
			f.name = name;
		}
		return f;
	},
	
	input : function(typ, style, id, txt, blur, zipprompt)
	{
		var inp = document.createElement("input");
		if(typeof(typ) == "string")
			inp.setAttribute("type", typ);
		if(typeof(id) == "string")
		{
			inp.setAttribute("id", id);
			inp.setAttribute("name", id);
		}
		if(typeof(txt) == "string")
			inp.setAttribute("value", txt);
		if(typeof(style) == "string")
			inp.className = style;
		if(blur)
		{
			inp.onfocus = function()
			{
				if(inp.value == zipprompt)
				{
					inp.value = "";
					inp.className = style;
				}
			}
			inp.onblur = function()
			{
				if(inp.value == "")
					inp.value = zipprompt;
				inp.className = inp.value == zipprompt ?  style + "_disabled" : style;
			}
			if(inp.value == zipprompt)
			{
				inp.className = style+"_disabled";
			}
		}
		return inp;
	},
	
	select : function(opts, style, id, selectedValue, opt0text)
	{
		var sel = document.createElement("select");
		typeof(style) == "string" ? sel.className = "vx_widget-select " + style : sel.className = "vx_widget-select";
		if(typeof(id) == "string")
			sel.id = id;
		if(opt0text && opt0text != "")
			sel.appendChild(this.option(opt0text, "", false));
		if(opts && opts.length)
		{
			for(var i = 0; i < opts.length; i++)
			{
				sel.appendChild(this.option(opts[i].text, opts[i].value, (typeof(selectedValue) != "undefined" && selectedValue != null && selectedValue == opts[i].value)));
			}
		}
		return sel;
	},
	
	option : function(text, value, selected)
	{
		var opt = document.createElement("option");
		opt.value = value;
		if(selected)
			opt.setAttribute("selected", "selected", 0);
		opt.appendChild(this.textNode(text));
		return opt;
	},
	
	actionImage : function(style, href, triple, target)
	{
		var img = new Image();
		img.src = "/_images/!.gif";
		img.className = style;
		img.style.borderStyle = "none";
		img.onmouseover = function()
		{
			triple ? this.style.backgroundPosition = "center" : this.style.backgroundPosition = "right";
		}
		if(triple)
		{
			img.onmousedown = function()
			{
				this.style.backgroundPosition = "right";
			}
		}
		img.onmouseout = function()
		{
			this.style.backgroundPosition = "left";
		}
		
		var a = document.createElement("a");
		if(target)
			a.target = target;
		href && href.length ? a.href = href : a.href = "javascript:null";
		a.appendChild(img);
		return a;
	},
	
	Show : function(o, s)
	{
		if(o && o.style)
			s && s.length ? o.style.display = s : o.style.display = "block";
	},
	
	Hide : function(o)
	{
		if(o && o.style)
			o.style.display = "none";
	},
	
	scrollingPanel : function(style, contents, id, additionalChildren)
	{
		var d = this.div(style, id);
		var dsc = this.div(style + "-scroll-container");
		var ds = this.div(style + "-scroll");
		dsc.appendChild(ds);
		d.appendChild(dsc);
		if(contents && contents.length)
		{
			this._ac(ds, contents);
		}
		else if(contents)
		{
			ds.appendChild(contents);
		}
		if(additionalChildren && additionalChildren.length)
		{
			this._ac(dsc, additionalChildren);
		}
		else if(additionalChildren)
		{
			dsc.appendChild(additionalChildren);
		}
		return d;
	},
	
	CreateOptionArrayForSelect : function(array, textid, valueid)
	{
		var arr = new Array();
		var t;
		var v;
		for(var i = 0; i < array.length; i++)
		{
			eval("t = array[i]." + textid);
			eval("v =  array[i]." + valueid);
			arr.push({"text":t, "value":v});
		}
		return arr;
	},
	
	ClearSelect : function(oList, prmpt)
	{
		if(oList)
		{
			if(oList.options && oList.options.length > 0)
			{
				for(var i = oList.options.length - 1; i > -1; i--)
				{
					oList.remove(i);
				}
			}
			if(typeof(prmpt) != "undefined")
				oList.appendChild(this.option(prmpt, "", ""));
		}
	},
	
	PopulateSelectFromArray : function(oList, itemArray, selectedvalue, prmpt)
	{
		if(oList && itemArray && oList.options && itemArray.length)
		{
			this.ClearSelect(oList);
			if(typeof(prmpt) != "undefined")
				oList.appendChild(this.option(prmpt, "", selectedvalue));
			for(var i = 0; i < itemArray.length; i++)
				oList.appendChild(this.option(itemArray[i][0], itemArray[i][1], itemArray[i][1] == selectedvalue));
			oList.disabled = false;
		}
	},
	
	SetSelectSelectedByValue :  function(oList, value, validate)
	{
		var match = false;
		if(oList && oList.options && oList.options.length > 0)
		{
			for(var i = 0; i < oList.options.length; i++)
			{
				if(oList.options[i].value == value)
				{
					oList.selectedIndex = i;
					match = true;
				}
			}
		}
		if(validate)
			return match;
	},
	
	SetSelectSelectedByText :  function(oList, text, validate)
	{
		var match = false;
		if(oList && oList.options && oList.options.length > 0)
		{
			for(var i = 0; i < oList.options.length; i++)
			{
				if(oList.options[i].text == text)
				{
					oList.options[i].selected = true;
					match = true;
				}
				else
				{
					oList.options[i].selected = false;
				}
			}
		}
		if(validate)
			return match;
	},
	
	GetSelectSelectedValue : function(oList)
	{
		var result = null;
		if(oList && oList.options && oList.selectedIndex > -1)
		{
			result = oList.options[oList.selectedIndex].value;
		}
		return result;
	},
	
	GetSelectSelectedText : function(oList)
	{
		var result = null;
		if(oList && oList.options && oList.selectedIndex > -1)
		{
			result = oList.options[oList.selectedIndex].text;
		}
		return result;
	},
	
	_createSelectOptsFromArray : function(arr)
	{
		var r = new Array();
		if(arr && arr.length)
		{
			for(var i = 0; i < arr.length; i++)
			{
				if(typeof(arr[i]) != "array" && typeof(arr[i]) != "object")
				{
					r.push({"text" : arr[i], "value" : arr[i]});
				}
				else
				{
					r.push({"text" : arr[i][0], "value" : arr[i][1]});
				}
			}
		}
		return r;
	},
	
	_convertToHtml : function (st)
	{
	    if(typeof(st)  != "undefined")
	    {
	        return st.replace(/\r\n/g, "<br />");
	    }
	},
	
	_ac : function(p, c) //append children
	{
		for(var i = 0; i < c.length; i++)
			p.appendChild(c[i]);
	},
	
	_buildDataRow : function(aColumnStyles, aColumnData, rowStyle)
	{
		if(aColumnStyles.length != aColumnData.length)
			VehixLoggingService.LogError("_buildDataRow with invalid arguments");
			
		var p = this;
		var ddr = p.div(rowStyle);
		var ddc;
		
		for(var i = 0; i < aColumnStyles.length; i++)
		{
			ddc = p.div(aColumnStyles[i]);
			ddc.appendChild(p.textNode(aColumnData[i]));
			ddr.appendChild(ddc);
		}
		return ddr;
	},
	
	_removeChildren : function(d)
	{
		d.innerHTML = "";
	},
	
	_replaceChildren : function(d, c)
	{
		d.innerHTML = "";
		d.appendChild(c);
	},
	
	NewWindow : function(sURL, sWindowName, bToolbar, bScrollBars, lWidth, lHeight, lTop, lLeft)
	{
		if(typeof lTop == 'undefined') {
			lTop = (screen.height - lHeight) / 2
		}
		if(typeof lLeft == 'undefined') {
			lLeft = (screen.width - lWidth) / 2
		}
		if(bToolbar == true){
			bToolbar = 'yes'
			bMenubar = 'yes'
		}else{
			bToolbar = 'no'
			bMenubar = 'no'
		}
		if(bScrollBars == true){
			bScrollBars = 'yes'
		}else{
			bScrollBars = 'no'
		}
		var lsFeatures = 'location='+bToolbar+',toolbar='+bToolbar+',scrollbars='+bScrollBars+',resizable=yes,status=yes';	
		lsFeatures += ',top='+ lTop
		lsFeatures += ',left='+ lLeft
		if(lWidth > 0){lsFeatures += ',width='+lWidth};
		if(lHeight > 0){lsFeatures += ',height='+lHeight};
		lsFeatures += ',menubar=' + bMenubar;
		try
		{
			popupWin = top.open(sURL, sWindowName, lsFeatures);
		}
		catch(e)
		{
			popupWin = window.open(sURL, sWindowName, lsFeatures);
		}
		popupWin.focus();
	}
}

Vehix$Web$UI = new Vehix$Web$UI();

(function($) {

	$.fn.tabSelect = function() {
		return $(this).each(function(i) {
			$(this).bind("click", { "index": i }, function(e) {
				var indx = e.data.index;
				$(this).siblings("a.tab").removeClass("on");
				$(this).addClass("on");
				$(this).closest(".tabs").siblings(".panels").first().children(".panel").each(function(i) {
					$(this).addClass("hide");
					if (i == indx)
						$(this).removeClass("hide");
					Vehix.Web.Ads.rotate('*');
				});
			});
		});
	};
	$.fn.twitterGet = function(account) {
		var container = $(this);
		$.getJSON('http://search.twitter.com/search.json?callback=?&rpp=3&q=from:' + account, function(data) {
			$.each(data.results, function(i, tweets) {
				container.append("<p>" + tweets.text + "<br /><span class='tweetTime'>" + relativeTime(tweets.created_at) + "</span></p>");
			});
		});
	};
	$.fn.getGoogleMap = function(lat, lon) 
	{
		if($(this)[0] != null)
		{
			var Latlong = new google.maps.LatLng(lat, lon);
			var options = { zoom: 15,
				center: Latlong,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.SMALL
				}
			};
			var map = new google.maps.Map($(this)[0], options);
			var marker = new google.maps.Marker({  
					  position:Latlong,  
					  map: map  
					});
		}
	};
})(jQuery);

function relativeTime(tweetTime) 
{
	var origStamp = Date.parse(tweetTime);
	var curDate = new Date();
	var currentStamp = curDate.getTime();
	var difference = parseInt((currentStamp - origStamp) / 1000);
	
	if (difference < 0) return false;
	if (difference <= 5) return "Just now";
	if (difference <= 20) return "Seconds ago";
	if (difference <= 60) return "A minute ago";
	if (difference < 3600) return parseInt(difference / 60) + " minutes ago";
	if (difference <= 1.5 * 3600) return "One hour ago";
	if (difference < 23.5 * 3600) return Math.round(difference / 3600) + " hours ago";
	if (difference < 1.5 * 24 * 3600) return "One day ago";
	// If the tweet is older than a day, show an absolute date/time value;

	var datesplit = tweetTime.split(' ');
	return datesplit[2] + " " + parseFloat(datesplit[1]).toFixed(0) + " " + datesplit[3];
}
