/* 
Title:		Addresses Javascript
Author: 	3 Crown Creative
*/

//----------------------------------------------------------------------
// FUNCTION: Housekeeping
//----------------------------------------------------------------------
function houseKeeping() {
	checkBrowser();	
	
	window.addEvent('domready',function() {

		/*  (1) Make all links to external sites open in a new window  */
		$$('a[href^="http://"]').each(function(a) {   /* grab all complete linked anchors */
			var href = a.get('href');
			if(!href.contains(window.location.host)) {  /* if it's not this domain */
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
			}
		});
		
		// Add pdf icons to pdf links
		$$("a[href$='.pdf']").each(function(a) {   
			var href = a.get('href');
				a.setProperties({
					rel: 'nofollow',
					target: '_blank'
				});
				a.addClass("pdf");
		});
	    
	});
	
	/*  dropdown menu stuff for IE */
	sfHover = function() {
		var sfEls = document.getElementById("nav").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	
	if (window.attachEvent) window.attachEvent("onload", sfHover);
	
}

//----------------------------------------------------------------------
// FUNCTION: Add Scrollbar Stylesheet when javascript is enabled
//----------------------------------------------------------------------
function linkCSS(title) { 
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     	if (a.getAttribute("rel").indexOf("style") != -1  && a.getAttribute("title")== title) {
			a.disabled = true;
			a.disabled = false;
		}
    }
}


//----------------------------------------------------------------------
// FUNCTION: add events to grid thumbnails													
//----------------------------------------------------------------------
function setThumbs() {
	window.addEvent('domready', function() {
		
		//  decativate the HREF
		$$('#grid a').each (function(link) {
			link.addEvent('click', function(event) {
				event.preventDefault();
			});
		});
		
		//  add click, mouseover and mouseout events
		$$('#grid img').each (function(el) {
			el.addEvents ({
				
				'click': function(imgID) {
					mouseClick(imgID);
				}.pass(el.id),
	
				'mouseover': function() {
					this.removeClass('imgNoFrame');
					this.addClass('imgFrame');
				},
				
				'mouseout': function(imgID) {
					mouseOut(imgID);
				}.pass(el.id)
			});
		});
		
		//  decativate the HREF for the arrows
		$$('.arrowL').each (function(link) {
			link.addEvent('click', function(event) {
				event.preventDefault();
			});
		});
		//  decativate the HREF for the arrows
		$$('.arrowR').each (function(link) {
			link.addEvent('click', function(event) {
				event.preventDefault();
			});
		});
		var swipe = new MooSwipe('portfolioImgLg');
		swipe.addEvent('swipeleft', function() {mouseClick('l');});
		swipe.addEvent('swiperight', function() { mouseClick('r'); });
	
	});
	
	
	document.addEvent('keydown', function(event) {
    	if (event.key == 'right') mouseClick('r');
		if (event.key == 'left') mouseClick('l');
	});
	
	
}
//---------------------------------------------------------------------------
//  FUNCTION: Mouseclick on a thumbnail 
//---------------------------------------------------------------------------
function mouseClick(imgID) {

	if (imgID=='r') 
		(curImg.substr(1)).toInt()+1>numImages ? imgID = prefix+'1' : imgID = prefix+((curImg.substr(1)).toInt()+1).toString();
	else
		if (imgID=='l')
			(curImg.substr(1)).toInt()-1==0 ? imgID = prefix+numImages.toString() : imgID = prefix+((curImg.substr(1)).toInt()-1).toString();

	var clickedImgName = 'portImg'+imgID.toString();
	var lastImgName = 'portImg'+curImg.toString(); 

	$(lastImgName).removeClass('portImgShow');
	$(lastImgName).addClass('portImgHide');
	$(clickedImgName).removeClass('portImgHide');
	$(clickedImgName).addClass('portImgShow');
					
	
	//  set the selelcted thumbnail to ON, and turn the previous selected thumbnail OFF
	$(imgID).removeClass('imgNoFrame');
	$(imgID).addClass('imgFrame');
	$(curImg).removeClass('imgFrame');
	$(curImg).addClass('imgNoFrame');

	//  reset the currently selected thumbnail
	curImg = imgID;

}

//---------------------------------------------------------------------------
//  FUNCTION: Mouseout of a thumbnail - remove frame
//---------------------------------------------------------------------------
function mouseOut(imgID) { 
	if (imgID != curImg) {
		$(imgID).removeClass('imgFrame');
		$(imgID).addClass('imgNoFrame');
	}
}


//---------------------------------------------------------------------------
//  FUNCTION: Check for browsers N4/IE4/IE5 Mac or older....
//---------------------------------------------------------------------------
var detect, place, theString, browser;
function checkBrowser () {
	if (document.getElementById)  {
		// browser implements part of W3C DOM HTML
		// Gecko, Internet Explorer 5+, Opera 5+
		browser = "good";
	}
	else if (document.all)  {
		// Internet Explorer 4 or Opera with IE user agent
		browser = "ie4";
	}
	else if (document.layers) {
		// Navigator 4
		browser = "net4";
		location.href = "oldbrowser.html";
	}
	
	detect = navigator.userAgent.toLowerCase();
	//if (detect.indexOf("safari") != -1)
		//linkCSS('safari');

	if (checkIt('msie')) { // browser is IE
		var version = detect.charAt(place + theString.length);
		if (checkIt('mac'))
			location.href = "maciebrowser.html";
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	theString = string;
	return place;
}

//----------------------------------------------------------------------
// FUNCTION: open a new window
//----------------------------------------------------------------------
function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}
