// flag to detect finger click
var iframeLinkClicked = 0;

$(document).ready(function(){
        
       
        //Show hand on scroll 
        $(window).scroll(function () { 
          displayPosition = $(window).scrollLeft() + $(window).width();
          if (iframeLinkClicked == 0)
              if (displayPosition > 5000)
                $("#navigation").fadeIn("slow"); 
              //else
              //  $("#navigation").fadeOut("slow");  
        });
         
    
    //Finger Clicks
//    $("#navigation a").click(function(){
//        $("#navigation").css('backgroundImage','url(images/hand_close.png)'); 
//        $(".nav_fingers").hide();
//        $("#arrows").hide();
//        $("#opencloseHand").fadeIn("slow"); //.css('display','block');
//        
//        $("iframe").css({'width':'100%'}).attr("scrolling", "yes");
//		
//		$("#opencloseHand a").html("X Open hand");
//		
//        iframeLinkClicked = 1;
//    });
    
    
    $("#navigation a").click(function(){
        $("#arrows").hide();
        $("#navigation").css({'position':'absolute', 'bottom':'0px', 'left':'0px'});
        $("#navigation").css('backgroundImage','url(images/hand_close.png)');
        $(".nav_fingers").hide();
        $("#opencloseHand").fadeIn("slow");
        $("#opencloseHand").show();
        
    });
    
    function setIframeProperties(url)
    {
         // Hide homepage background
        $("#container").hide();
        // Show iframe set URL
        $("#iframe").show();
        $("#iframe").attr("src", url);
		$("#toolbox").show();
    }
    
    $("#navigation .one a").click(function(){
        setIframeProperties("http://www.sense.org.uk");
		$("#topText").hide();
		$("#opencloseHand").css("background","url(images/show_hand.png) no-repeat");
    });
    
    $("#navigation .two a").click(function(){
        setIframeProperties("http://www.facebook.com/srch.php#/pages/sensebook/103789761062?ref=mf");
		$("#topText").hide();
		$("#opencloseHand").css("background","url(images/show_hand.png) no-repeat");
    });
    
    $("#SensePics").click(function(){
        setIframeProperties("http://www.flickr.com/search/?q=taste+OR+smell+OR+touch+OR+sight+OR+sound+OR+sensegalleryexhibition+OR+sensefriendspics");
        $("#topText").show().html("<p><strong>Have a flick through some sensual snaps.</strong><br /> - Also, tag your pics with any of the five senses i.e. (smell, taste, touch, sight or sound) and you could feature here.</p>");
		$("#opencloseHand").css("background","url(images/show_hand.png) no-repeat");
    });
    
    $("#SenseVids").click(function(){
        setIframeProperties("http://www.youtube.com/user/SenseCharity");
		$("#topText").hide();
		$("#opencloseHand").css("background","url(images/show_hand.png) no-repeat");
    });
	
	$("#SenseMarks").click(function(){
        setIframeProperties("http://sensehub.enjoysthin.gs/");
        $("#topText").show().html("<p><strong>Dig through our collection of sensual bookmarks.</strong><br /> - And add a few of your own, by emailing recommendations <a href='mailto:sensehub@gmail.com'><strong>here</strong></a>:</p>");
		$("#opencloseHand").css("background","url(images/show_hand.png) no-repeat");
    });
    
    $("#navigation .four a").click(function(){
        setIframeProperties("http://sensehub.enjoysthin.gs/");
		$("#topText").hide();
		$("#opencloseHand").css("background","url(images/show_hand.png) no-repeat");
    });
    
    $("#navigation .five a").click(function(){
        setIframeProperties("http://www.sensori.org.uk");
		$("#topText").hide();
		$("#opencloseHand").css("background","url(images/show_hand.png) no-repeat");
    });
    
	//Open/Close Hand
    //$("#opencloseHand").toggle(
//      function () {
//        $("#navigation").css('backgroundImage','url(images/hand_open.png)');
//        $(".nav_fingers").show();
//		$("#opencloseHand").css('backgroundImage','url(images/close_hand.png)');
//      },
//      function () {
//        $("#navigation").css('backgroundImage','url(images/hand_close.png)');
//		$("#opencloseHand").css('backgroundImage','url(images/show_hand.png)');
//      }
//    );
	
	//Open/Close Hand
    $("#opencloseHand").click(function(){
	    $("#navigation").css('backgroundImage','url(images/hand_open.png)');
        $(".nav_fingers").show();
		$("#opencloseHand").css('background','none');
	});
    
    // Close - Top right corner button
    $("#toolbox a").click(function(){
        window.location = 'index.html';
        //alert($(".#iframe").attr("src") );
    });
});


function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}


