/*
//Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18398419-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
*/
$(document).ready(function() {				
	previous = "";
	AmountOfPages = 8;
	ProjectDescriptionsArray = new Array();
	DetermineDivHeights();
	PlaceTentacleGFX();	
	
    $('#projectdescriptions').bind("scroll", function(event) {	    	// Bind Actions to Scroll Event
		for (i=AmountOfPages; i>=1; i--) {								// Add all top positions of Project Descriptions to an Array

			ProjectDescriptionsArray[i]=$("#"+i).offset().top;			// From High-To-Low, Test to see if it is over the VISIBLE-LINE
			if (ProjectDescriptionsArray[i]<=Visiline){
				break;													// If so, break operation
				};
		}																// End of For Loop
		if (i != previous){												// Perform Swotch only once - After For Loop break out.
			swotch(i);
			previous = i;
		};	
    });
	
	$(window).resize(function() {
		DetermineDivHeights();
		PlaceTentacleGFX();
	});
	
});

//Showing and hiding the "Read more" Links.
function ToggleMe(target, textToggle){
	$(target).toggle(400);
	var MoreOrLess=document.getElementById(textToggle.id);
	test = MoreOrLess.innerHTML.indexOf("more");
	if (test>=0){
		document.getElementById(textToggle.id).innerHTML = '...Show less';
	} else {
		document.getElementById(textToggle.id).innerHTML = '...Show more';
	}	
}

//Showing and hiding the "Download" Links.
function ToggleMeDL(target, textToggle){
	$(target).toggle(400);
	var MoreOrLess=document.getElementById(textToggle.id);
	test = MoreOrLess.innerHTML.indexOf("...Downloads");
	if (test>=0){
		document.getElementById(textToggle.id).innerHTML = '...Hide Downloads';
	} else {
		document.getElementById(textToggle.id).innerHTML = '...Downloads';
	}	
}

//Switch "Showme"-page ON and the rest OFF
function swotch(showme){
	for (var i=1; i<=AmountOfPages; i++) {
		if (i==showme){
			showmenow = "#Page"+showme 
			$(showmenow).fadeIn("fast");
		alert:(i);				
		} else {
			hideme = "#Page"+i;
			$(hideme).hide();
		}
	}
/* BGR(showme); */
}

//Idevice stuff
function isIPad(){
    return navigator.platform == "iPad";
}

function isIPhone(){
    return navigator.platform == "iPhone";
}

if (isIPad()){
alert("Dear iPad user, \n please use two fingers to scroll \n the text on this page.");
};

if (isIPhone()){
alert("Dear iPhone user, \n please use two fingers \n to scroll the text \n on this page.");
};


function BGR(showme){
	switch(showme)
	{
	case 2:
		document.body.background="images/Nimbling_BGR.gif";
		break;
	case 4:	
		document.body.background="images/Nimbling_InnerShadow.gif";
		break;
	case 6:
		document.body.background="images/Nimbling_Fabric.jpg";	
		break;
	}
	}

//Determine DIV heights and the "Visiline" that determines when to switch project thumbs
function DetermineDivHeights(){
	projectdescriptionsheight = ($(window).height()-360)+"px"; //contentarea = 300, middle bit is 28, bottom margin = 50ish
	projectdescriptionstop = $('#projectdescriptions').offset().top;
	Visiline = (($(window).height()+projectdescriptionstop)/2)+80;
	if ($(window).height()> 900){
			Visiline = ($(window).height()-180);
		}
	$('#projectdescriptions').height(projectdescriptionsheight);
}

function PlaceTentacleGFX(){
	var tentaclePosLeft = (($('#projectdescriptions').offset().left)-1170);
	$('#tentacleBGR').css({"left": (tentaclePosLeft) + "px"});
	$('#tentacleBGR').css({"overflow": "visible"});
}

// Gets an Images Filename without the path and extension.
function FileName( szFile, iTrim )
{   return szFile.substring(szFile.lastIndexOf("/") + 1, szFile.length - iTrim);
}

// Image Mouse Over/Out Effects
function MEffect( oEvent, szDir )
{  var oTarget;
  if( oEvent.srcElement ) oTarget = oEvent.srcElement;
  else if( oEvent.target ) oTarget = oEvent.target;

  switch( oEvent.type )
  {
     case "mouseover":
        oTarget.src = szDir + "/" + FileName(oTarget.src, 4) + "_O.png";
     break;

     case "mouseout":
        oTarget.src = szDir + "/" + FileName(oTarget.src, 6) + ".png";
     break;
  }
}
