


	//////////////////////////////////
	/// GLOBAL VARS
	//////////////////////////////////

	var TEMPLATE_URL = '/wp-content/themes/friendlondon_tv';



	//////////////////////////////////
	/// CONVERT URL IN STRING
	//////////////////////////////////

	function convertURL(text){
		if (text) {
			text = text.replace(
				/((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
	            function(url){
					var full_url = url;
					if (!full_url.match('^https?:\/\/')) {
						full_url = 'http://' + full_url;
					}
					return '<a href="' + full_url + '" target="_blank" rel="nofollow">' + url + '</a>';
				});
			}
		return text;
	}

	function disablePlayers() {
		alert('222');
	}


	//////////////////////////////////
	/// CONVERT URL IN STRING
	//////////////////////////////////
	
	function togglePlayerState(video_id) {
		switch (jwplayer(video_id).getState()) {
			case 'PLAYING':
			case 'BUFFERING':
				jwplayer(video_id).pause(); 
			break;
			default:
				jwplayer(video_id).stop();
			break;
		}
	}
	
	function togglePlayerStateV2(video_id) {
		switch (jwplayer(video_id).getState()) {
			case 'PLAYING':
			case 'BUFFERING':
				jwplayer(video_id).stop(); 
			break;
			default:
				jwplayer(video_id).stop();
			break;
		}
	}

	//////////////////////////////////
	/// IPAD/IPHONE DETECT
	//////////////////////////////////
	
	function isiPad(){
		return (navigator.platform.indexOf("iPad") != -1);
	}

	function isiPhone(){
	    return (
	        //Detect iPhone
	        (navigator.platform.indexOf("iPhone") != -1) ||
	        //Detect iPod
	        (navigator.platform.indexOf("iPod") != -1)
	    );
	}

	function recalcRows(items,position) {
		return;
		var counter = 0;
		var rowCounter = 0;
		var columnPosition = (position%4);
		var rowPosition = Math.floor((position/4));
		jQuery(items).each(function (i) {
			if (i == 0 || (i%4)==0) {
				rowCounter++;
				jQuery(this).addClass('first-child');
			}
			if (jQuery(this).hasClass('two-columns')) {
				rowCounter++;
			}
			if ((i%((rowCounter*4)-1))==0 && i > 0) {
				jQuery(this).addClass('last-child');
			}
		});
	}


	//////////////////////////////////
	/// DOCUMENT READY
	//////////////////////////////////

	jQuery(document).ready(function () {

		/*if (jQuery('.featured-item').size() > 0) {
		
			jQuery('#featured').scrollable({
				items: '.featured-items',
				next: '#next',
				prev: '#prev',
				speed: 1000,
			//	circular: true, // do no use because it clones first and last div on the beginning and end, breaking the player
				onBeforeSeek: function () {
					var video_id = jQuery('.featured-item:eq('+this.getIndex()+')').find('object, a').attr('id');
					if (video_id) {
						if (video_id.length > 0) {
							togglePlayerState(video_id);
						}
					}
				}
			});
		}*/
		
		if (jQuery('.featured-item').size() > 0) {
		
			jQuery('.featured-items').cycle({
				
				fx: 'scrollHorz',
				next: '#next',
				prev: '#prev',
				speed: 700,
				timeout: 60000,
			
			});
			
			jQuery( '.featured-items .featured-item' ).each(function() {
			
			
			});	
			
		}


		if (jQuery('.featured-item').size() > 0) {


			// LAYOUT THE LATEST WORK
			
			/*$('#latest-work').isotope({
				itemSelector : '.latest-item',
	        layoutMode : 'masonry',
	        masonry: {
	            columnWidth: 251
	        },
	        masonryHorizontal: {
	            rowHeight: 291
	        },
				animationEngine : 'css',
				transformsEnabled: false,
				resizesContainer: false,
				sortBy: 'width',
				getSortData : {
					number : function ( $elem ) {
    					return parseInt( $elem.attr('current-sort') );
					}
				}
			});*/

			jQuery( '#latest-work' ).isotope({
			
				itemSelector : '.latest-item',
				layoutMode : 'masonry',
				masonry: {
					columnWidth: 251
				},
				masonryHorizontal: {
					rowHeight: 291
				},
				animationEngine : 'css',
				transformsEnabled: false,
				resizesContainer: true,
				sortBy: 'number',
				getSortData : {
					number : function ( $elem ) {
						return parseInt( $elem.attr('current-sort') );
					}
				}
			});
			

			// LATEST WORK HOVER
			
			jQuery('.latest-item .thumbnail').hover(
				function () { jQuery(this).find('.thumb').stop().animate({opacity: '0.0'},450); },
				function () { jQuery(this).find('.thumb').stop().animate({opacity: '1.0'},250); }
			);


			function _reLayout() {
			
				jQuery('#latest-work').isotope( 'reLayout' );
			
			}


			//CLOSE A LATEST WORK ITEM

			jQuery('.latest-item .close').click(function () {
				var parent = jQuery(this).parent().parent().parent();
				
				if ($(parent).hasClass('two-columns')){
		            jQuery(parent).removeClass('two-columns').addClass('one-column').css({height: '271px'});
    		    }
    		    
				_reLayout();
			});


			// HANDLE THE LATEST WORK

			jQuery('.latest-item .thumbnail img').click(function () {

				var parent = jQuery(this).parent().parent().parent();
				var parent_div = parent.parent();
				var item = parent;
				
				
				
				jQuery( '#latest-work > div' ).each(function() {
					
	
					
				});
				
		      	if ($(parent).hasClass('two-columns')){
		      		
		            jQuery(parent).removeClass('two-columns').addClass('one-column').css({height: '271px'});
		            
    		    } else {
				
					/*if (parent.attr('initial-sort') > jQuery('#latest-work').children('work-item').size()) {
						jQuery('#latest-work').css({height: '2017px'})
					} else {
						jQuery('#latest-work').css({height: '1727px'})
					}*/
				
					if (parent.attr('current-sort')%4==0) {
						parent.attr('current-sort',parseInt(parent.attr('current-sort'))-1);
						parent.prev().attr('current-sort',parseInt(parent.prev().attr('current-sort'))+1);
					}
					
    		    	$('#latest-work').isotope('reLayout');
				jQuery(parent_div).children().each(function (i) {
					//jQuery(this).attr('current-sort',i);
				});

    		    }

    		    	jQuery(parent).removeClass('one-column').addClass('two-columns').css({height: '562px'});
    		    	jQuery(parent).siblings().removeClass('two-columns').addClass('one-column').css({height: '271px'});
    		    	_reLayout();
				
				if( !isiPhone() && !isiPad() ) {
				
					jQuery.scrollTo(
						parent,
						800,
						{ 'offset' : {
							'left': 0,
							'top': -20
						}}
					);
					
				}

				jQuery(parent_div).children().each(function (i) {
					jQuery(this).attr('current-sort',jQuery(this).attr('initial-sort'));
				});


			});
			
			function showFirstLatestWork() {
				
				var thisone = jQuery( '.latest-item' ).first();
				
				jQuery( thisone ).removeClass( 'one-column').addClass( 'two-columns' );
				
				jQuery( 'latest-content' , thisone ).show();
				
				jQuery( thisone ).css({ height: '562px' });
				
				_reLayout();
				
			}
			
			showFirstLatestWork();

		}



	//////////////////////////////////
	/// CONTACT
	//////////////////////////////////
	
	if (jQuery("body").hasClass('page') && jQuery("#contact-map").size()>0) {

		var Icon = new GIcon();
		Icon.image = TEMPLATE_URL + "/images/friend-gmarker/image.png";
		Icon.iconSize = new GSize(24,54);
		Icon.shadow = TEMPLATE_URL + "/images/friend-gmarker/shadow.png";
		Icon.shadowSize = new GSize(54, 54);
		Icon.iconAnchor = new GPoint(0, 54);
		Icon.infoWindowAnchor = new GPoint(5, 2);

		var map = new GMap2(document.getElementById('contact-map')); 
		var friendHQ = new GLatLng(51.523186,-0.098813); 
		map.setCenter(friendHQ, 16);
		map.setUIToDefault();
		marker = new GMarker(friendHQ, Icon); 
		map.addOverlay(marker); 

	//end contact
	}

	//////////////////////////////////
	/// DIRECTORS
	//////////////////////////////////

	if (jQuery("body").hasClass('page') && jQuery("#directors-list").size()>0) {

		// GLOBAL
		
		function toggleDirectorsByGenre(genreclass) {
			jQuery('.director').each(function () {
				if (genreclass.length>0) {
					if (jQuery(this).hasClass(genreclass)) {
						jQuery(this).show();
					} else {
						jQuery(this).hide();
					}
				} else {
					jQuery(this).show();
				}
			});
			
			jQuery('.director').removeClass('first-child').removeClass('last-child');
			
			var itemCount = 0;
			
			
			
			jQuery('.director:visible').each(function (i) {
				
				var theHref = jQuery( '.thumbnail a' , this ).attr( 'href' );
				
				if( genreclass == 'music_video') {
				
					jQuery( '.thumbnail a' , this ).attr( 'href' , theHref + '#promo' );
				
				} else {
					
					if( theHref.indexOf( '#' ) != -1 )
						theHref = theHref.substr( 0 , theHref.indexOf( '#' ) );
					
					jQuery( '.thumbnail a' , this ).attr( 'href' , theHref );
				
				}
				
				itemCount++;
				if (itemCount==1 || (i%4)==0) {
					jQuery(this).addClass('first-child');
				} else if ((itemCount%4)==0) {
					jQuery(this).addClass('last-child');
				}
			});
			
		}
		
		jQuery('.director .thumbnail').hover(function () { 
		
			jQuery( this ).find( '.hover' ).show();
			jQuery(this).find('.thumb').stop().animate({opacity: '0.0'},450); 
			
		}, function () {
		
			jQuery(this).find('.thumb').stop().animate({opacity: '1.0'},250); 
			
		});
		// GENRES
		
		jQuery("#sort-by li.genre").click(function () {
			if (jQuery(listitem).hasClass('active')) return;
			var listitem =  jQuery(this);
			var genreclass = jQuery(listitem).attr('ca:typename');
			jQuery(listitem).addClass('active').siblings().removeClass('active');
			toggleDirectorsByGenre(genreclass);
		});
		
		
		// TAGS
		jQuery("li.filter-tag").click(function () {
		
			var parent = jQuery( this ).parent().parent();
			
			jQuery( 'li.filter-tag' , parent ).removeClass( 'active' );

			var listitem =  jQuery(this);

			var loadResults = function(data) {
				if (data.length > 0) {
				
					jQuery('.director').hide();
					
					for (item in data) {
						jQuery('#director-'+data[item]).show();
					}
					
					
					
				}
				
				jQuery('.director').removeClass('first-child').removeClass('last-child');
				
				var itemCount = 0;
				
				jQuery('.director:visible').each(function (i) {
						itemCount++;
						if ((itemCount%4)==0) {
							jQuery(this).addClass('last-child');
						}
					});
	  		};

			var resultsAll = {
				dataType: 'json',
				success: loadResults,
				data: {
					search_action: 'ALL'
				}
			};

			var resultsGenre = {
				dataType: 'json',
				success: loadResults,
				data: {
					search_action: 'GENRE', 
					term_id: jQuery(this).attr('ca:id'), 
					term_type: jQuery(this).attr('ca:type')
				}
			};
			
			if (jQuery(this).hasClass('active')) {
				jQuery(listitem).removeClass('active');
				var settings = resultsAll;
			} else {
				jQuery(listitem).addClass('active').siblings().removeClass('active');
				var settings = resultsGenre;
			}

			jQuery.ajax(settings);
			
			

		});

		jQuery('#filter-tags').hide();
		jQuery('#search-by-tags').click(function () {
			jQuery('#filter-tags').slideToggle();
			toggleGenres(jQuery('#show-all'));
			jQuery('.director').show();
			toggleDirectorsByGenre('');
		});

		function toggleGenres(active) {
			jQuery(active).siblings().removeClass('active');
			jQuery(active).addClass('active');
		}

		jQuery('#show-all').click(function () {
			toggleGenres(jQuery(this));
			jQuery('.director').show();
			toggleDirectorsByGenre('');
		});


	}
	
	//////////////////////////////////
	/// DIRECTOR SINGLE PAGE
	//////////////////////////////////
	
	if (jQuery("body").hasClass('archive') && jQuery("#director-reel").size()>0) {
		
		
		jQuery(window).hashchange( function(){
		
			jQuery( '.work-item' ).hide();
  			
  			var fadeSpeed = 1200;
  			var url = "" + location;
			url = url.substr( 0 , url.indexOf( '#' ) );
  		  	var hash = location.hash;
  		  	hash = hash.replace( '#' , '' );
  		  	
  		  	if( hash == 'promo' && jQuery(".promo").size() > 0) {
			
				jQuery( '.work-item.promo' ).show();
				jQuery( '.reels' ).empty();
				jQuery( '.reels' ).append( '<a href="' + url + '#">Go to Commercial reel</a>' );
				
				jQuery( '#director-biography .film-index-item' ).each(function() {
				
					if( !jQuery( this ).hasClass( 'promo' ) ) {
					
						jQuery( this ).hide();
					
					} else {
					
						jQuery( this ).show();
					
					}
				
				});
		
			} else {
			
				jQuery( '.normal' ).show();
				jQuery( '.reels' ).empty();
				jQuery( '.reels' ).append( '<a href="' + url + '#promo">Go to Music Video reel</a>' );
				
				jQuery( '#director-biography .film-index-item' ).each(function() {
				
					if( !jQuery( this ).hasClass( 'normal' ) ) {
					
						jQuery( this ).hide();
					
					} else {
					
						jQuery( this ).show();
					
					}
				
				});
			
			}
  		  	
  		})
  		
  		jQuery(window).hashchange();

	}


		jQuery('.film-index-item a').click(function () {
			jQuery.scrollTo(
				'a[name="' + jQuery(this).parent().attr('fr-name') + '"]',
				800,
				{ 'offset' : {
					'left': 0,
					'top': -37
				}}
			);
			return false;
		});


	// end ready
	});