window.onload = function() {
    document.getElementById("navigation").style.display = 'none';
	HtinyScrolling.init(); scrollTips.init();
	
	}

var HtinyScrolling = {
	speed : 80,      //set here the scroll speed: when this value increase, the speed decrease. 
	maxStep: 150,	 //set here the "uniform motion" step for long distances
	brakeK: 3,		 //set here the coefficient of slowing down
	hash:null,		
	currentBlock:null,
	requestedX:0,
	init: function() {
		if(navigator.appVersion.indexOf('AppleWebKit')>0) {
			document.getElementById('arrows').style.display = 'none';
		}
		var lnks = document.getElementsByTagName('a');   
		for(var i = 0, lnk; lnk = lnks[i]; i++) {   
			if ((lnk.href && lnk.href.indexOf('#') != -1) &&  ( (lnk.pathname == location.pathname) ||
			('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {  
			lnk.onclick = this.initScroll;   		
			}   
		}    
	},
	getElementXpos: function(el){
		var x = 0;
		while(el.offsetParent){  
			x += el.offsetLeft;    
			el = el.offsetParent;
		}	return x;
	},		
	getScrollLeft: function(){
		if(document.all) return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		else return window.pageXOffset;   
	},	
	getWindowWidth: function(){
		if (window.innerWidth)	return window.innerWidth; 
		if(document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
	},
	getDocumentWidth: function(){
		if (document.width) return document.width;
		if(document.body.offsetWidth) return document.body.offsetWidth;
	},
	initScroll: function(e){
		var targ;  
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;   
		HtinyScrolling.hash = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length); 
		HtinyScrolling.currentBlock = document.getElementById(HtinyScrolling.hash);   
		if(!HtinyScrolling.currentBlock) return;
		HtinyScrolling.requestedX = HtinyScrolling.getElementXpos(HtinyScrolling.currentBlock); 
		HtinyScrolling.scroll(targ); 
		return false;
	},
	scroll: function(targ){
		var left  = HtinyScrolling.getScrollLeft();
		if(HtinyScrolling.requestedX > left) { 
			var endDistance = Math.round((HtinyScrolling.getDocumentWidth() - (left + HtinyScrolling.getWindowWidth())) / HtinyScrolling.brakeK);
			endDistance = Math.min(Math.round((HtinyScrolling.requestedX-left)/ HtinyScrolling.brakeK), endDistance);
			var offset = Math.max(2, Math.min(endDistance, HtinyScrolling.maxStep));
			
		} 
		else 
		{ 
		    var offset = - Math.min(Math.abs(Math.round((HtinyScrolling.requestedX-left)/ HtinyScrolling.brakeK)), HtinyScrolling.maxStep);
		} window.scrollTo(left + offset, 0);  
		if(Math.abs(left-HtinyScrolling.requestedX) <= 1 || HtinyScrolling.getScrollLeft() == left) {
			window.scrollTo(HtinyScrolling.requestedX, 0);
			if(typeof XULDocument != 'undefined') {
				location.hash = HtinyScrolling.hash;
			}
			if(window.opera) {mark.change_colors(HtinyScrolling.hash, 30, 3500,'#aaaaaa','#fffbea');}
			mark.change_opacity(HtinyScrolling.hash);
			HtinyScrolling.hash = null;
		} else 	setTimeout(HtinyScrolling.scroll,HtinyScrolling.speed);			
	}
}

/* the mouse scrolling doesn't work with Opera, that hasn't a event associated to the mouse wheel */
 
var scrollTips = {
	dx : null,
	init : function() {	
		if (window.addEventListener) {
		window.addEventListener("DOMMouseScroll", this.mouseScroll, false);
		} else 	window.onmousewheel = document.onmousewheel = this.mouseScroll;
		var right = document.getElementById('arrowright');
		right.onmouseover = function() {this.dx=setInterval('scrollTips.arrowScroll(1)',100); 
		    //alert(document.body.offsetWidth);
		return false;
		}
		right.onmouseout = function() { clearInterval(this.dx); return false;}
		var left = document.getElementById('arrowleft');
		left.onmouseover = function() {this.dx=setInterval('scrollTips.arrowScroll(0)',100);return false;}
		left.onmouseout = function() { clearInterval(this.dx); return false;}
	},

	mouseScroll : function(e) {
		if (!e) var e = window.event;
		if (window.opera) {delta = event.wheelDelta/120; delta = -delta;}
		if (e.wheelDelta <= 0 || e.detail>=0){  
		window.scrollBy(80,0);
		} else  window.scrollBy(-80,0) ; 
	} ,
	
	arrowScroll: function(val) {
		if(val==1) {
			window.scrollBy(70,0);
		} else {
			window.scrollBy(-70,0)
		}
	}
}

