// JavaScript Document
$(document).ready(function(){
	//replaceJovers();
	
	showBoxes(".project div",0.8,200);
	showBoxes(".icons .nc_1",0.8,200);
	setupBoxes(".icons .nc_1",0.9,200);
	
//	window.setTimeout(showIgloo, 2000);
	
	// Call me popup

	/*$('.email').each(function(){
		var t = $(this);
		$(this).qtip({
			content: {
				url: "/simple_contact_form"
			},
			position: {
				corner: {
					target: 'bottomMiddle', 
					tooltip: 'topRight'
				}
			},
			show: { 
				when: 'mouseover', 
				delay: 200,
				'effect':{
					'type':'fade',			
					'length':200
				},
				solo: true // Only show one tooltip at a time
			},
			hide: {
				'when': 'mouseout',
				'effect':{
					'type':'fade',			
					'length':200
				},
				delay: 500,	
				'fixed': true
			},
			style: {
				tip: {
					corner: 'topRight'
				},
				border: {
					width: 8,
					radius: 0,
					color: '#089DF9'
				},
				name: 'light', // Use the default light style
				width: 220 // Set the tooltip width
			}
		 });
		var api = $(this).qtip("api");
		$(this).click(function(){api.show();return false;});
		
		api.onShow = function(){
			t.addClass("selected");
		}
		api.beforeHide = function(){
			t.removeClass("selected");
		}	
	});
	*/
	
	/*
   $('.call_back a').each(function()
   {
  
	  var t = $(this);
      $(this).qtip(
      {
         content: {
            // Set the text to an image HTML string with the correct src URL to the loading image you want to use
			url: "/callback_form", // Use the rel attribute of each element for the url to load
         },
         position: {
            corner: {
               target: 'rightMiddle', // Position the tooltip above the link
               tooltip: 'leftTop'
            },
            adjust: {
               	x : 0,
				y : -115
            }
         },
         show: { 
            when: 'mouseover', 
			delay: 200,
			'effect':{
				'type':'fade',			
				'length':200
			},
            solo: true // Only show one tooltip at a time
         },
         hide: {
			'when': 'mouseout',
			'effect':{
				'type':'fade',			
				'length':200
			},
			delay: 500,	
			'fixed': true
		 },
         style: {
            tip: {
				corner: 'leftMiddle',
				
			},// Apply a speech bubble tip to the tooltip at the designated tooltip corner
            border: {
               width: 4,
               radius: 0,
			   color: '#EA0A50'
            },
            name: 'light', // Use the default light style
            width: 440 // Set the tooltip width
         }
      })
		var api = $(this).qtip("api");
		$(this).click(function(){api.show();return false;});
		
	  api.onShow = function(){
		t.addClass("selected");
	  }
	  api.beforeHide = function(){
		t.removeClass("selected");
	  }	
   });
	
	*/
	
	
	
	
	
});


function showIgloo(){

}

function randomiseArray(){
	return (Math.round(Math.random())-0.5);
}

function showBoxes(selector,time,speed){

	var boxes = $(selector);
	
	var total = boxes.length;

	boxes.sort( randomiseArray );

	var interval = time * 1000 / total;

	var delay = 250;

	boxes.each(function(index){
		var box = $(this);
		//if(index == 0) box.parent().show();
		unhide(box.parent());
		box.fadeTo(0,0);
		box.show();		
		window.setTimeout(function(){box.fadeTo(speed,1)}, index * interval + delay);
	});

}

function unhide(obj){
	obj.css("opacity",1);
	obj.css("filter","alpha(opacity=100)");	
}



function setupBoxes(selector,opacity,speed){

	var boxes = $(selector);
	
	boxes.each(function(index){
		var box = $(this);
		
		//Add elements for rollovers and background
		//box.wrapInner("<div class='content'>");
		//box.append("<div class='content_background'>&nbsp;</div>");
		
		//Assign variables to elements
		var content = box.find("div");
		//var content_background = box.find(".content_background");

		//var padding = box.css("padding");
		//box.css("padding","0");
		//content.css("padding",padding);
		//Set width of transparency

		//content_background.width(box.width());
		//content_background.height(box.height());

		//Css the content boxes
		//content.css({position:'absolute','z-index':1});
		//content_background.css({'background':'#089df9','position':'absolute'});
		
		//Set opacity to zero
		content.fadeTo(0,0);
		//content_background.fadeTo(0,0);
		
		//Hover over
		box.hover(function(){

		  	content.stop();
			//content_background.stop();
			
			content.fadeTo(0,opacity);
			//content_background.fadeTo(0,100);
			
			box.css('cursor','pointer');
			
		},function(){
		  	content.fadeTo(speed,0);
			//content_background.fadeTo(speed,0);
			
			box.css('cursor','default');
		});
		
		//On click
		box.click(function(){
			var a = box.find("a");
			window.location.href = a.attr("href");
		});
		
	});	
}


function replaceJovers(className,overClass,active){
	if(!overClass && overClass !== false) overClass = "over";
	var jovers = $(className ? '.'+className : '.jover');
	jovers.each(function(index){
		var jover = $(this);
		var over = jover.attr("data-jover");
		var out = jover.attr("data-joff");
		var href = jover.find("a").attr("href");
		if(active) href = jover.find("a."+active).attr("href");
		var anc = $(this).find('a.button');
		var active_anc = anc;
		if(active) active_anc = $(this).find('a.button.'+active);
		active_anc.wrap('<span class="button_span" />');
		var spa = $(this).find('span.button_span');
		if(active) spa = $(this).find('span.button_span');
		var p = $(this).find('p');
		jover.hover(function(){
			if(over) jover.css('backgroundImage',"url('"+over+"')");
			jover.css('cursor',"pointer");
			if(overClass) jover.addClass(overClass);
			//anc.addClass('button_over');
			if(overClass) spa.addClass('span_over');
			if(overClass) p.addClass('hover');
		},function(){
			if(out) jover.css('backgroundImage',"url('"+out+"')");
			jover.css('cursor',"auto");
			if(overClass) jover.removeClass(overClass);
			//anc.removeClass('button_hover');
			if(overClass) spa.removeClass('span_over');
			if(overClass) p.removeClass('hover');
		});
		jover.click(function(){
			window.location.href = href;
		})
		//Preload
		var img = new Image();
		img.src = over;
	});
	
}
