//facebook
function callPublish(msg, attachment, action_link) {
  FB.ensureInit(function () {
    FB.Connect.streamPublish(msg, attachment, action_link);
  });
}

$(function(){

	
	$('.top-slider-inner h2 a').hover(function(event) {
        $(this).parents('.top-slider-wrapper').toggleClass('on');
    });

	function tsw_height_adjust(delay, speed, height) {

		var tsw = $('.top-slider-wrapper');
		var ts = $('.top-slider');
		var tsi = $('.top-slider-inner');

		if(typeof height == 'undefined') {
			var height = tsi.height();
		}
		tsw.delay(delay).animate({
			'height' : height
		}, speed);

	};
	var tsw = $('.top-slider-wrapper');
	var ts = $('.top-slider');
	var tsi = $('.top-slider-inner');
	var wh = ($(window).height() - $('#head').height());

	tsw_height_adjust(2000, 1000);

	$('.quiz-yes').click(function(event){
		event.preventDefault();
		$.ajax({

			beforeSend : function(XMLHttpRequest) {
				$('h2.cta',tsi).hide();
				tsi.append('<h2 class="loading"><div class="grid_10 prefix_2 omega">Loading...</div></h2>');
			},
			url : '/quiz/4',
			success : function(data, textStatus, XMLHttpRequest) {
				$('.loading', tsi).replaceWith('<h2 class="lab-cta"><div class="grid_10 prefix_2 omega">quiz 1.0 <span>from</span> <a href="http://labohratory.com" title="fangohr labohratory">fangohr labohratory &#8594;</a></div></h2>');

				//$('#content, #foot').animate({'height' : 0}, 500);
				$('#content, #foot, .footer-nav').remove();
				tsi.append($('form.questions', data));

				jQuery('#quiz-slider').codaSlider({
					'autoHeight' : false,
					'autoHeightEaseDuration' : 500,
					'crossLinking' : false,
					'dynamicArrowLeftText' : '&#8592; Change your last answer',
					'dynamicArrowRightText' : 'Next',
					'dynamicTabs' : false,
					'dynamicTabsAlign' : 'left',
					'panelTitleSelector' : 'h3',
					'slideEaseDuration': 500
				});
				tsw.animate({
					'backgroundColor' : '#EEEEEE'
				}, 500);
				
				if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { 
				    tsw_height_adjust(0, 1000, 1100);
				}else{
				    tsw_height_adjust(0, 1000, 680);
				}

//				tsw_height_adjust(0, 500, wh);
				
//				tsw_height_adjust(0, 1000, 'auto');

				$('form.questions').ajaxForm({
					success: function(data) {
						var results = $('.results', data);
						
						$('.result-text h3', results).hide();

						$('form.questions').replaceWith(results);
						$('.quiz-again').click(function(event){
							event.preventDefault();
							$('.results, .lab-cta').remove();
							$('h2', tsi).show();
							$('.quiz-yes').click();
						});
						
						var rms = $('.result-meter-score');
						var scores = rms.attr('rel').split(':');
												
						
						rms.delay(1000).animate({ width : '100%' }, {
							duration : 2000,
							step : function() {
								var percent = (($(this).width() / $(this).parent().width()));
								var score = scores[0];
								var total = scores[1];
								var amt = (score * percent);
								
								$('.result-meter-score-text', this).text(amt.toFixed(0) + '/' + total );
							},
							complete: function() {
								var score = scores[0];
								var total = scores[1];
								$('.result-meter-score-text', this).text(score + '/' + total );
							}						
						});
						$('.result-text h3').delay(2000).fadeIn(1000);

						$('.result-social').keyup(function(event){
							var t = $(this);
							var r = t.val();
							$('.tweet-this a').attr({'href' : 'http://twitter.com/home?status=' + encodeURIComponent(r)});

							if(r.length > 140) {
								t.addClass('result-social-toomany');
							} else {
								t.removeClass('result-social-toomany');
							};
						});
						
						if($.browser.msie) {
							
							$('.result-list li').each(function() {
								var el = $(this);
								var p = el.css('width').split('%');
								el.css({ 'width' : (p[0] -1) + '%', 'margin-right':'2px' });
							});
						}
					}
				});

				$('.questions input:radio').attr({'checked': false});

			}
		});
	});
	
	$.address.change(function(event){
		if(event.value == 'quiz') {
			$('.quiz-yes').click();
		}
	});
});

