if(document.getElementById){
	var html = document.getElementsByTagName("html")[0];
	html.className = 'js';
}

var woo_settings = {idle_timeout:'300000', domain:'favola.dk'};

// DOM ready
$(document).ready(function(){
	smoothEffects();
	labelToInput();
	overlay();
	ieFix();
	twitter('favola_dk');
	mailchimp();
	$.geekGaTrackPage('UA-15485432-1', '.favola.dk');
	woo();
});

function smoothEffects(){
	// header links
	$('header p a').hover(function(){
		$(this).stop().animate({height:"17px"},100)
	},function(){
		$(this).stop().animate({height:"14px"},100)
	})
	// boxes
	setTimeout('$(\'article.intro\').hide().slowEach(300,function(){$(this).show()})',400);
	setTimeout('$(\'article.intro img\').hide().slowEach(300,function(){$(this).fadeIn(1500)})',300);
	$('#content a:has(img)').hover(function(){
		$(this).find('img').stop().animate({opacity:.5,top:'-5px'},300)
	},function(){
		$(this).find('img').stop().animate({opacity:1,top:0},300)
	})
	$('#focus a').hover(function(){
		$(this).find('img').stop().animate({opacity:.5},300)
	},function(){
		$(this).find('img').stop().animate({opacity:1},300)
	})
	//twitter
	var twitterTitle = $('#twitterlink').attr('title');
	$('#twitterlink').removeAttr('title').after('<p id="twittertitle">'+twitterTitle+'</p>').hover(function(){
		clearTimeout(this.sfTimer);
		$('#twittertitle').stop().animate({width:'145px'}, 300);
	},function(){
		this.sfTimer=setTimeout(function(){$('#twittertitle').stop().animate({width:'0'}, 200)},600);
	})
	$('#tweet ul').fadeIn(1000);
}

function labelToInput(){
	$("label.labelToInput[for]").each(function(i){
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		var newVal = this.innerHTML.replace(regexp,"");
		if($("#"+this.htmlFor).is("input") || $("#"+this.htmlFor).is("textarea")){
			if(  ($("#"+this.htmlFor).val() == "") || ($("#"+this.htmlFor).val() == newVal)  ){
				$("#"+this.htmlFor).attr("value",newVal);
				$("#"+this.htmlFor).focus(function(){if(this.value == newVal) this.value = "";});
				$("#"+this.htmlFor).blur(function(){if(this.value == "") this.value = newVal;});
			}
		} else if($("#"+this.htmlFor).is("select")){
			var orgOptions = $("#"+this.htmlFor).html();
			$("#"+this.htmlFor).html("")
			strSelected = ' selected="selected"';
			if(orgOptions.indexOf("selected=") > -1) strSelected = "";
			newOptions = "<option"+strSelected+">"+newVal+"</option>"+orgOptions;
			$("#"+this.htmlFor).html(newOptions)
		}
		$(this).hide();
	})
	cleanForms();
}

function cleanForms(){
	$("form:has(label.mdValueToInput)").submit(function(){
		$("label.mdValueToInput[for]").each(function(){
			var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
			var newVal = this.innerHTML.replace(regexp,"");
			if($("#"+this.htmlFor).attr("value") == newVal){
				$("#"+this.htmlFor).attr("value","");
			}
		})
	})
}

function overlay(){
	$('.overlay').fancybox({
		'titleShow'		: false,
		'scrolling'		: 'no',
		'overlayOpacity': 0.75,
		'overlayColor'	: '#25231b',
		'centerOnScroll': true,
		'padding'		: 15
	})
	$('.overlay-map').fancybox({
		'titleShow'		: false,
		'scrolling'		: 'no',
		'overlayOpacity': 0.75,
		'overlayColor'	: '#25231b',
		'centerOnScroll': true,
		'padding'		: 15,
		'type'			: 'iframe',
		'width'			: 1000,
		'height'		: 700
	})
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
    	return '<div id="fb-title">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + (currentIndex + 1) + ' / ' + currentArray.length + '</div>';
	}
	$('article.reference a:has(img)').attr('rel','gallery').fancybox({
		'scrolling'		: 'no',
		'overlayOpacity': 0.75,
		'overlayColor'	: '#25231b',
		'centerOnScroll': true,
		'padding'		: 15,
		'titlePosition'	: 'inside',
		'titleFormat'	: formatTitle,
		'width'			: 930,
		'height'		: 480
	})
}

function ieFix(){
	if($.browser.msie && $.browser.version <= 8){
		$('header p a').css('position','relative').append('<div class="lc"></div><div class="rc"></div>');
		$('header nav li').css('position','relative').find('a').append('<div class="lc"></div><div class="rc"></div>');
		var btnwidth = $('#mc_embed_signup .btn').width() + 24;
		$('#mc_embed_signup .btn').parent().addClass('btnWrap').append('<div class="lc" style="right:'+btnwidth+'px"></div><div class="rc"></div>');
		$('#facebook').attr('frameborder','0').attr('allowTransparency','true').attr('scrolling','no');
	}
}

