if( $('body').hasClass('home') ){
	initSlideshow();
	initTestemonials();
	initDemoText();
}
	
function initTestemonials(){
	var obj = $('#testimonials');
	var endPos = 100 * (obj.children().length - 1);
	
	var test_int = setInterval(function(){ 
		if(  obj.scrollTop() == endPos )
			obj.animate({ scrollTop:'0' }, 1200);
		else
			obj.animate({ scrollTop:'+=100' }, 1000);
	}, 10000);
}

function initSlideshow(){
	var divObj = $('#slideshow div.inner > div'),
		ulObj = $('#slideshow ul'),
		imgObjWidth = 455;
		speed = 100,
		animated = false,
		itemsCount = ulObj.find('li').size(),
		itemsPos = 0,
		rotate = true,
		t = 0;
	
	var positionDots = $('<ul/>').addClass('position').hide();
	for (var i=0; i < itemsCount; i++){
		var li = $('<li/>')
		if(i > 0) 
			li.fadeTo(0, 0.3);
		else
			li.addClass('active');
			
		positionDots.append(li);
	}
	var positionDotsChild = positionDots.children();

	positionDots.appendTo('#slideshow').fadeIn(1500);
	
	var desc = $('<div/>');
	desc.addClass('desc');
	desc.html( ulObj.find('li.active img').attr('alt') );
	$('#slideshow').append(desc);
	desc.animate({ top:-26 },300);
	
	$('#slideshow b.next').hover(function(){
			$(this).stop().animate({ right:-3 }, speed);
		}, function(){  
			$(this).stop().animate({ right:3 }, speed*2);
	}).siblings('b.prev').hover(function(){
			$(this).stop().animate({ left:-3 }, speed);
		}, function(){  
			$(this).stop().animate({ left:3 }, speed*2);
	});
	
	function aniBtns(btn, pos){
		$(btn).stop().animate({ left:pos }, speed);
	}
	
	$('#slideshow b').click(function(){
		clearInterval(t);
		var direction = $(this).hasClass('next') ? 'next' : 'prev';
		fadeRotation(direction);
		setTimeout(function(){ setAutoRotate(); },6000);
	});
	
	positionDotsChild.click(function(){
		if( !$(this).hasClass('active') ){
			setAutoRotate();
			itemsPos = $(positionDotsChild).index( $(this)[0] );
			fadeImages(itemsPos);
		}
	});
	
	setAutoRotate();
	function setAutoRotate(){
		clearInterval(t);
		t = setInterval(function(){ fadeRotation('next'); },5000);
	};
	
	function fadeImages(pos){
		if( !animated ){
			positionDotsChild.filter('.active').removeClass('active').fadeTo(200, 0.3);
			positionDotsChild.eq(pos).addClass('active').fadeTo(500, 1);
			
			animated = true;
			desc.animate({ top:0 },300);
			
			ulObj.find('li.active').animate({ opacity:0 },800, function(){
				animated = false;
				$(this).removeAttr('class').removeAttr('style');
				var active = ulObj.find('li.active');
				active.removeAttr('style');
				desc.html( active.find('img').attr('alt') ).animate({ top:-26 },300);
			});
			ulObj.find('li').eq(pos).addClass('active').css('z-index',1);
		}
	}
	
	function fadeRotation(direction){
		//if( !animated && !(direction == 'next' && itemsPos == (itemsCount-1)) && !(direction == 'prev' && itemsPos == 0) ){
		if( !animated && !(direction == 'prev' && itemsPos == 0) ){
			if( direction == 'next' && itemsPos == (itemsCount-1) )
				itemsPos = 0;  // if last gallery element is visible and we clicked "next", then go to the first again (endless rotation)
			else
				itemsPos = (direction == 'next') ? itemsPos + 1 : itemsPos - 1;
			
			fadeImages(itemsPos);
		}
	}
}

