/*
 * author: Mindaugas Bujanauskas
 * website: http://www.roadkillrevived.com/
 * e-mail: hello@roadkillrevived.com
 * filename: core.js
 * description: The core of the whole js engine for Denee Motion
 *
 */

// some basic variables
// v_videos holds a jq function to parse the vimeo json data
// and v_response contains received json data
var v_videos, i, v_response;

// start a jq script
$(function(){

	// a simple funciton to show preloader
	function preload (type) {
		// the type is where to show - in the list or in the preview
		type = type || 1;
		if ( (type == 1) || (type == 'list') ) {
			// this one tells to show it in the list
			$('.film .list')
				.html('<div class="preloader"><img src="images/preloader_01.gif" width="16" height="16" alt="Fetching recent videos.." /></div>');
		} else {
			// and this one show it in the preview
			$('.film .preview')
				.html('<div class="preloader"><img src="images/preloader_02.gif" width="16" height="16" alt="Fetching selected video" /></div>');
		}
	}
	preload();
	
	// function to display a video
	function show(vid) {
		// we want to get rid of any content of the preview window
		$('.film .preview').empty();
		// then show the preloader
		preload(2);
		// and display the video
		$(".film .preview").oembed('http://www.vimeo.com/'+vid, {maxWidth: 588, embedMethod: "fill"});
	}

	// username and url for vimeo, together with callback func.
	var v_usr = 'deneemotion';
	var v_url = 'http://vimeo.com/api/v2/' + v_usr + '/videos.json?callback=v_videos';
	// we embed the json object here
	$.getScript(v_url);
	
	// a callback function to parse the received data and output the results
	v_videos = function (data) {
		// this is for later development
		v_response = data;
		// we want to get the name of the file
		// it is in case the client wants to add a splash screen or for any other reason
		var sPath = window.location.pathname;
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		// reset the counter
		i = 0;
		// adding a container for video list
		$('.film .list').html('<ul class="vimeo"></ul>');
		// parsing json data
		$.each(data, function(){
			// we want to display only first four videos
			if (i < 3) {
				// checking the length of description
				// we don't want it to be too long
				if (this.description.length > 115) { this.description = this.description.substr(0, 114) + '...'; }
				// adding a list item to unordered list
				$('.film .vimeo').append('<li><a href="'+sPage+'#/video/'+this.id+'" ref="'+this.id+'" style="background-image: url('+this.thumbnail_small+');"><span class="title">'+this.title+'</span> <span class="desc">'+this.description+'</span></a></li>');
				// if this is the first video and the user hasnt followed any link to this page
				if ( (i < 1) && (window.location.hash == '') ) {
					// we add this video to the preview window
					show(this.id);
				}
			}
			// increase the counter :)
			i++;
		});
		
		//display the view more videos link
		$('.film a.more').css('display', 'block');
	};

	// action for a click on video list item
	$(".film .list a").live('click', function(e){
		// checking if the left mouse button was clicked
		// live event catches all clicks
		if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 0) ) {
			// setting the location hash
			window.location.hash = '/video/'+$(this).attr('ref');
			// showing the video
			show($(this).attr('ref'));
			// preventing from acting like a link
			return false;
		}
	});
	
	
	// behaviour for contact us now button
	$('.header .button a').click(function(){
		// checking for the state of button
		if (!$(this).hasClass('active')) {
			// changing the look of the button
			$(this).addClass('active');
			// showing the contacts pane
			contacts('show');
		} else {
			//changing the look of the button
			$(this).removeClass('active');
			// hiding the contacts pane
			contacts('hide');
		}
		
		// prevent acting like a normal link
		return false;
	});
	
	// behaviour for the close button in contact us now screen
	$('.contactContainer .contactos .close').click(function(){
		// hide the screen
		contacts('hide');
		// remove the class from the cool button
		$('.header .button a').removeClass('active');
		// prevent from acting as a normal link
		return false;
	});
	
	// function to control contact us now container
	function contacts (action) {
		// defaul action for this function is to show the container
		action = action || 'show';
		
		// what action to perform
		if (action == 'show') {
			// there is a compatibility issue in ie6
			// the padding causes the content to jump when it is being shown
			// so we remove the padding and then add it later on as a separate animation
			/* if ( ($.browser.msie) && ($.browser.version == '6.0') ) {
				$('.contactContainer .contactos')
					.css('paddingTop', 0)
					.slideDown(500)
					.animate({'paddingTop': '20px'});
			} else {
				// here we act normaly
				$('.contactContainer .contactos').slideDown(500);
			} */
			
			// better solution working on all major browsers
			$('.contactContainer .contactos')
					.css({'paddingTop': 0, 'height': 0})
					.animate({'height': '80px', 'paddingTop': '12px'}, 'nowmal', 'swing');
		} else {
			$('.contactContainer .contactos').slideUp(500);
		}
	}
	
	// initialize some basic stuff on page
	function init () {
		// for starters let's check status of the contact us stripe
		if ($('.contactContainer .contactos').css('display') == 'block') {
			// if it's not hidden we want to display that clearly on our button
			// this will also lead to correctly working actions when it's clicked
			$('.header .button a').addClass('active');
		}
		
		// making the packages description working
		// using a cycle plugin
		$('.bodi .packages .cycle') 
			.after('<div class="packNav">')
			.cycle({ 
				fx: 'scrollLeft',
				speed: 500,
				timeout: 8000,
				pager: '.packNav',
				random: 1,
				fit: 1,
				pause: 1
			});
			
		// hash navigation / friendly urls
		if (window.location.hash != '') {
			// getting the hash and spliting it into parts
			var fullHash = window.location.hash;
			var hash = fullHash.split('/');
			// checking what to show
			if (hash[1] == 'video') {
				show(hash[2]);
			}
		}
	}
	init();
	
});
