var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function popup_link(path, width, height) {
	var width = (width == null) ? 1000 : width;
	var height = (height == null) ? 800 : height;
	
	var extra = "";
	if (width != 0)
	{
		extra += "width="+width;
	}
	if (height != 0)
	{
		extra += ", height="+height;
	}
	
	//alert('Extra Info: '+extra)
	
	new_window = window.open(path,"");
}

function changeTheme(theme,redirect) {
	
	location.href = "/utils/changeTheme/?t="+theme+"&r="+redirect;
	
}

function validateForm(fields,form,error_field) {
	
	var aFields = fields.split(',');
	var error = "";
	var fieldEl = "";
	error_field = typeof(error_field) != 'undefined' ? error_field : false;
	var missing_fields = [];
	
	for (i = 0; i < aFields.length; i++) {
		fieldEl = document.getElementById(aFields[i]);
		if (fieldEl.type == "select-one") {
			if (fieldEl[fieldEl.selectedIndex].value == "NULL") {
				error += "You must select a " + fieldEl.title + "." + (error_field ? "<br />" : "\n\r");
				missing_fields.push(aFields[i]);
			}
		} else  {
			if (fieldEl.value == '') {
				if (fieldEl.type == "file") {
					error += "You must select a " + fieldEl.title + " to upload." + (error_field ? "<br />" : "\n\r");
				} else {
					error += "You must fill in the " + fieldEl.title + " field." + (error_field ? "<br />" : "\n\r");
				}
				missing_fields.push(aFields[i]);
			}
		}
	}
	
	if (error != "") {
		if (error_field) {
			jQuery('#' + error_field).html(error).effect("highlight", {}, 3000);
		} else {
			alert(error);
		}
		
		for (i = 0; i < missing_fields.length; i++) { // BFDCF3 E41B17
			jQuery('#' + missing_fields[i]).css({'border': '1px solid #BFDCF3', 'background-color': '#FF0000'});
		}
		
		return false;
	} else {
		document.getElementById(form).submit();
		return true;
	}	
}

/* SLIDE SHOW FUNCTIONS AND VARIABLES */
var slideshowLeft = 0;
var slideshowPosition = 1;
function featuredSlideshow(id,max) {

	// alert(id);
	// console.log("MAX: " + max + " | ID: " + id);
	
	if (id == 'next') {
		slideshowPosition++;
		slideshowLeft += 645;
		if (slideshowPosition > max) {
			slideshowPosition = 1;
			slideshowLeft = 0;
		}
	} else if (id == 'prev') {
		slideshowPosition--;
		slideshowLeft -= 645;
		if (slideshowPosition <= 0) {
			slideshowPosition = max;
			slideshowLeft = (max - 1) * 645;
		}
	} else {
		slideshowPosition = id;
		slideshowLeft = (id - 1) * 645;
	}

	// console.log("LEFT: " + slideshowLeft + " | POSITION: " + slideshowPosition);

	jQuery("#home_features_portfolio_item_wrapper").animate({"right": slideshowLeft + "px"}, "slow");

}

function showSaleCategory() {

	var item = jQuery('#category').val();

	switch (item) {
		case 'large_items':
			location.href = '/movingsale/large_items/';
			break;
			
		case 'games':
			location.href = '/movingsale/games/';
			break;
			
		case 'books':
			location.href = '/movingsale/books/';
			break;
			
		case 'movies':
			location.href = '/movingsale/movies/';
			break;
	}
	
	return;

}




