//detect IE
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) //test for MSIE x.x;
var ieversion=new Number(RegExp.$1); // capture x.x portion and store as a number
	
$(document).ready(function(){

	//text glow
	$('#sub-content .entry-content h3, #content h1, #content h2').dropShadow({left: 0, top: 0, blur: 4, color: "#ffa300", opacity: 1});
	
	//lightbox
	$('a.lightbox').lightBox();
	
	//preload images
	$.preloadImages("shop-now-hover.png");

	
	
	//jCarousel
	$("#slider-content").jcarousel({
	    scroll: 1,
	    auto: 5,
	    wrap: 'last',
	    animation: 500,
	    initCallback: mycarousel_initCallback,
	    // This tells jCarousel NOT to autobuild prev/next buttons
	    buttonNextHTML: null,
	    buttonPrevHTML: null,
	    itemVisibleInCallback: {
								onBeforeAnimation:  mycarousel_reloadCallback
							   }
	});
	
	/* RAFFLE PAGE 
    if($(".page-template-raffle-tpl-php").length > 0)
    {
   		//file upload
	    
		$('#file_upload').uploadify({
	    'uploader'  : '/wp-content/themes/wetab/uploadify/uploadify.swf',
	    'script'    : '/wp-content/themes/wetab/uploadify/uploadify.php',
	    'cancelImg' : '/wp-content/themes/wetab/uploadify/cancel.png',
	    'auto'      : false,
		'wmode'     : 'transparent',
		'removeCompleted' : false,
	    onComplete  : function(event, ID, fileObj, response, data){
	    	if(parseInt(response) != 1)
	    	{
	    		$('#file_upload').uploadifyClearQueue();
		    	$('#error-box').html(response);
		    	$('#error-box').show();
	    	}
	    	else
	    	{
	    		$('#raffle-subscribe').css('text-align', 'center');
	    		$('#raffle-subscribe').html('Vielen Dank die Teilnahme!');
	    	}
	    },
		'onSelectOnce' : function(event,data) {
      		$('#upload-submit').css('display', 'block');
    	}
		});
		
		$('#raffle-subscribe ul li input').focus(function()
		{
			$(this).css('border-color', '#ffcd00');
		});
		
		$('#raffle-subscribe ul li input').blur(function()
		{
			$(this).css('border-color', '#333333');
		})

	}
	*/
	
	/* NEW RAFFLE PAGE */
	$('#raffle').submit(function()
	{
		//reset error
		$('#raffle-subscribe label').css('color', '#999999');
		$('#raffle-subscribe label a').css('color', '#999999');
		
		$('#raffle-subscribe ul li input').css('border-color', '#333333');
		$('#raffle-subscribe ul li input').css('color', '#e5e5e5');
		$('#raffle-subscribe ul li select').css('border-color', '#333333');
		$('#raffle-subscribe ul li select').css('color', '#e5e5e5');
		
		$('#raffle-subscribe .messages textarea').css('color', '#e5e5e5');
		$('#raffle-subscribe .messages textarea').css('border-color', '#333333');
	
		var error = false;
	
		//check fields
		if (!$('#first_name').val()){
			$('label[for=first_name]').css('color', '#D3273B');
			$('#first_name').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!$('#last_name').val()){
			$('label[for=last_name]').css('color', '#D3273B');
			$('#last_name').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!validateEmail($('#email').val())){
				$('label[for=email]').css('color', '#D3273B');
				$('#email').css('border-color', '#D3273B');
				
				error = true;
		}
		if (!$('#obs_account').val()){
			$('label[for=obs_account]').css('color', '#D3273B');
			$('#obs_account').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!$('#obs_project').val()){
			$('label[for=obs_project]').css('color', '#D3273B');
			$('#obs_project').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!$('#obs_package').val()){
			$('label[for=obs_package]').css('color', '#D3273B');
			$('#obs_package').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!$('#app_name').val()){
			$('label[for=app_name]').css('color', '#D3273B');
			$('#app_name').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!$('#short_description').val()){
			$('label[for=short_description]').css('color', '#D3273B');
			$('#short_description').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!$('#description').val()){
			$('label[for=description]').css('color', '#D3273B');
			$('#description').css('border-color', '#D3273B');
	
			error = true;
		}
		if ($("select[name='category'] option:selected").val() == 0){
			$('label[for=category]').css('color', '#D3273B');
			$('#category').css('border-color', '#D3273B');
	
			error = true;
		}
		if (!$('#terms').is(":checked")){
			$('label[for=terms]').css('color', '#D3273B');
			$('label[for=terms] a').css('color', '#D3273B');
	
			error = true;
		}
		
		if(!error)
		{
		  $('.loading').show();
		  
		  $.ajax({
			type: "POST",
			url: '/wp-content/themes/wetab/uploadify/new-raffle.php',
			cache: false,
			data: $(this).serialize(),
			success: function(html){
			
				$('.loading').hide();
				
				if(parseInt(html) == 1)
				{
					$('#raffle-subscribe').html('Your contest submission was successful!');
				}
				else
				{
					$('#error-box').html(html);
					$('#error-box').show();
				}
			},
			error: function(){
			  //ajax_error();
			}
		  });
		 }
		 
		return false;
	});
	
});


