// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ---------------   APLUS NEW MEDIA  Javascript Behaviours   -----------------
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// Make CrossBrowser Decisions
// ---------------------------------------------

// Decide which ADD syntax to use
function addEvent(obj, evType, fn){
 if (obj.addEventListener){obj.addEventListener(evType, fn, true); return true;}
 else if (obj.attachEvent){var r = obj.attachEvent("on"+evType, fn); return r;}
 else { return false; }

// Decide which SOURCE and TARGET syntax to use
 if (typeof e == 'undefined') {var e = window.event;}
 var source;
 if (typeof e.target != 'undefined') {source = e.target;}
 else if (typeof e.srcElement != 'undefined') {source = e.srcElement;}
 else {return false;}
}

// Onload Helper
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function'){window.onload = func;}
  else { window.onload = function(){oldonload(); func();}
  }
}




// Build Generic Functions
// --------------------------------------------

// Toggle visibility of any given element

function toggleVis(elem){
	if(elem){
		var vis;
		if(vis==false)
		{elem.style.visibility = 'visible'; vis=true;}
		else
		{elem.style.visibility = 'hidden'; vis=false;}
	}
}
function setVis(e){
	if(e){e.style.visibility = 'visible';}
}
function setInvis(e){
	if(e){e.style.visibility = 'hidden';}
}
function popWin(url, left, top, width, height){
	var win=0;
  	win = window.open(url, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	win.focus();
}


// Objects
// --------------------------------------------


	function FormsBehaviour(){

		// Properties
		// =====================================================================
		var t = this;
		t.forms = document.getElementsByTagName('form');
		t.inputs = document.getElementsByTagName('input');

		// Methods
		// =====================================================================
		t.showFocus = function(){
			if(t.forms){
				for(i=0; i<t.inputs.length; i++){
					if(t.inputs[i].type=='text'){
					t.inputs[i].onfocus = function(){this.style.border = '1px solid orange'; this.style.backgroundColor = '#fff'; this.style.color = '#333';}
					t.inputs[i].onblur =  function(){this.style.border = '1px solid #333'; this.style.backgroundColor = '#fff'; this.style.color = '#333';}
					}
				}
			}
		}

		t.focusFirstField = function(){
			if(t.inputs[0]){
				if(t.inputs[0].value==''){
				t.inputs[0].focus();
				}
			}
		}


		// Initialize
		// =====================================================================
		t.showFocus();
		t.focusFirstField();

	}




	function ImageFader(elem){
			// Originally From Clagnut.com
			// Adapted to be Object Oriented by Aaron Glenn

			// Properties
			var t = this;
			t.obj = elem;
			if(!t.obj){return false}
			t.sty = t.obj.style;
			t.step = 10;

			// Methods
			t.setOpacity = function (opacity){
				opacity = (opacity == 100)?99.999:opacity;
				t.sty.filter = "alpha(opacity:"+opacity+")";	// IE/Win
				t.sty.KHTMLOpacity = opacity/100;	// Safari<1.2, Konqueror
				t.sty.MozOpacity = opacity/100;	// Older Mozilla and Firefox
				t.sty.opacity = opacity/100;	// Safari 1.2, newer Firefox and Mozilla, CSS3
			}

			t.fadeIn = function (opacity) {
				if (opacity <= 100) {
					t.setOpacity(opacity);
					opacity += t.step;
					var func = "t.fadeIn("+ opacity +");";
					var fading = window.setTimeout(func, 100);
				}
			}

			t.initImage = function () {
				t.setOpacity(0);
				t.sty.visibility = 'visible';
				t.fadeIn(0);
			}

			// Constructor
			var init = t.initImage();
	}

	// Adds Inner Shadow
	function InnerShadow(elem){
		var t = this;
		t.defaultShadow = function(){
			if(elem){
			var shad = document.createElement('div');
			elem.appendChild(shad);
			shad.className = "innershadow";
			shad.style.width = '400px';
			}
		}
		t.defaultShadow();
	}



	function ImageSlider(elem, height, controls){

		//Constructor
		t = this;
		t.obj = elem;
		if(!t.obj){return false}
		t.img = t.obj.getElementsByTagName('img')[0];
		if(!t.curY){t.curY = 0;}
		else{t.curY  = parseFloat(t.img.style.top);}

		t.curX  = parseFloat(t.img.style.left);

		// Properties
		t.height 	= height;
		t.sliding 	= false;
		t.dir   	= '-';
		t.controls 	= '';
		t.images 	= 3;
		t.imageNum	= '';

		// Methods
		t.startSlide = function (endX, endY){
			
				difY = (Math.abs(endY) - Math.abs(t.curY));
				if(!difY){
					difY = 0;
				}
				//console.log(endY + ' ' +t.curY);
				//difX = (Math.abs(endX) - Math.abs(t.curX));
				difX = 0;
				movY = (t.dir=='-')?((difY)/-2):((difY)/2);
				movX = (t.dir=='-')?((difX)/-2):((difX)/2);
				if(!t.curY){t.curY = 0;}
				else{t.curY  = parseFloat(t.img.style.top);}
				t.curX  = t.img.style.left;

				t.img.style.top = (t.curY+movY)+"px";
								console.log( (t.curY+movY)+"px");
				//t.img.style.left = (t.curX+movX)+"px";
				if(movY!=0){
					var func = "t.startSlide("+ endX +", "+ endY +");";
					t.sliding = window.setTimeout(func, 20);
					}else{
					t.stopSlide();
				}
		}

		t.stopSlide = function (){
				t.sliding = window.clearTimeout(t.sliding);
		}

		t.initControls = function(){

			// Make Controller
			var listParent = document.createElement('ul');
			listParent.className = 'controls';

			// Build Controls
			c=0;
			for(c=0; c<3; c++){
				//alert('what');
				listItem = document.createElement('li');
				anchor = document.createElement('a');
				txt = document.createTextNode(c+1);
				anchor.appendChild(txt);
				anchor.href = '#';
				anchor.posY = (-(c*150));
				anchor.posX = 0;
				anchor.onclick = function(){
									t.startSlide(this.posX, this.posY);
									t.setUnderline(c);
									return false;
								}
				listItem.appendChild(anchor);
				listParent.appendChild(listItem);
			}

			// Add Controls Port Images
			t.obj.appendChild(listParent);
		}

		t.setUnderline = function(num){
			//for(i=0; i<t.controls.length; i++){t.controls[i].style.textDecoration = 'none';}
			//t.controls[t.imgNum].style.textDecoration = 'underline';
			//t.controls[t.imgNum].blur();
		}
		t.initControls();
	}

	function initPortfolio(){
		var alldivs = document.getElementsByTagName('div');
		var sliders = new Array();
		var shadows = new Array();
		var x = 0;
		for(i=0; i<(alldivs.length); i++){
			if(alldivs[i].className == 'port-image'){
				portImage = alldivs[i];
				//sliders[x] = new ImageSlider(portImage, '150');
				shadows[x] = new InnerShadow(portImage);
				//faders[x] = new ImageFader(name);
				x++;
			}
		}
	}


	function FlashObject(id){
		var t = this;
		t.path = '';
		t.swf = '';
		t.width = '';
		t.height = '';
		t.flashVars = '';
		t.addParams = '';
		t.flashObj = document.getElementById(id);
		if(!t.flashObj){return false}

		// Methods
		t.writeFlash = function(){

		}
	}

/*
	// Flash Photo Viewer Object
	// ========================================
	//
	//	Overview: 	This is a JS Object that interacts with a flash movie to display photos
	//				It requires an array of photos to loop through and the flash swf movie
	//
	//	Arguments:	Array data 		: Any array of photos
	//				String flash 	: Name of flash object/embed
	//				String inline 	: ID of the inline image
	//				String holder	: ID of the holder div
	//
	//  Methods:	hideInline		: Hides the inline image if JS is enabled
	//				showFlash		: Displays the flash file if JS is enabled
	//				setControls		: Assigns onClicks for the controlling links
	//				loadPhoto		: Talks to flash and loads a photo into the viewer, defaults to the main image
	//				loadPhotos		: Calls three instances of the loadPhoto method to load all three images at once



	function PhotoViewer(data, flash, inline, holder){

		// Properties
		// ====================================
		var t = this;
		t.flash = (window.photoViewer)?(window.document["photoViewer"]):(document.photoView);
		t.holder = holder;
		t.photos = data;
		t.image = image;
		t.curNum = 3;
		t.randNum = math.floor(random(t.photos.length));


		// Methods
		// =====================================
		t.hideInline = function(){
			var static_image = document.getElementById(t.image);
			if(!static_image){return false}
			static_image.style.display = 'none';
		}

		t.showFlash = function(){
			var flash_viewer = document.getElementById(t.holder);
			flash_viewer.style.display = 'block';
		}

		t.setControls = function(){
			var prev = document.getElementById('prev-link');
			var next = document.getElementById('next-link');
			if((!prev)||(!next)){return false;}
			prev.onclick = function(){loadPhotos('prev'); this.blur(); return false;}
			next.onclick = function(){loadPhotos('next'); this.blur(); return false;}
		}

		t.loadPhoto = function(file, mc='img_main'){
			t.flash.SetVariable(mc, file);
			t.flash.SetVariable("doLoad", 'true');
		}

		t.loadPhotos = function (direction){
			var num = (direction == 'next')?(1):(-1);
			t.main = t.photos[t.curNum+(num)];
			t.next = t.photos[t.curNum+(num+1)];
			t.prev = t.photos[t.curNum+(num-1)];
			t.curNum = t.curNum + (num);
			t.loadPhoto(t.main, "img_main");
			t.loadPhoto(t.next, "img_next");
			t.loadPhoto(t.prev, "img_prev");
		}

		// Constructor
		// =====================================
		t.init = function (){
			t.hideInline();
			t.showFlash();
			t.setControls();
		}

		// Initialize
		var init = t.init();

	}
*/



	function loadPhoto(mc, file){
		var flashViewer = (window.photoViewer)?window.document["photoViewer"]:document.photoView;
		flashViewer.SetVariable(mc, file);
		flashViewer.SetVariable("doLoad", 'true');
	}

	function loadPhotos(direction){
		var num = (direction == 'next')?(1):(-1);
		mainPhoto = photoArray[curNum+(num)];
		nextPhoto = photoArray[curNum+(num+1)];
		prevPhoto = photoArray[curNum+(num-1)];
		curNum = curNum + (num);
		loadPhoto("img_main", mainPhoto);
		loadPhoto("img_next", nextPhoto);
		loadPhoto("img_prev", prevPhoto);
	}

	function initPhotos(){
		var static_image = document.getElementById('photo-image');
		var flash_viewer = document.getElementById('flash-holder');
		if(!static_image){return false}
		static_image.style.display = 'none';
		flash_viewer.style.display = 'block';
		var prev = document.getElementById('prev-link');
		var next = document.getElementById('next-link');
		if((!prev)||(!next)){return false;}
		prev.onclick = function(){loadPhotos('prev'); this.blur(); return false;}
		next.onclick = function(){loadPhotos('next'); this.blur(); return false;}
	}


	// Tack on the Onloads
	addLoadEvent = function(){}

	window.onload = function(){initPortfolio(); initPhotos(); var form = new FormsBehaviour();}

