$(document).ready(function() {
	
	$.each($('.mailer'),function() {
		Email.mailto(this,'ejt');
	});
	
	$("a.zoom").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'titlePosition'  : 'inside'
	});
	
	// ENABLE NEW TAB/WINDOW LINKS TO EXTERNAL SITES
	$("a[rel*='external']").click(function() {
		window.open($(this).attr('href'), 'external', '');
		return false;
	});
	
	$('#golh-golh').click(function() {
			clearSearch(this);
	});
	
    $('.song').each(function(){

        audio_file = $(this).attr('id');

        $(this).flash({
                swf: '/assets/swf/player.swf',
                flashvars:
                {
                    file:'/assets/mp3/' + audio_file + '.mp3'
                },
                allowscriptaccess:'always',
                wmode:'opaque',
                height:24,
				width:520
        });
    });
	
	$('#order_form').submit(function() {
		resetOrderForm();		
		orderAmount = $.trim($('#order_amount').val());
		orderAmount = orderAmount.replace('$', '');
		orderAmountNoComma = orderAmount.replace(',', '');
		$('#order_amount').val(orderAmount);
		
		error = 0;
		if(isNaN(orderAmountNoComma)) {
			$('#order_amount_error').css('display','block');
			error = 1;
		}

		if($.trim($('#order_name').val()) == "") {
			$('#order_name_error').css('display','block');
			error = 1;
		}
		
		if($.trim($('#order_zip').val()) == "" || $('#order_zip').val().length < 5) {
			$('#order_zip_error').css('display','block');
			error = 1;
		}
		
		if($.trim($('#order_email').val()) == "" || validateEmail($('#order_email').val()) == false) {
			$('#order_email_error').css('display','block');
			error = 1;
		}
		
		if($.trim($('#order_cc').val()) == ""/* || validateCreditCard($('#order_cc').val()) == false*/) {
			$('#order_cc_error').css('display','block');
			error = 1;
		}
		
		if($.trim($('#order_cvv2').val()) == "" || $('#order_cvv2').val().length < 3 || $('#order_cvv2').val().length > 4) {
			$('#order_cvv2_error').css('display','block');
			error = 1;
		}
		
		if(error == 0) {
			return true;
		} else {
			return false;
		}
	});
	
	/* /E N A B L E  M A I L I N G  L I S T
	$('#kinetic').click(function() {
		window.scrollTo(0,0);
		$('#text_email').focus();
		clearSearch('#text_email');
	});
	/*
	
	/* E N A B L E  S L I D E S H O W
	$('#masthead_bg_images').cycle({
    	speed:4000
	});
	*/
});

function resetOrderForm() {
	$('#order_amount_error').hide();
	$('#order_name_error').hide();
	$('#order_zip_error').hide();
	$('#order_email_error').hide();
	$('#order_cc_error').hide();
	$('#order_cvv2_error').hide();
}

function validateEmail($email) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if( !emailReg.test( $email ) ) {
		return false;
	} else {
		return true;
	}
}

function validateCreditCard(CC) {                        
      if (CC.length > 19) {
           return (false);
	  }

      sum = 0; mul = 1; l = CC.length;
      for (i = 0; i < l; i++) {
           digit = CC.substring(l-i-1,l-i);
           tproduct = parseInt(digit ,10)*mul;
           if (tproduct >= 10)
                sum += (tproduct % 10) + 1;
           else
                sum += tproduct;
           if (mul == 1)
                mul++;
           else
                mul = mul - 1;
      }
      if ((sum % 10) == 0) {
           return (true);
      } else {
           return (false);
      }
 }

/*function downloadFile(fileName) {
	$.ajax({
		type: 'POST',
		url: proxyUrl,
		data: 'method=downloadProxy&fileName=' + fileName + '&ip=' + ip + '&referer=' + referer + '&url=' + url,
		success: function(r) {
			window.location = 'http://' + fileName;
		}
	});

}*/

function clearSearch(i) {
	if($(i).val() == 'Your email') {
		$(i).val('');
	}
}

var Media = {
	video: function(targ,url,w,h) {
		$(targ).flash({
			swf: url,
			width:w,
			height:h
		});
	},
	vimeo: function(targ,vid_id,w,h) {
		$(targ).flash({
			swf: 'http://vimeo.com/moogaloop.swf?clip_id=' + vid_id + '&amp;server=vimeo.com',
			width:w,
			height:h,
			allowfullscreen:'true',
			allowscriptaccess:'always'			
		});
	}
};

var Email = {
    mailto: function (link, name, domain) {
		if (!domain) {
            domain = "ejtmusic.com";
        }

        link.href = "mailto:" + name + "@" + domain;
        link.onMouseover = null;
    }
};
