var $j = jQuery.noConflict();
var movie_index = 0;
var movie_xScroll;
var movie_yScroll;
var movie_maxWidth;
var movie_maxHeight;
var movie_minWidth = 150;
var cacheMovie = null;
var movie_title = "";
var movie_api = "";
var movie_has_prev = false;
var movie_has_next = false;

function movieClick(event){
	$j("select").hide();
	var t = event;
	movie_index = t.rel - 1;
	movie_title = event.title;
	setMovieInnerHtml(t.href);
	return false;
};
function setMovieInnerHtml(image, item_title){

	setMovieApi(image);

	//ajax request
	$j.ajax({
		type: 'GET',
		url: "/people/"+detail_id+"/mo/movie/",
		data: {
			src: image
		},
		success: function(data){
			var navi = '';
			if(movie_has_prev){
				navi += '<a href="" id="lightbox-prev"></a>';
			}
			if(movie_has_next){
				navi += '<a href="" id="lightbox-next"></a>';
			}
			var price = $j("#drama .buy .amazon .lowestprice").html();
			$j("#lightbox-layer-image .buy .amazon a").attr("href", "http://www.amazon.co.jp/exec/obidos/ASIN/"+asin+"/mucollepc-22/ref=nosim/");
			if(price == ""){
				$j("#lightbox-layer-image .buy .amazon").hide();
			}
			if(typeof item_title != "undefined"){
				$j("#lightbox-playing").html(item_title);
				$j("#lightbox-nav").html(navi);
			}else{
				$j("#lightbox-playing").html("");
				$j("#lightbox-nav").html("");
			}
			$j("#lightbox-imageContainer").html(data);
			$j("#lightbox-infoHeader").html(movie_title);

			cacheMovie = new Array();
			cacheMovie.width = 468;
			cacheMovie.height = 390;
			if($j('#lightbox-layer-info').css("display") == "none"){
				movieLoaded();
			}

			$j('#lightbox-prev').click(function(event){
				prevMovie(event);
				return false;
			});
			$j('#lightbox-next').click(function(event){
				nextMovie(event);
				return false;
			});
		}
	});
};

function prevMovie(event){
	var title = $j("#songs .disc .tracks .now a").attr("title");
	var href = "";
	var prevTitle = "";
	var prevHref = "";
	var artist = $j("#discography .title strong").html();
	if(artist_yt_word != ''){
		artist = artist_yt_word;
	}
	$j("#songs .disc .tracks a").each(function(){
		if($j(this).attr("title") == title){
			title = prevTitle;
			href = prevHref;
			return false;
		}
		prevTitle = $j(this).attr("title");
		prevHref = $j(this).attr("href");
		i++;
	});
	movie_title = title;
	getYoutubeMovieDetail(artist, title);
};
function nextMovie(event){
	var title = $j("#songs .disc .tracks .now a").attr("title");
	var href = "";
	var artist = $j("#discography .title strong").html();
	if(artist_yt_word != ''){
		artist = artist_yt_word;
	}
	var loopEnd = false
	$j("#songs .disc .tracks a").each(function(){
		if(loopEnd == true){
			title = $j(this).attr("title");
			href = $j(this).attr("href");
			return false;
		}
		if($j(this).attr("title") == title){
			loopEnd = true
		}
	});
	movie_title = title;
	getYoutubeMovieDetail(artist, title);
};

function movieLoaded(){
	if (cacheMovie == null) return;
	if(cacheMovie.width == 0){
		setTimeout("movieLoaded()", 100);
		return;
	}
	var height = cacheMovie.height;
	var width = cacheMovie.width;
	movie_maxWidth = $j(window).width() * 0.8;
	movie_maxHeight = $j(window).height() * 0.8;
	if(isIE){
		//IE
		if(movie_api == "yt"){
			width = 468;
			height = 390;
		}
	}
	var hPer = movie_maxHeight / cacheMovie.height;
	var wPer = movie_maxWidth / width;

	$j('#lightbox-layer-image').show();

	if (self.pageYOffset)
	{	// Some browser
		movie_yScroll = self.pageYOffset;
		movie_xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop)
	{	// Explorer 6 Strict
		movie_yScroll = document.documentElement.scrollTop;
		movie_xScroll = document.documentElement.scrollLeft;
	} else if (document.body)
	{	// All other browsers
		movie_yScroll = document.body.scrollTop;
		movie_xScroll = document.body.scrollLeft;	
	}
	var top = ($j(window).height() - height - 200) / 2 + movie_yScroll;
	var left = ($j(window).width() - width) / 2 + movie_xScroll;
	$j('#lightbox-imageContainer').height(height).width(width);
//	$j('#lightbox-layer-image').height(parseInt(height)+parseInt($j("#lightbox-nav").height())).width(width);
	$j('#lightbox-infoFooter').width(width);
	$j('#lightbox-layer-loading').hide();

	if (self.pageYOffset)
	{	// Some browser
	} else if (document.documentElement && document.documentElement.scrollTop){
		//IE6 Only
		$j('#lightbox-layer-info').css('top', top);
	}

	$j('#lightbox-layer-info').css('left', left).show();
	$j("#lightbox-layer-info").draggable({opacity:1, containment:"document"});
}

$j(document).ready(function () {
	$j('#lightbox-close').click(function(){closeMovieLightBox();return false;});
	$j("#lightbox-layer-loading img").load(function(){showMovieLoading();$j('#lightbox-layer-loading').hide();$j('#lightbox-layer-info').hide();});
});

function closeMovieLightBox(){
	$j('#lightbox-layer-info').hide();
	$j('select').show();

	$j("#songs .disc .tracks li").each(function(){
		if($j(this).attr("class") == "now"){
			$j(this).attr("class", "");
			return false;
		}
	});

	cacheMovie = null;
}

function showMovieLoading(){
	$j('#lightbox-layer-image').hide();
	if (self.pageYOffset)
	{	// Some browser
		movie_yScroll = self.pageYOffset;
		movie_xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop)
	{	// Explorer 6 Strict
		movie_yScroll = document.documentElement.scrollTop;
		movie_xScroll = document.documentElement.scrollLeft;
	} else if (document.body)
	{	// All other browsers
		movie_yScroll = document.body.scrollTop;
		movie_xScroll = document.body.scrollLeft;	
	}
	var top = ($j(window).height() - $j('#lightbox-layer-info').height()) / 2 + movie_yScroll;
	var left = ($j(window).width() - $j('#lightbox-layer-info').width()) / 2 + movie_xScroll;
//	$j('#lightbox-layer-info').css('top', top).css('left', left).show();
	$j('#lightbox-layer-loading').show();
}

function setMovieApi(url){
	if(url.toString().indexOf("youtube", 0) != -1){
		movie_api = "yt"
	}
	if(url.toString().indexOf("ameba", 0) != -1){
		movie_api = "am"
	}
	if(url.toString().indexOf("zoome", 0) != -1){
		movie_api = "zm"
	}
	if(url.toString().indexOf("veoh", 0) != -1){
		movie_api = "ve"
	}
}

function isIE(){
	if(navigator.userAgent.indexOf("MSIE") != -1){ //
		return true;
	}
	return false;
}

