	
// JavaScript Document
$(document).ready(function(){
	replaceJovers();
	replaceJovers("pro_box");
	replaceJovers("cont_banner");
	
	//FAQS
	var faqs = $('.question_answer');
	if(faqs.length > 1){
		//setupSwfAddress();
		faqs.each(function(index){
			var faq			= $(this);
			var id 			= faq.attr('id').substr("question_answer_".length);
			var question	= faq.find("h3");
			question.wrapInner('<a>');
			var question_link	= question.find("a");			
			var answer		= faq.find("div");
			var all_answers	= $('.question_answer div');
			answer.hide();
			
			question.click(function(){
				var is_open = (answer.css('display')!='none');
				
				
				//if(is_open) changeAddress('');
				//else changeAddress("q"+id);
				
				$('.question_answer div').slideUp(500);
				$('.question_answer h3 a').removeClass("selected")
				if(is_open){
				}else{
					question_link.addClass("selected")					
					answer.slideDown(500);
				}
				
			})
			
			question.hover(function(){
				question.css('cursor',"pointer");
			},function(){
				question.css('cursor',"auto");
			});
		});
		var l = window.location.href;
		var id = l.substr(l.indexOf("#")+2);
		if(id != "")	$('#question_answer_'+id +' h2').click();

	}
	
	//Prod images
	//Product section jovers
	var current;
	var box = $('.product_line');	
	box.each(function(index){
	
		var t = $(this);
		var h2 = $(this).find('h2 a');
		var images = $(this).find('img');
		var a = $(this).find('a.active');
		a.wrap('<span class="button_span" />');
		var spa = $(this).find('span.button_span');
		var p = spa.parent();
		
		var over = function(){
			spa.addClass('span_over');
			p.addClass('hover');		  
			t.css('cursor',"pointer");
		}
		
		var out = function(){
			spa.removeClass('span_over');
			p.removeClass('hover');
			t.css('cursor',"default");
		}
		
		h2.hover(over,out);
		images.hover(over,out);		
		images.click(function(){
			window.location = a.attr("href");
		});
		
		
	});
/*	var over = function(){
		box.css('cursor',"pointer");
		images.css('cursor',"pointer");
		var l = $(this).parent().parent().find("a.active");
		var p = l.parent();
		spa.addClass('span_over');
		p.addClass('hover');
		l.
	}
	var out = function(){
		box.css('cursor',"pointer");
		images.css('cursor',"pointer");
	}*/
	
	//Product image...
	//We want to preload them all
	var product_image_holder = $('.product_image');	
	var product_image = $('.product_image img');
	var product_thumbs = $('.product_thumbs img');
	var h1;
	var h2;
	var preloads = new Array();
	product_thumbs.each(function(index){
		var l = $(this).attr("data-full");
		var h = $(this).attr("data-height");
		if(l){
			//Preload
			preloads.push(new Image());
			preloads[preloads.length-1].src = l;
			
			//Add click
			$(this).wrap("<a href='#' onclick='return false;'>");
			var a = $(this).find("a");
			$(this).click(function(){
				product_image.fadeOut("medium",function(){
					//Resize:
					product_image_holder.height(product_image.height());
					product_image.attr("src",l);
					product_image_holder.animate({height:h},"fast",function(){
						product_image.fadeIn("medium");
					});
				});
			})
		}
	});
	
	
	
	/*
	$('.launch a').qtip({
   content: {
            // Set the text to an image HTML string with the correct src URL to the loading image you want to use
            text: 'ERRRR',
            url: "http://www.google.com", // Use the rel attribute of each element for the url to load
            title: {
               text: 'Wikipedia - ' , // Give the tooltip a title using each elements text
               button: 'Close' // Show a close link in the title
            }
         },
   show: 'mouseover',
   hide: 'mouseout'
	})*/
	
	// Call me popup
   $('.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");
	  }	
   });
	

	$('.post .entry').find("a.more-link").addClass("button").addClass("read_more");
	

	$('.post .entry').find("a").each(function(){
		var a = $(this);
		var href = a.attr("href");
		var target = a.attr("target");				
		var s = href.split(".");
		var type = s[s.length-1];
	
		
		if(type == "pdf"){
			a.addClass("button");
			a.addClass("down");
		}
		
		if(target == "_blank"){
			a.addClass("button");
			a.addClass("next");
		}
		
		if(type == "jpg" || type == "gif" || type == "png"){
			a.fancybox();	
		}
	});
	
	
	
	
});


function changeAddress(address){
	window.location.href = "#/" + address;
	return false;
}

function setupSwfAddress(){

	var addressChange = function(e){

		//Get parts
		var v = e.value;
		if(v.substr(0,1) == "/") v = v.substr(1);
		if(v.substr(0,1) == "q") v = v.substr(1);		

		//If it's blank
		$('.question_answer p').slideUp(500);		
		if(v == ''){
			return true;	
		}else{
			$('#question_answer_'+v+' p').slideDown(500);
			return true;
		}
		

	}
	
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, addressChange);
}

function submitNicEdit(d){
	try{
		for(var e in nicEditors.editors){
			for(var i in nicEditors.editors[e].nicInstances) nicEditors.editors[e].nicInstances[i].saveContent();	
		}
	}catch(e){
		
	}
}

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');
		jover.addClass("jover_active");		
		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
		if(over){
			var img = new Image();
			img.src = over;
		}
	});
	
}

