function hideAll() {
	$(".loc-desc").each(function() {
		$(this).hide();
	});	
}
function stateHide() {
	//state jump box
	var testState = $("#select-state :selected").val();
	$("#select-state").fadeIn("fast");
	$(".loc").each(function() {
		$(this).hasClass(testState) ? $(this).show() : $(this).hide("");
	});
}
	
function buildMap() {
	$(".loc-info a").click(function() {
		//get map id
		var grabMapID = $(this).parent().attr("id");
		var mapIDSplit = grabMapID.split("-");
		var mapID = mapIDSplit[1];
		
		iframeHTML = '<iframe src="http://www.baobag.com.au/find_a_bag_test/map/' + mapID + '" width="400" height="250"><a href="/find_a_bag_test/map/107" scrolling="no">Show map&nbsp;&raquo;</a></iframe>';
		
		if(!$(this).hasClass(".mapAdded")) {
			$(this).parent().next().prepend
			(iframeHTML);
			$(this).addClass(".mapAdded");
		}
			
		if($(this).parent().next().css("display")=="block") {
			$(this).parent().next().hide("fast");
			$(this).removeClass("clicked");	
		} else {
			$(this).parent().next().show("fast");
			$(this).addClass("clicked");	
		}
	return false;
	});
}

function MM_jumpMenu(targ,selObj,restore){
	if(selObj.id == "select-type") {
		selectType2();
	} else if(selObj.id == "select-state") {
		if(targ !== "loc") {
			segment_prefix = "/find_a_bag_test/";
			eval(targ+".location='"+ segment_prefix + selObj.options[selObj.selectedIndex].value+"'");
			if (restore) selObj.selectedIndex=0;
	}
		} else {
		if(targ !== "loc") {
			eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
			if (restore) selObj.selectedIndex=0;
		}
	}

}


function checkLocation() {
	stateDisplaying = "NSW";
	regionDisplaying = "All";
	
	stateDisplaying = $("#select-state :selected").val();
	regionDisplaying = $("#select-region :selected").text();
	
	$("#d-state").html(stateDisplaying);
	$("#d-region").html(regionDisplaying);
}
 
 
function selectType() {
	var testState = $("#select-state :selected").val();
	var testType = $("#select-type :selected").val();
	
	
 	$(".loc").each(function(i) {
 			rawClass = $(this).attr("class");
			splitRawClass = rawClass.split(" ");
			splitFinalClass = splitRawClass[0].split("-");
			getClassRight = splitFinalClass[1];
			if(getClassRight.charAt(testType) == "1") {
				$(this).show();
			} else {
				$(this).hide();
			}
 	});
}

function selectType2() {
	var testState = $("#select-state :selected").val();
	var testType = $("#select-type :selected").text();
		
	if(testType !== "All") {
	
	counter = 0;
	shownCounter = 0;
	
 	$("p.loc-type").each(function(i) {
		counter++;
		if(testType == $(this).text() && $(this).parent().parent().hasClass(testState)) {
			$(this).parent().parent().show("fast");
			shownCounter++;
		} else  {
			$(this).parent().parent().hide("fast");
 		}
 	
	});
	
	if(shownCounter == 0) {
		noResultsHTML = "There are no results to display.";
		$("#no-results").html(noResultsHTML);
	}
	
	} else {
	$("p.loc-type").each(function(i) {
		if( $(this).parent().parent().hasClass(testState)) {
			$(this).parent().parent().show("fast");
		}
 	
	});
	}
}

var winid = null;
function puViewer(url, width, height) {
	if (winid != null) {
		winid.close();
		winid = null;
	}
	if (width === undefined) {
		width = 720;
	}
	if (height === undefined) {
		height = 580;
	}
	winid = window.open(url, 'puViewer', 'resizable=1,scrollbars=1,status=1,toolbar=0,menubar=0,width='+width+',height='+height)
	winid.focus();
}

// Copied from the script found on Particle Tree http://www.particletree.com
function getBrowserWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth != 0) {
		return document.documentElement.clientWidth;
	} else if (document.body) {
		return document.body.clientWidth;
	}
	return 0;
}

//addEvent() by John Resig
function addEvent( obj, type, fn ) {
	if (obj.addEventListener){
		obj.addEventListener( type, fn, false );
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function(){obj["e"+type+fn](window.event);}
		obj.attachEvent("on"+type, obj[type+fn]);
	}
}

// Modified from the script found on A List Apart @ http://www.alistapart.com
function determineLayout() {
	var obj, title, i;
	var browserWidth = getBrowserWidth();
	if (browserWidth > 1020) {
		title = "wide";
	} else {
		title = "normal";
	}
	for (i=0; (obj = document.getElementsByTagName("link")[i]); i++) {
		if (obj.getAttribute("rel").indexOf("style") != -1 && obj.getAttribute("title")) {
			obj.disabled = true;
			if (obj.getAttribute("title") == title) obj.disabled = false;
		}
	}
}

function externalLinks() {
	if (!document.getElementsByTagName) {
		return false;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

// This variable has to be in the global scope
// if not it will not work properly in IE
var preloadImgObj = new Array();
function preloadImages(extraImages) {
	var imgArr = new Array();
	imgArr[0] = "images/menu/menu-lv2-off.gif";
	imgArr[1] = "images/menu/menu-lv2-on.gif";

	if (extraImages != undefined) {
		imgArr = imgArr.concat(extraImages);
	}
	for (var i=0; i<imgArr.length; i++) {
		preloadImgObj[i] = new Image();
		preloadImgObj[i].src = imgArr[i];
	}
}

(function ($) {
	$.fn.btnRollOver = function() {
		$('img.ro')
			.mouseover(function(){
				$(this).attr('src', $(this).attr('src').replace(/-off/, '-on'));
			})
			.mouseout(function(){
				$(this).attr('src', $(this).attr('src').replace(/-on/, '-off'));
			});
	};
}) (jQuery);

addEvent(window, 'load', externalLinks);
