jQuery(document).ready(function($) {
	$('.question h4').click(function () {
		$(this).parent().find('p').toggle();
		return false;
	});

	$('a[rel=close]').click(function(event){
		event.preventDefault();
		$('#layer').fadeOut('slow')
	});

	$('a[rel=show[contact]]').click(function(event){
		event.preventDefault();		
		$('#layer').fadeIn('slow')
		$('#layer .contact-sent').hide()
	});

	$('a[rel=show[booking]]').click(function(event){
		event.preventDefault();		
		$('#layer').fadeIn('slow')
		$('#layer .contact-sent').hide()			
	});
	
	var validation = $('#contact-form').validate({
		rules: {
			fname: 'required',
			lname: 'required',
			u_email: {
				required: true,
				email:true
			},
			u_message: 'required'
		},
		messages: {
			fname: 'Please enter firstname',
			lname: 'Please enter lastname',
			u_email: 'Please enter valid email address',
			u_message: 'Please enter some message for us!'
		}
	})

	// contact form handling
	$("#submit-contact").click(function(event){
		event.preventDefault();
		event.stopPropagation();

		if($("#submit-contact").hasClass('disabled')){
			return false;
		} else{
			$("#submit-contact").addClass('disabled')	
		}
			
		
				
		// validate				
		if(!$('#contact-form').validate().valid()){			
			$("#submit-contact").removeClass('disabled')											
			return false;			
		} 
		
		// get data

		user_fname = $('#fname').val()
		user_lname = $('#lname').val()
		user_email = $('#u_email').val()
		user_message = $('#u_message').val()
		
		$.post('/xhr.php', {
			fname: user_fname,
			lname: user_lname,
			email: user_email,
			message: user_message
		},function(data,status){
			if(data == 'success'){
				$('#layer .contact-sent').show()
				$("#submit-contact").removeClass('disabled')								
				$('#contact-form')[0].reset()
			} else{
				$("#submit-contact").removeClass('disabled')				
			}
			
		})
		
		
	});


	// videos switching
	
	$('#videos-list a.btn_watch').click(function(event){
		video = $(this).parent().find('div.player')
		title = $(this).parent().find('h1')
		descr = $(this).parent().find('p')
		$('#player div.player-container').html(video.html())
		$('#video .video_text h1').html(title.html())
		$('#video .video_text p').html(descr.html())		
		$("#fb-share-video, #myspace-share-video").attr({
			href: this.href
		})
		$('html,body').animate({scrollTop: $("#video").offset().top},'fast')
		
	});
	
	//$('#videos-list a.btn_watch').first().click()
	
	$('#lamp a').click(function(event){
		event.preventDefault();
		overlay = $('#video-overlay')
		if(overlay.length == 0){
			$('body').append('<div id="video-overlay" class="lights-on"></div>')
			$('#video-overlay').height($(document).height())
			$('#video-overlay').width($(document).width())					
		}
		
		overlay = $('#video-overlay')
		
		if(overlay.hasClass('lights-on')){
			overlay.fadeIn('slow');
			overlay.removeClass('lights-on')
			$('#player').addClass('lights-off')			
		} else{
			overlay.fadeOut('fast');
			overlay.addClass('lights-on')
			$('#player').removeClass('lights-off')						
		}
	
	});
	
	
	// video sharing for FB
	$('#fb-share-video').click(function(event){
		event.preventDefault()		
		if(this.href.indexOf('#video-') == -1){
			return;
		} else{
			url = 'http://www.facebook.com/sharer.php?u='+ encodeURIComponent(this.href) + '&t=' + encodeURIComponent("Paintsplat.com - " + $('#video .video_text h1').html())
			options = 'width=760,height=450,toolbar=no,menubar=no'		
			handle = window.open(url,'shareWindow',options)
		}
	});
	
	// video sharing for MYSPACE 
	$("#myspace-share-video").click(function(event){
		event.preventDefault()		
		if(this.href.indexOf('#video-') == -1){
			return;
		} else{
			title = "Paintsplat.com - " + $('#video .video_text h1').html()
			content = '';
			url = this.href;
			location = ''
			myspace_share(title,content,url,location)
		}
	})
	
	// post sharing for FB	
	$('#fb-post-share').click(function(event){
		event.preventDefault()		
		url = 'http://www.facebook.com/sharer.php?u='+ encodeURIComponent(window.location.href) + '&t=' + encodeURIComponent($('title').html())
		options = 'width=760,height=450,toolbar=no,menubar=no'		
		handle = window.open(url,'shareWindow',options)
	});	
	
	$("#myspace-post-share").click(function(event){
		event.preventDefault()		
		title = $('title').html()
		content = '';
		url = window.location.href
		location = ''
		myspace_share(title,content,url,location)
	})	
	
	// init thumbnails for videos from youtube
	
	$('div.ventry.youtube').each(function(){
		uri = $(this).find('.source').attr('href')
		thumb = getYoutubeThumbnail(uri,'small')
		$(this).find('img').attr({'src':thumb})
	})
	
	
	$('a.colorbox').colorbox()
	

});


function video_playback(){
	x = window.location.href;
	if(x.indexOf('#video-') != -1){
		id = x.substring(x.indexOf("#video-"))
		$(id + " a.btn_watch").click()
	}
}

function myspace_share(T, C, U, L)
{
    var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(T)
    + '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L;
    window.open(targetUrl);
}


function getYoutubeThumbnail( url, size ){
  if(url === null){ return ""; }
  size = (size === null) ? "big" : size;
  var vid;
  var results;

  results = url.match("[\\?&]v=([^&#]*)");

  vid = ( results === null ) ? url : results[1];
  if(size == "small"){
    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
  }else {
    return "http://img.youtube.com/vi/"+vid+"/0.jpg";
  }
}