$(document).ready(function(){
	
	// // $('#contactForm').ajaxForm();
	// jQuery.validator.setDefaults({ 
	//     debug: true 
	// });
	// jQuery.validator.messages.required = "x";
	// 
	// 
	// 
	// 
	// $("#contactForm").validate({
	// 	submitHandler: function(form) {
	// 		// alert('sadsa');
	// 
	// 	   },
	// 	
	// rules: {
	//     email: {
	//       required: true
	//     }
	//   },
	// messages: {
	// 	email: {
	// 		required: "x"
	// 	},
	// 	name: {
	// 		required: "x"
	// 	},
	// 	message: {
	// 		required: "x"
	// 	}
	// }
	// });
	// 

// alert( window.location.href );
	function getParameterByName( name )
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
	    return "";
	  else
	    return decodeURIComponent(results[1].replace(/\+/g, " "));
	}

// alert(getParameterByName('contact'));
// JSON  - EVENTS http://pipes.yahoo.com/pipes/pipe.run?_id=1d0906d90e7afa42a5ffafce523c76e7&_render=json


// Navigation Content	
$('#primaryNav #home').bind('click', function(){
	$('.mainContainer').css('display', 'none');
	$('div.mainContainer').html('');
		showVideo();
	// Find size of mainContent DIV block (checks for <600 browser width)
		buttonHeight();
});

$('#primaryNav #about').bind('click', function(){
	$.get("templates/about.html", function(data){
		// Replace HTML in main content area
			$('div.mainContainer').html(data);
		// Replace Video Content with image
			$('#mediaWindow .video').css('display', 'none');
			$('#mediaWindow').html('<img src="images/gallery-thumbs/WOTM-gallery-img1.jpg" width="392" height="269"/>');
			$('.mainContainer').css('display', 'block');	
			// Find size of mainContent DIV block (checks for <600 browser width)
				buttonHeight();		
	});
});

$('#primaryNav #credits').bind('click', function(){
	$.get("templates/credits.html", function(data){
		$('div.mainContainer').html(data);
		$('#mediaWindow .video').css('display', 'none');
		$('#mediaWindow').html('<img src="images/gallery-thumbs/WOTM-gallery-img10.jpg" width="392" height="269"/>');
		$('.mainContainer').css('display', 'block');
		// Find size of mainContent DIV block (checks for <600 browser width)
			buttonHeight();	
	});
});


$('#primaryNav #gallery').bind('click', function(){
	var bw = $('html').outerWidth();
	if(bw > 600) {
		$('#mediaWindow').css('z-index', '30');
		$('#mediaWindow').html('<img id="galleryImage" src="images/gallery/WOTM-gallery-img7.jpg" width="392" height="269"/>');
		expandGalleryPics();
		}
		
		galleryPics();
	
});

$('#primaryNav #screenings').bind('click', function(){
	// var dataURL = 'data/ypipes-screenings.json';
	var randomnumber=Math.floor(Math.random()*11);
	var dataURL = "screenings.json?"+randomnumber;

	$.getJSON(dataURL, function(data){
		var htmlFragment = '';
		for (var i = 0, len = data.count; i<len; i++){
			var title = data.value.items[i].title;
			var starts = data.value.items[i]["loop:starttime"];
			var ends = data.value.items[i]["loop:endtime"];
			var location = data.value.items[i].location;
			var description = data.value.items[i].description;
			var link = data.value.items[i].link;
			var date = data.value.items[i]["loop:dateformat"];
			htmlFragment += '<div id="screening"><h3><a href="'+link+'">'+title+'</a></h3><p class="date" style="">'+date+'</p><p class="time">'+starts+' - '+ends+'</p><p class="location" style="">'+location+'</p></div>';
		}
		
		
		$('div.mainContainer').html(htmlFragment);
		$('#mediaWindow .video').css('display', 'none');
		$('#mediaWindow').css('overflow', 'visible');
		
		$('#mediaWindow').html('<img src="images/gallery/WOTM-gallery-img4.jpg" width="392" height="269"/>');
		$('.mainContainer').css('display', 'block');
		$('.mainContainer').css('overflow', 'auto');
		buttonHeight();
	});
});


$('#primaryNav #contactLink').bind('click', function(){
	$.get("templates/contact.html", function(data){
		// Find size of mainContent DIV block (checks for <600 browser width)
		
		$('#mediaWindow').html('<img src="images/gallery/WOTM-gallery-img2.jpg" width="392" height="269"/>');
			buttonHeight();
	});
});


// FUNCTIONS
function buttonHeight() {
	browserWidth = $('html').outerWidth();
	if(browserWidth > 600) return;
	var topHeight = 0, divHeight = $('div.mainContainer').outerHeight();
	if (divHeight === null) divHeight = 0;
	topHeight = divHeight + 650;
	footerHeight = divHeight + 300
	// alert(footerHeight);
	$('#primaryNav').css('top',topHeight+'px');
	// $('#footer').css('height',footerHeight+'px');
}

function showVideo() {
	var devicewidth = screen.width;
	var videoTag = devicewidth < 480? '<iframe title="YouTube video player" width="320" height="212" src="http://www.youtube.com/embed/q2cjnXIDcjM" frameborder="0" allowfullscreen></iframe>' : '<iframe src="http://player.vimeo.com/video/20522692" width="392" height="260" frameborder="0"></iframe>';
$('#mediaWindow').html('<div class="video">'+videoTag+'</div>');
return;
}

function expandGalleryPics() {
	$('img#galleryImage').bind('click', function(){
		$('#galleryImage').addClass("expandPicture");
		$('.gallery').css("z-index","0");
		$('img.expandPicture').bind('click', contractGalleryPic);
	});
	
	
}

function contractGalleryPic(){
	// $('#galleryImage').addClass("contractPicture");
	$('#galleryImage').addClass("contractPicture").removeClass("expandPicture");
	var src = $(this).attr('src');
	setTimeout( function(){resetGalleryPic(src)}, 1000 );
}

function resetGalleryPic(url){
	$('#mediaWindow').html('<img id=\"galleryImage\" src=\"'+url+'\" width=\"392\" height=\"269\"/>');
	expandGalleryPics();
}

// Gallery Code
function galleryPics() {
	// Load data template for this page
		$.get("templates/gallery.html", function(data){
			$('div.mainContainer').html(data);
			
			$('.mainContainer').css('display', 'block');
			$('.mainContainer').css('overflow', 'hidden');
	// Detect click on thumbnail image
			$('.gallery li').bind('click', function(){
		// Event handler to reduce image size
				$('.mainContainer').css('z-index', '0');
		// Show image in larger area if larger than 600px
		 $('#mediaWindow').html('<img id="galleryImage" src="images/gallery/WOTM-gallery-'+$(this).attr('class')+'.jpg" width="392" height="269"/>');
		// Click handler ofr image in bottom right - expands the image to large size
			
				expandGalleryPics();
			
			});
			// Find size of mainContent DIV block (checks for <600 browser width)
			
			buttonHeight();
		});	
};

function browserWidth(){
	var bw = $('html').outerWidth();
	return bw;
}
showVideo();


  });