$('#getItBtn, #getFFplugin').hover(function(){
	if( $(this).hasClass('png24') && $.browser.msie )
		$(this.firstChild).hide();
	else
		$(this.firstChild).stop().animate({ opacity:0 }, 200);
}, function(){
	if( $(this).hasClass('png24') && $.browser.msie )
		$(this.firstChild).show();
	else
		$(this.firstChild).stop().animate({ opacity:1 }, 200);
});

$('#getItBtn').click(function(){
	//pageTracker._setCustomVar( 2, "User Clicked GetIt Button", "Yes", 3 );
	//pageTracker._trackPageview();
	_gaq.push(['_trackEvent', 'GetIt_Button', 'clicked']);
});


function initDemoText(){
	/*
	var doubleClick = 0;
	
	$('#demoText').click(function(){
		doubleClick++;
		clearTimeout( t );
		var t = setTimeout(function(){ doubleClick = 0 }, 200);
		if( doubleClick > 1 ){
			var text = $(this).text();
			$(this).hide().after("<textarea id='demoTextEdit'>"+ text +"</textarea>");
		}
	});
	*/
	/*
	$(document).click(function(e){
		if( $('#demoTextEdit').length && e.target.id != 'demoTextEdit' && e.target.id != 'demoText' ){
			changeDemoText();
		}
	});
	*/
	
	function changeDemoText(){
		var text = $('#demoTextEdit').val();
		$('#demoTextEdit').remove();
		$('#demoText').html(text).show();
	}

	$('#demoTextBtn').toggle(function(){
			$('#demoText').hide().after("<textarea id='demoTextEdit'>"+ $('#demoText').text() +"</textarea>");
			this.firstChild.innerHTML = 'Annotate';
		},function(){
			this.firstChild.innerHTML = 'Edit Text';
			changeDemoText();
			try{
				setTimeout(function(){ headup.OnPageLoad(); }, 500);
			}
			catch(err){  }
	});
}

$("a[rel='ext']").attr("target","_blank");

$("a[rel='scroll']").live('click',function(){ 
	var target = $(this).attr('href');
	$(target).ScrollTo(600);
	if ( $(this).hasClass("highlight") )
		$(target).removeAttr("style").stop().effect("highlight", { color: "#d3dde5"}, 5000);
	return false; 
});

/*-------------------------
	Support Page
--------------------------*/
if( $('#content').hasClass('support') ){
	var prevElement = null;
	$('#content > div.faq ul > li > a').click(function(){
		if( prevElement ) $(prevElement).removeAttr('style');
		var selectedElement = $(this).attr('href');
		$(selectedElement).css({ color:'#0056D2' });
		prevElement = selectedElement;
	});
}

/*-------------------------
	Form page
--------------------------*/

if( $('#contactForm').length ){
	$('#contactForm div.item > :input').focus(function(){
		color = '#9cb4cc';
		if( $(this).parent('div.item').hasClass('item-error') )
			color = '#d79797';
		$(this).parent('div.item').css('border-color',color);
	}).blur(function(){
		$(this).parent('div.item').removeAttr('style');
	});
}

function mark(item){
	if( !$(item).parent().hasClass("item-error") )
		$(item).parent().addClass('item-error');
	$("#form_helper").text("Please check the marked fields");
	//$(item).parent().stop().removeAttr("style").effect("highlight", { color:"#ebd7d7" }, 3000);
}

function unmark(item){
	$(item).parent().removeClass('item-error');
	$("#form_helper").text("");
}

/* Contact us validation */