/* FUNCTIONS */
function show_video_content(video)
{
	$('#main-hero #video-container').css('background', '#000');
	
	 $.ajax({
		type: "POST",
		url: '/wp-content/themes/wetab/ajax/video' + video + '.html',
		cache: false,
		success: function(html){
			$('#main-hero #video-container').css('z-index', '1');
			$('.relative').css('top', '-99999px');
			$('#main-hero #video-container').html(html);
			
			if(!ieversion)
			{
				var myPlayer = VideoJS.setup("video");
				myPlayer.play();
			}

		},
		error: function(){
		  ajax_error();
		}
	  });	
}

function hide_video_content()
{
	$('#main-hero #video-container').css('z-index', '-1');
	$('#main-hero #video-container').css('background', 'none');
	$('#main-hero #video-container').html('');
	$('.relative').css('top', '0');
}


function mycarousel_initCallback(carousel) {
    jQuery('#slider-controls a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
        carousel.startAuto(0);
        
        return false;
    });


    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}

function mycarousel_reloadCallback(carousel, item, idx, state){
	$('#slider-controls a').removeClass('selected');
	$('#slider-controls a[rel='+idx+']').addClass('selected');
}

function validate_upload_form(){
	
	//reset error
	$('#raffle-subscribe label').css('color', '#999999');
	
	$('#raffle-subscribe ul li input').css('border-color', '#333333');
	$('#raffle-subscribe ul li input').css('color', '#e5e5e5');
	
	$('#raffle-subscribe .message textarea').css('color', '#e5e5e5');
	$('#raffle-subscribe .message textarea').css('border-color', '#333333');

	var error = false;

	//check fields
	if (!$('#first_name').val()){
		$('label[for=first_name]').css('color', '#D3273B');
		$('#first_name').css('border-color', '#D3273B');

		error = true;
	}
	if (!$('#last_name').val()){
		$('label[for=last_name]').css('color', '#D3273B');
		$('#last_name').css('border-color', '#D3273B');

		error = true;
	}
	if (!$('#description').val()){
		$('label[for=description]').css('color', '#D3273B');
		$('#description').css('border-color', '#D3273B');

		error = true;
	}
	if (!$('#terms').is(":checked")){
		$('label[for=terms]').css('color', '#D3273B');

		error = true;
	}
	if (!validateEmail($('#email').val())){
			$('label[for=email]').css('color', '#D3273B');
			$('#email').css('border-color', '#D3273B');
			
			error = true;
	}
	
	if(!error)
	{
		$('#file_upload').uploadifySettings('scriptData', {'first_name': $('#first_name').val(), 'last_name': $('#last_name').val(), 'email': $('#email').val(), 'description': $('#description').val()});
			
		$('#file_upload').uploadifyUpload();
	}
}

function validateEmail(email) {
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length)
		return false;

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email))
		return false;

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
		return false;

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
		return false;

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&+=_\.]*$/.test(local))
			return false;
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
		return false;	

	return true;
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", "/wp-content/themes/wetab/images/"+arguments[i]);
	}
}
