$( function() {
	$( 'div.pngfix' ).pngFix();

	$( '#shows' ).wmFade();
	bindPegs();

	$('div.jScrollPane').jScrollPane({showArrows:true});
	$( 'div.custom-drop div.holder-scroll-pane' ).hide();

	$( 'div.custom-drop a.customClick' ).live( 'click', function() {
		var $container = $( this ).parents( 'div.custom-drop' );
		if( $( this ).hasClass( 'custom-dd-arrow1' ) ) {
			$( 'div.custom-drop' ).hide();
			$container.show();
			$( 'div.custom-dd', $container ).removeClass( 'custom-dd' ).addClass( 'custom-dd2' );
			$( 'div.custom-dd-lft', $container ).removeClass( 'custom-dd-lft' ).addClass( 'custom-dd-lft2' );
			$( 'div.custom-dd-mid', $container ).removeClass( 'custom-dd-mid' ).addClass( 'custom-dd-mid2' );
			$( 'div.custom-dd-rt', $container ).removeClass( 'custom-dd-rt' ).addClass( 'custom-dd-rt2' );
			$( this ).removeClass( 'custom-dd-arrow1' ).addClass( 'custom-dd-arrow2' );
			$( 'div.holder-scroll-pane', $container ).show();
		} else {
			$( 'div.custom-drop' ).show();
			$( 'div.custom-dd2', $container ).removeClass( 'custom-dd2' ).addClass( 'custom-dd' );
			$( 'div.custom-dd-lft2', $container ).removeClass( 'custom-dd-lft2' ).addClass( 'custom-dd-lft' );
			$( 'div.custom-dd-mid2', $container ).removeClass( 'custom-dd-mid2' ).addClass( 'custom-dd-mid' );
			$( 'div.custom-dd-rt2', $container ).removeClass( 'custom-dd-rt2' ).addClass( 'custom-dd-rt' );
			$( this ).removeClass( 'custom-dd-arrow2' ).addClass( 'custom-dd-arrow1' );
			$( 'div.holder-scroll-pane', $container ).hide();
		}
	});

	$( 'a.area_list' ).live( 'click', function( e ) {
		e.preventDefault();
		var $this = $( this );
		if( $this.parent().hasClass( 'regions' ) ) {
			var decades = $this.attr( 'decades' ).split( ',' );
			var genres = $this.attr( 'genres' ).split( ',' );
			$( 'div.genres' ).each( function() {
				if( genres.indexOf( $( 'a', this ).attr( 'rel' ) ) >=0 ) {
					$( this ).show();
				} else {
					$( this ).hide();
				}
			});
			$( 'div.decades' ).each( function() {
				if( decades.indexOf( $( 'a', this ).attr( 'rel' ) ) >=0 ) {
					$( this ).show();
				} else {
					$( this ).hide();
				}
			});
		}
		var titleDiv = $this.parent().parent().parent().parent().prev()[ 0 ];
		$( titleDiv ).attr( 'rel', $this.attr( 'rel' ) ).text( $this.text() );
		$( 'a.customClick', $this.parents( 'div.custom-drop' ) ).trigger( 'click' );
	});

	$( 'input.backButton' ).live( 'click', function( e ) {
		$( 'div.dynamicText' ).html( backHtml );
		$( this ).show();
		$( this ).removeClass( 'backButton' ).addClass( 'movieSubmit' ).val( 'Submit' );
	});

	$( 'input.movieSubmit' ).live( 'click', function( e ) {
		var args = { length : 0 }, $this = $( this );
		$( 'div.sectionViewText' ).each( function() {
			if( $( this ).attr( 'rel' ) != 0 ) {
				args[ this.className.split( ' ' )[ 0 ].replace( 'Value', '' ) ] = $( this ).attr( 'rel' );
				args.length++;
			}
		});
		if( args.length > 0 ) {
			$.ajax( {
				url : '/ajax/movie_o_dex.php',
				type : 'post',
				data : args,
				success : function( resp ) {
					window.backHtml = $( 'div.dynamicText' ).html();
					$( 'div.dynamicText' ).html( resp );
					$( 'div.dynamicText div.jScrollPane' ).jScrollPane( { showArrows : true } );
					$this.removeClass( 'movieSubmit' ).addClass( 'backButton' ).val( 'Back' );
				}
			});
		} else {
			alert( 'Please select atleast one of the above options!' );
		}
	});

	$( 'div.movie_list' ).live( 'click', function() {
		$.ajax( {
			url : '/ajax/movie_o_dex.php',
			type : 'post',
			data : 'movie_id=' + $( this ).attr( 'rel' ),
			success : function( resp ) {
				$( 'div.dynamicText' ).html( resp );
				$( 'input.backButton' ).hide();
			}
		});
	});

	$( 'a.back_anchor' ).live( 'click', function() {
		$( 'input.backButton' ).trigger( 'click' );
	});

	$( 'a.thumbnails-vedio , a.wm-exp' ).click( function() {
		$( getYoutubeVideo( $( this ).attr( 'rel' ) ) ).appendTo( document.body ).lightbox({backColor : '#000', opacity : .9 });
	});

	$( document ).bind( 'keypress', function( e ) {
		var $workWith = $( 'div.holder-scroll-pane:not(:hidden)' );
		if( $workWith.length > 0 ) {
			$( 'div.single-list', $workWith ).each( function() {
				if( String.fromCharCode( e.charCode ) == $( this ).text().trim().toLowerCase().substr( 0, 1 ) ) {
					var t = $(window).scrollTop(), l = $(window).scrollLeft();
					this.scrollIntoView();
					$(window).scrollTop( t );
					$(window).scrollLeft( l );
					return false;
				}
			});
		}
	});

	$( 'div.vedio-thumbs a.thumbnails-vedio:gt(5)' ).each( function() {
		$( this ).hide();
	});

	$( 'div.vedio-thumbs a.pre' ).click( function( e ) {
		e.preventDefault();
		if( $( this ).next().hasClass( 'clear' ) ) {
			if( !$( 'div.vedio-thumbs a.thumbnails-vedio:not(:hidden):last' ).next().hasClass( 'pre' ) ) {
				$( 'div.vedio-thumbs a.thumbnails-vedio:not(:hidden):last' ).next().show();
				$( 'div.vedio-thumbs a.thumbnails-vedio:not(:hidden):first' ).hide();
			}
		} else {
			if( !$( 'div.vedio-thumbs a.thumbnails-vedio:not(:hidden):first' ).prev().hasClass( 'pre' ) ) {
				$( 'div.vedio-thumbs a.thumbnails-vedio:not(:hidden):first' ).prev().show();
				$( 'div.vedio-thumbs a.thumbnails-vedio:not(:hidden):last' ).hide();
			}
		}
	});

	$( 'a.rateSubmitButton' ).live( 'click', function() {
		var movie_id = $( this ).attr( 'rel' ), points = $( '#peg_rate a.glass1' ).length + ( 0.5 * $( '#peg_rate a.glass3' ).length ), page = $( this ).parent().attr( 'rel' );
		$.ajax( {
			type: "POST",
			url: "/ajax/tweet_movie.php",
			data: "movie_id=" + movie_id + "&points=" + points + "&page=" + page,
			success: function( msg ) {
				$( '#movie_tweet' ).html( msg );
				pegState = 1
				bindPegs();
			}
		});
	});
});

var pegState = 1;

function bindPegs() {
	$( '#peg_rate a' ).each( function() {
		if( !this.defaultClass ) {
			this.defaultClass = this.className;
		}
	}).bind( 'mouseenter', function() {
		if( pegState == 1 ) {
			var itsNewClass = this.className == 'glass5' ? 'glass3' : 'glass1';
			$( this ).attr( 'class', itsNewClass ).prevAll().attr( 'class', 'glass1' ).end().nextAll().attr( 'class', 'glass5' );
		}
	}).bind( 'mouseleave', function() {
		if( pegState == 1 ) {
			$( '#peg_rate a' ).each( function() {
				this.className = this.defaultClass;
			});
		}
	});
}

function tweet_movie() {
	$( 'div.tweet-button' ).show();
	pegState = 2;
	$( 'a.glass3' ).click( function() {
		if( pegState == 2 ) {
			this.className = 'glass1';
		}
	});
}