function ContactValidation(form){
	var jform = $(form);
	var method = jform.attr('method');
	var action = jform.attr('action');
	var data = {};
	
	var submit = true;
	var error = $(form).find("div.buttons > div.error")[0];
	var emailFilter=/^.+@.+\..{2,3}$/;
	var email_illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;

	if (form.name.value.length < 3){
		mark(form.name);
		submit = false;
	} else unmark(form.name);
	
	if (!emailFilter.test(form['email'].value) || form['email'].value.match(email_illegalChars)){
		mark(form['email']);
		submit = false;
	} else unmark(form['email']);
	
	if (!submit){
		return false;
	}
		
//	$("#submit_btn").addClass("disabled").attr("disabled","disabled");
	jform.fadeTo(300, 0.5);
	$("#form_helper").text("Sending...");
	
	jQuery('*[name]', jform).each(function() {
		var t = jQuery(this);
		var val = (t.attr('type') == 'checkbox') ? (t.attr('checked') == true) ? 1 : 0 : t.val();
		data[t.attr('name')] = val;
	});
	
	jQuery[method](action, data, function(data) {
		if(data){
			jform.fadeTo(300, 0);
			var title_text = $('#contactStep2').find('h2');
			title_text.text( title_text.text() + form.name.value + '!');
			$('#contactStep2').fadeIn(300);
			//$("#form_helper").css("color","green").text("Succsessfuly sent");
		}
		else{
			jform.fadeTo(300, 1);
			$("#form_helper").css("color","#D37171").text("Something went wrong, please try again");
		}
	});
	return false;
}


/* scrollTo jQuery extention */
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('3.R=6(e){7 l=0;7 t=0;7 w=3.a(3.X(e,\'1e\'));7 h=3.a(3.X(e,\'1f\'));7 m=e.L;7 B=e.F;1a(e.S){l+=e.T+(e.8?3.a(e.8.W):0);t+=e.V+(e.8?3.a(e.8.10):0);e=e.S}l+=e.T+(e.8?3.a(e.8.W):0);t+=e.V+(e.8?3.a(e.8.10):0);c{x:l,y:t,w:w,h:h,m:m,B:B}};3.1d=6(e){b(e){w=e.k;h=e.C}f{w=(d.Y)?d.Y:(1.4&&1.4.k)?1.4.k:1.9.L;h=(d.H)?d.H:(1.4&&1.4.C)?1.4.C:1.9.F}c{w:w,h:h}};3.U=6(e){b(e){t=e.i;l=e.A;w=e.r;h=e.D}f{b(1.4&&1.4.i){t=1.4.i;l=1.4.A;w=1.4.r;h=1.4.D}f b(1.9){t=1.9.i;l=1.9.A;w=1.9.r;h=1.9.D}}c{t:t,l:l,w:w,h:h}};3.a=6(v){v=12(v);c 14(v)?0:v};3.16.E=6(s){o=3.17(s);c u.18(6(){n 3.P.E(u,o)})};3.P.E=6(e,o){7 z=u;z.o=o;z.e=e;z.p=3.R(e);z.s=3.U();z.J=6(){1b(z.j);z.j=1c};z.t=(n N).Z();z.M=6(){7 t=(n N).Z();7 p=(t-z.t)/z.o.I;b(t>=z.o.I+z.t){z.J();11(6(){z.q(z.p.y,z.p.x)},13)}f{G=((-g.O(p*g.Q)/2)+0.5)*(z.p.y-z.s.t)+z.s.t;K=((-g.O(p*g.Q)/2)+0.5)*(z.p.x-z.s.l)+z.s.l;z.q(G,K)}};z.q=6(t,l){d.19(l,t)};z.j=15(6(){z.M()},13)};',62,78,'|document||jQuery|documentElement||function|var|currentStyle|body|intval|if|return|window||else|Math||scrollTop|timer|clientWidth||wb|new|||scroll|scrollWidth|||this||||||scrollLeft|hb|clientHeight|scrollHeight|ScrollTo|offsetHeight|st|innerHeight|duration|clear|sl|offsetWidth|step|Date|cos|fx|PI|getPos|offsetParent|offsetLeft|getScroll|offsetTop|borderLeftWidth|css|innerWidth|getTime|borderTopWidth|setTimeout|parseInt||isNaN|setInterval|fn|speed|each|scrollTo|while|clearInterval|null|getClient|width|height'.split('|'),0,{}))