/* Copyright (C) 2007 - 2011 YOOtheme GmbH, YOOtheme Proprietary Use License (http://www.yootheme.com/license) */

jQuery(function($){

	/* Accordion menu */
	//$('.menu-accordion').accordionMenu({ mode:'slide' });
	
	/* Follower */
	//$('div.mod-line ul.menu').follower({effect: {transition: 'linear', duration: 200}})

	/* Smoothscroller */
	$('a[href="#page"]').smoothScroller({ duration: 500 });

	/* Spotlight */
	$('.spotlight').spotlight({fade: 300});

	/* Match height of div tags */
	var matchHeight = function() {
		$('#top > .horizontal div.deepest').matchHeight(20);
		$('#bottom > .horizontal div.deepest').matchHeight(20);
		$('#maintop > .horizontal div.deepest').matchHeight(20);
		$('#mainbottom > .horizontal div.deepest').matchHeight(20);
		$('#contenttop > .horizontal div.deepest').matchHeight(20);
		$('#contentbottom > .horizontal div.deepest').matchHeight(20);
		$('#middle, #left, #right').matchHeight(20);
		$('#mainmiddle, #contentleft, #contentright').matchHeight(20);
	};

	/* Animate background position */
	var animateBackgroungPos = function(selector, options) {
		
		if((navigator.userAgent.match(/(iPhone|iPod|iPad)/i))) {
			return;
		}
		
		var elements = $(selector);

		var options = $.extend({
			transition: 'linear',
			repeat: 5,
			duration: 5000,
			direction: 1,
			width: 558
		}, options);
		
		var timer = false;
		
		function animate() {
			
			if(options.repeat==-1) {
				clearInterval(timer);
				return;
			}
			
			$(elements).each(function(i){
				
				if ($.browser.msie) {
					$(this).stop().css({'background-position-x': "0px", 'background-position-y': "40px"}).animate({ 
						'background-position-x': (options.direction * options.width) + "px",
						'background-position-y': "40px" 
					}, options.duration, options.transition );
				} else {
				
					$(this).stop().css('background-position', "0px 40px").animate({ 
						'background-position': (options.direction * options.width) + "px 40px" 
					}, options.duration, options.transition );
				
				}
			});
			
			options.repeat--;
			
			if(options.repeat==0) {
				options.transition = 'easeOutSine';
				options.duration = 2 * options.duration;
			}	
		}
		
		if (options.repeat) {
			timer = window.setInterval(animate, options.duration);
			animate();
		}	
	}
	
	var bgwidth = 558;
	var bgduration = 5000;
	
	switch(Warp.Settings.background) {
		
		case 'disco':
			repeat = 2;
			bgduration = 12500;
			bgwidth = 759;
			break;
			
		case 'jellyfish':
			repeat = 2;
			bgduration = 12500;
			bgwidth = 1028;
			break;
			
		case 'nebula':
			repeat = 2;
			bgduration = 20000;
			bgwidth = 556;
			break;
			
		case 'spotlights':
			repeat = 2;
			bgduration = 12500;
			bgwidth = 556;
			break;
		
	}
	
	$('#menu').css("visibility", "hidden");
	
	$(window).bind("load", function(){
		
		matchHeight();
		
		/* Dropdown menu */
		$('#menu').css("visibility", "visible");	
		
		if (Warp.Settings.bganimation == 1) {
			animateBackgroungPos('div#page-body', {direction: -1, width: bgwidth, duration: bgduration});
		}		
	})


try {
    var jqueryLoaded=jQuery;
    jqueryLoaded=true;
} catch(err) {
    var jqueryLoaded=false;
}


var fnames = new Array();var ftypes = new Array();//fnames[0]='EMAIL';//ftypes[0]='email';var err_style = '';
try{
    err_style = mc_custom_error_style;
} catch(e){
    err_style = '';
}
var head= document.getElementsByTagName('head')[0];
var style= document.createElement('style');
style.type= 'text/css';
if (style.styleSheet) {
  style.styleSheet.cssText = '.mce_inline_error {' + err_style + '}';
} else {
  style.appendChild(document.createTextNode('.mce_inline_error {' + err_style + '}'));
}
head.appendChild(style);
$(document).ready( function($) {

jQuery.validator.addMethod('required_group', function(val, el) {
        return $module.find('.required_group:filled').length;
});
    jQuery.validator.addMethod('required_group', function(value, element, checked) {
            var $module = $(element).parents('.input-group');
            return $module.find('input:checked').length;
            });


jQuery.validator.messages.required_group = 'Please choose at least one section to subscribe to.';
  var options = { invalidHandler: function(form, validator) {
	                    var errors = validator.numberOfInvalids();
	                    if (errors) {
	                        alert(validator.errorList[0].message);
	                        validator.errorList[0].element.focus();
	                    }
	                },
errorClass: 'mce_inline_error', onkeyup: function(){}, onfocusout:function(){}, onblur:function(){}  };
  var mce_validator = $("#mc-embedded-subscribe-form").validate(options);
  options = { url: 'http://twistedindustries.us1.list-manage.com/subscribe/post-json?u=1270fa2c87b854a51f89d2f96&id=8be14abeb0&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').show();
        //$('#mce-'+resp.result+'-response').html(resp.msg);
        alert(resp.msg); 
        $('#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); 
                alert(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);
			alert(msg);   
        }
    }
}

	$("#point-de-la-mode-footer a").fancybox({
		'autoDimensions': false,
		'hideOnContentClick': true,
		'width': 582,
		'centerOnScroll': true
	});

	$(".items .content a:has(img), .single .item .content a:has(img)").fancybox({
		'hideOnContentClick': true,
		centerOnScroll: true
	});

	$("a[href^='http:']:not([href*='" + window.location.host + "'])").each(function() {               
        $(this).attr("target", "_blank");
    });

	var EmailPrevious = 'Enter your email here';	
	// clear input on focus
	$('#mce-EMAIL').focus(function()
	{
		if($('#mce-EMAIL').val()!='')
		{
			EmailPrevious = $('#mce-EMAIL').val();
			$('#mce-EMAIL').val('');
		}
	});
	// if field is empty afterward, add text again
	$('#mce-EMAIL').blur(function()
	{
		if($(this).val()=='')
		{
			$('#mce-EMAIL').val(EmailPrevious);
		}
	});

	
});




Cufon.replace('#menu li', { hover: true });
Cufon.replace('.cufon-now, .page h1.title, #search .results-header, .search-results h1.title.with-results, h3.comments-meta, #respond h2');
