// Bottom Link Menu

function LinkMenu(toggle,object,x,y,yoffset,xoffset){
	if (x != null) {
		y-=yoffset;
		x-=xoffset;
		var curtop = y+"px";
		var curleft= x+"px";
		changecss('#LinkMenuBlock','top',curtop);
		changecss('#LinkMenuBlock','left',curleft);
	}
	if (toggle) document.getElementById(object).style.display = 'block';
	else document.getElementById(object).style.display = 'none';
}

function findPosX(obj){
	var curleft = 0;
		if(obj.offsetParent)
			while(1){
				curleft += obj.offsetLeft;
				if(!obj.offsetParent) break;
				obj = obj.offsetParent;
			} else if(obj.x) curleft += obj.x;
		return curleft;
}

function findPosY(obj){
	var curtop = 0;
		if(obj.offsetParent)
			while(1){
				curtop += obj.offsetTop;
				if(!obj.offsetParent)	break;
				obj = obj.offsetParent;
			} else if(obj.y) curtop += obj.y;
		return curtop;
}

// Process Email Form Data
function processForm() {
	$('submit').disabled = true;
	$('submit').value = "Processing... Please Wait...";
	$('contact_form').request({
		onSuccess: function(transport)
		{
			if(transport.responseText.match(/^OK/) != null) {
				$('contact_form').reset();
				location.href="thanks.html";
			} else {
				alert(transport.responseText);
			}
			$('submit').value = 'Send Message';
			$('submit').disabled = false;
		}
	});
	return false;
}

// Process Portfolio/Menu Mouse over highlight effects
function PortfolioOver(ImgCon,ProCon,MenuItem) {
	if (ImgCon) {set=document.getElementById(ImgCon); set.style.border="solid 1px #a70000"; set.style.top="2px"; set.style.left="2px";}
	if (ProCon) {dec=document.getElementById(ProCon); dec.style.left="2px"; dec.style.top="2px"; dec.style.color="#a70000";}
	if (MenuItem) {mnu=document.getElementById(MenuItem); mnu.style.color="#a70000";}
}
function PortfolioOut(ImgCon,ProCon,MenuItem) {
	if (ImgCon) {set=document.getElementById(ImgCon); set.style.border="solid 1px #036"; set.style.top="0px"; set.style.left="0px";}
	if (ProCon) {dec=document.getElementById(ProCon); dec.style.left="0px"; dec.style.top="0px"; dec.style.color="#036";}
	if (MenuItem) {mnu=document.getElementById(MenuItem); mnu.style.color="#036";}
}

// Swap mouse over image
function swapImage(ID,image) {
	document.getElementById(ID).src=image;
}

// Disable Mouse Select except on forms
window.onload = function() {
	document.onselectstart = function () { return false; } // ie
	document.onmousedown = function () { return false; } // mozilla

	document.getElementById('contact_form').onmouseover = function() {
		document.onselectstart = function () { return true; } // ie
		document.onmousedown = function () { return true; } // mozilla
	}

	document.getElementById('contact_form').onmouseout = function() {
		document.onselectstart = function () { return false; } // ie
		document.onmousedown = function () { return false; } // mozilla
	}
}

// Disable Mouse Right Click
	var isNS = (navigator.appName == "Netscape") ? 1 : 0;
	if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
	function mischandler(){
		return false;
	}
function mousehandler(e){
	var myevent = (isNS) ? e : event;
	var eventbutton = (isNS) ? myevent.which : myevent.button;
		if((eventbutton==2)||(eventbutton==3)) return false;
}

document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;

// redirect cache pages
	var ref = document.referrer;
	var url = document.location.href;
	var bot_check = /google|yahoo|bing|lycos|gigablast|jeeves/i;
	var unique_check = /unique|fabricating/i;
	var keyword_check = /nordic|reelcraft|reel|hose|valve|manifold|cable|product|emergency|petroleum|industrial|welding/i
	var cache_check = /cachecache|webcache|cache|cc.bingj/i;
	var cache_location = /portfolio|resume|contact|render-pg1|render-pg2|render-pg3|catalog-pg1|catalog-pg2|acad-pg1|acad-pg2|inventor-pg1|inventor-pg2|inventor-pg3|inventor-pg4|advert-pg1|excel-pg1|misc-pg1|siteindex|terms/i;
	var site_check = /yaroslavdrozd.ca|yidweb.net/i;
	var site_name = url.match(site_check);

	if (site_name == null) { site_name="yaroslavdrozd.ca"; } //change back to yidweb.net for uploading to yidweb.net
	if (ref.match(bot_check) != null) { if (ref.match(keyword_check) != null) { window.location = "http://www."+site_name+"/redirect.html?keyword="+ref.match(keyword_check); } }
	else if (ref.match(bot_check) != null) { if (ref.match(unique_check) != null) { window.location = "http://www."+site_name+"/404_error.html"; } }


	if (url.match(cache_check) != null) {
		var page_name = url.match(cache_location);
		if (page_name != null) { window.location = "http://www."+site_name+"/"+page_name+".html"; }
			else { window.location = "http://www."+site_name+"/index.php"; }
	}

// Change CSS attributes
function changecss(theClass,element,value) {
	//	Last Updated on June 23, 2009
	//	documentation for this script at
	//	http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
	var cssRules;
	var added = false;
	for (var S = 0; S < document.styleSheets.length; S++){
		if (document.styleSheets[S]['rules']) {
			cssRules = 'rules';
		} else if (document.styleSheets[S]['cssRules']) {
			cssRules = 'cssRules';
		} else {
		//no rules found... browser unknown
		}
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
				if(document.styleSheets[S][cssRules][R].style[element]){
					document.styleSheets[S][cssRules][R].style[element] = value;
					added=true;
					break;
				}
			}
		}
		if(!added){
			if(document.styleSheets[S].insertRule){
				document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
		}
	}
}