function twitter(account){
	getTwitters('tweet', {
		id: account,
		count: 3,
		enableLinks: true,
		ignoreReplies: true,
		onTimeoutCancel: true,
		template: '<small>%time%</small><p>%text%</p>',
		callback: function(){$('#twitter ul').fadeIn()},
		onTimeout: function(){$(this).html('<p>Twitter overload...</p>')}
	});
}

// mail chimp
var fnames = new Array();var ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='MMERGE1';ftypes[1]='text';var err_style = '';
try{
	err_style = mc_custom_error_style;
} catch(e){
	err_style = 'margin: 1em 0 0 0; padding: 1em 0.5em 0.5em 0.5em; background: rgb(255, 238, 238) none repeat scroll 0% 0%; font-weight: bold; float: left; z-index: 1; width: 80%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; color: rgb(255, 0, 0);';
}
function mailchimp(){
	var options = { errorClass: 'mce_inline_error', errorElement: 'div', errorStyle: err_style, onkeyup: function(){}, onfocusout:function(){}, onblur:function(){}  };
	var mce_validator = $("#mc-embedded-subscribe-form").validate(options);
	options = { url: 'http://favola.us1.list-manage.com/subscribe/post-json?u=5bb7ef9e428bef6e8b5283185&id=ff046f59c2&c=?', type: 'GET', dataType: 'json', contentType: "application/json; charset=utf-8",
		beforeSubmit: function(){
			$('#mce_tmp_error_msg').remove();
			$('.datefield','#mc_embed_signup').each(function(){
					var txt = 'filled';
					var fields = new Array();
					var i = 0;
					$(':text', this).each(function(){
						fields[i] = this;
						i++;
					});
					$(':hidden', this).each(function(){
						if ( fields[0].value=='MM' && fields[1].value=='DD' && fields[2].value=='YYYY' ){
							this.value = '';
						} else if ( fields[0].value=='' && fields[1].value=='' && fields[2].value=='' ){
							this.value = '';
						} else {
							 this.value = fields[0].value+'/'+fields[1].value+'/'+fields[2].value;
						}
					});
				});
			return mce_validator.form();
		}, 
		success: mce_success_cb
	};
	$('#mc-embedded-subscribe-form').ajaxForm(options);

}

function mce_success_cb(resp){
	$('#mce-success-response').hide();
	$('#mce-error-response').hide();
	if (resp.result=="success"){
		$('#mce-'+resp.result+'-response').fancybox({
			'titleShow'		: false,
			'scrolling'		: 'no',
			'overlayOpacity': 0.75,
			'overlayColor'	: '#25231b',
			'centerOnScroll': true,
			'padding'		: 15,
			'width'			: 300,
			'height'		: 200,
			'content'		: '<p class="mce-response" id="mce-'+resp.result+'-response">'+resp.msg+'</p>'
		}).trigger('click');
		$('#mc-embedded-subscribe-form').each(function(){
			this.reset();
		});
	} else {
		var index = -1;
		var msg;
		try {
			var parts = resp.msg.split(' - ',2);
			if (parts[1]==undefined){
				msg = resp.msg;
			} else {
				i = parseInt(parts[0]);
				if (i.toString() == parts[0]){
					index = parts[0];
					msg = parts[1];
				} else {
					index = -1;
					msg = resp.msg;
				}
			}
		} catch(e){
			index = -1;
			msg = resp.msg;
		}
		try{
			if (index== -1){
				$('#mce-'+resp.result+'-response').show();
				$('#mce-'+resp.result+'-response').html(msg);			 
			} else {
				err_id = 'mce_tmp_error_msg';
				html = '<div id="'+err_id+'" style="'+err_style+'"> '+msg+'</div>';
				
				var input_id = '#mc_embed_signup';
				var f = $(input_id);
				if (ftypes[index]=='address'){
					input_id = '#mce-'+fnames[index]+'-addr1';
					f = $(input_id).parent().parent().get(0);
				} else if (ftypes[index]=='date'){
					input_id = '#mce-'+fnames[index]+'-month';
					f = $(input_id).parent().parent().get(0);
				} else {
					input_id = '#mce-'+fnames[index];
					f = $().parent(input_id).get(0);
				}
				if (f){
					$(f).append(html);
					$(input_id).focus();
				} else {
					$('#mce-'+resp.result+'-response').show();
					$('#mce-'+resp.result+'-response').html(msg);
				}
			}
		} catch(e){
			$('#mce-'+resp.result+'-response').show();
			$('#mce-'+resp.result+'-response').html(msg);
		}
	}
}

function woo(){
	
	var wsc = document.createElement('script');
	wsc.src = document.location.protocol+'//static.woopra.com/js/woopra.js';
	wsc.type = 'text/javascript';
	wsc.async = true;
	var ssc = document.getElementsByTagName('script')[0];
	ssc.parentNode.insertBefore(wsc, ssc);
}

