﻿$(function()
{
	//generate radios from global var movieRating
	GenerateRadios();
	
	$("a#send_friend").fancybox({ 'hideOnContentClick': false, 'enableEscapeButton': true, 'frameWidth': 460, 'frameHeight': (VH < 620 ? VH : 620), 'overlayOpacity': 0.8, 'overlayColor': '#000' });
	$("a#create_report").fancybox({ 'hideOnContentClick': false, 'enableEscapeButton': true, 'frameWidth': 900, 'frameHeight': (VH < 660 ? VH : 660), 'overlayOpacity': 0.8, 'overlayColor': '#000' });
	$("a#create_test_report").fancybox({ 'hideOnContentClick': false, 'enableEscapeButton': true, 'frameWidth': 900, 'frameHeight': (VH < 660 ? VH : 660), 'overlayOpacity': 0.8, 'overlayColor': '#000' });
	$("a#embed").fancybox({ 'hideOnContentClick': false, 'enableEscapeButton': true, 'overlayOpacity': 0.8, 'overlayColor': '#000' });

	$("a#not_licensed_no_money").fancybox({ 'hideOnContentClick': false, 'enableEscapeButton': true, 'overlayOpacity': 0.8, 'overlayColor': '#000' });
	$("a#not_licensed_has_money").fancybox({ 'hideOnContentClick': false, 'enableEscapeButton': true, 'overlayOpacity': 0.8, 'overlayColor': '#000' });
	$("a#why_pay").fancybox({ 'hideOnContentClick': false, 'enableEscapeButton': true, 'overlayOpacity': 0.8, 'overlayColor': '#000' });

	//BONUS GALLERY
	$("#gallery_carousel").jcarousel({
		initCallback: galleryInitCallback
	});
});

function resizePlayer(width, height)
{
	var w = 640, h = 385;

	if (width != 0) w = width;
	if (height != 0) h = height;

	var HDMode = (parseInt(w) == 960);

	if (HDMode)
		$('#boxMovie').addClass('boxMovieFull');
	else
		$('#boxMovie').removeClass('boxMovieFull');

	$('#boxMovie').height(h + 25);
	$('#player').width(w).height(h);
}

function embedTrailer(productID)
{
	var player = APP_PATH + 'controls/trailer/player.swf',
		installer = APP_PATH + 'controls/trailer/expressInstall.swf',
		flashVars = { 'key': productID },
		paramsObj = { 'id': 'player', 'name': 'player', 'allowScriptAccess': 'always', 'allowFullScreen': true, 'quality': 'high', 'bgcolor': '#000000', 'wmode': 'transparent' };
	swfobject.embedSWF(player, 'player', '640', '385', '10.0.0', installer, flashVars, paramsObj);
}

function embedPlayer(containerID, playerID, width, height, initParams)
{
	Silverlight.createObject(
		APP_PATH + 'controls/drm/NXTPlayer4.xap?' + parseInt(Math.random() * Math.pow(10, 17)),
		$('#' + containerID).get(0),
		playerID,
		{
			width: width,
			height: height,
			background: 'black',
			version: '4.0.50401.0',
			autoUpgrade: 'true',
			windowless: 'true', //TODO: performance test of this setting
			enableGPUAcceleration: 'true'
		},
		{},
		initParams,
		null
	);
}
function OnResize_SLPlayer(mode)
{
	var w = 640, h = 385;
	var testMovie = ($('#movie_survey').length == 0);

	switch (mode)
	{
		case 'normal':
			w = 640;
			h = 385;
			$('#boxMovie').removeClass('boxMovieFull');
			break;

		case 'wide':
			w = 960;
			h = 534;
			$('#boxMovie').addClass('boxMovieFull');
			break;
	}

	$('#boxMovie').height(h + (testMovie ? 0 : 25));
	$('#player').width(w).height(h);
}
function OnMovieFinished_SLPlayer(movieType)
{
}
function OnPlaylistFinished_SLPlayer()
{
	//removed
	//if ($('#movie_survey').length == 1)
	//	ShowSurvey();
}
function ShowSurvey()
{
	return;
	if ($('#boxMovie').hasClass('boxMovieFull'))
		$('#boxMovie').removeClass('boxMovieFull');

	$('#boxMovie').height(410);
	$('#player').width(640).height(385);

	$('#movie_player').css('left', -10000 + 'px');
	$('#movie_survey').show();
}
function HideSurvey()
{
	$('#movie_survey').hide();
	$('#movie_player').css('top', 0).css('left', 0);
}

/****************************** VEZI FILM *************************************/

function ViewMovie()
{
	$pusher.Call('VIEW_MOVIE');
}

function ViewTestMovie()
{
	$pusher.Call('VIEW_TEST_MOVIE');
}

function ChargeMovie(productID)
{
	$pusher.Call('CHARGE_MOVIE', productID);
}

function OnCheckToViewMovieComplete(status, message, url, price)
{
	switch (message)
	{
		case 'NOT_LOGGED':
			LoginForm(true);
			break;
		case 'LOGGED': //can view test movie
			window.location = url;
			break;

		case 'NOT_LICENSED_NO_MONEY':
			$('a#not_licensed_no_money').trigger('click');
			break;

		case 'NOT_LICENSED_HAS_MONEY':
			$('#product_price').text(price);
			$('a#not_licensed_has_money').trigger('click');
			break;

		case 'LICENSED':
			window.location = url;
			break;
		default: //DEFAULT ERROR			

			break;
	}
}

function OnChargeMovieComplete(status, message, url)
{
	if (status)
		window.location = url;
	else
	{
		$('#span_not_licensed_has_money').html(message);
		$('#span_not_licensed_has_money').css('color', 'Red');
		$('a#not_licensed_has_money').trigger('click');
	}
}

function CloseFancyBox()
{
	$.fn.fancybox.close();
}

function GoToTestMovie(testUrl)
{
	CloseFancyBox();
	window.location = testUrl;
}

/************************** PRODUCT INFORMATION SIDEBAR **************************/

//used also in report.js for verifications
function ScrollToSynopsys()
{
	$('.movieDescription .synopsys P A').click(function()
	{
		$.scrollTo($('#anchorMovieFull'), 500);
		return false;
	});
}

/******************************* RATE PRODUCT ***********************************/

function RateProduct(value)
{
	$pusher.Call('RATINGS_ADD', value);
}

function OnRatingsAddComplete(value, message)
{
	if (value > 0)			//wishlist add success				
	{
		$().rating('select', value);
		$('.star-rating').rating('disable');
	}
	else					//wishlist add default error
		alert(message);
}

function CloseFancyBox()
{
	$.fn.fancybox.close();
}

//************************************************* BONUS GALLERY ****************************************************//
var currentItemIndex = -1;
function ShowBonusItem(itemID)
{
	//item: {id:'{0}',mediaType:'{1}',title:'{2}',thumbUrl:'{3}',mediaSource:'{4}'}

	if (typeof (JSGallery) == 'undefined')
		return;

	var item = null;
	for (var i = 0; i < JSGallery.length; i++)
	{
		if (JSGallery[i].id == itemID)
		{
			item = JSGallery[i];
			currentItemIndex = i;
			break;
		}
	}

	if (item == null || currentItemIndex == -1)
		return;

	if ($('#boxGalleryFotoBig').css('display') == 'none')
	{
		$('#boxGalleryFotoBig').css('display', '');
		$.scrollTo($("#boxGalleryFotoBig"), 500);
	}

	if (item.mediaType == 'Video')
	{
		embedPlayer('bonus_video', 'bonus_player', '640', '385', item.mediaSource);
		$('#bonus_video').css('border', '1px solid #5D5048');
		
		$('#bonus_image').css('display', 'none');
		$('#bonus_video').css('display', '');
	}
	else
	{
		$('#bonus_video').empty();
		$('#bonus_image').attr('src', item.mediaSource);

		$('#bonus_video').css('display', 'none');
		$('#bonus_image').css('display', '');
	}

	$('#PrevItem').unbind('click');
	$('#NextItem').unbind('click');

	if (JSGallery.length < 2)
	{
		$('#PrevItem').removeClass('iconLeft').addClass('iconLeftInact');
		$('#NextItem').removeClass('iconRight').addClass('iconRightInact');
	}
	else if (currentItemIndex == 0)
	{
		$('#PrevItem').removeClass('iconLeft').addClass('iconLeftInact');
		$('#NextItem').removeClass('iconRightInact').addClass('iconRight');
	}
	else if (currentItemIndex + 1 == JSGallery.length)
	{
		$('#PrevItem').removeClass('iconLeftInact').addClass('iconLeft');
		$('#NextItem').removeClass('iconRight').addClass('iconRightInact');
	}
	else
	{
		$('#PrevItem').removeClass('iconLeftInact').addClass('iconLeft');
		$('#NextItem').removeClass('iconRightInact').addClass('iconRight');
	}

	if (!$('#PrevItem').hasClass('iconLeftInact'))
	{
		var prevItemID = JSGallery[currentItemIndex - 1].id;
		$('#PrevItem').bind('click', function() { ShowBonusItem(prevItemID); });
	}
	if (!$('#NextItem').hasClass('iconRightInact'))
	{
		var nextItemID = JSGallery[currentItemIndex + 1].id;
		$('#NextItem').bind('click', function() { ShowBonusItem(nextItemID); });
	}
	
	//unhighlight all
	$('#gallery_carousel li a').removeClass('selected');

	//highlight selected li
	$('#gallery_' + itemID).addClass('selected');

	//css purposes
	$('.boxGalleryFotoBig .iconLeft, .boxGalleryFotoBig .iconRight, .boxGalleryFotoBig .iconLeftInact, .boxGalleryFotoBig .iconRightInact').vAlign();
}
function CloseBonusItem()
{
	$('#bonus_video').empty();
	$('#boxGalleryFotoBig').css('display', 'none');
	currentItemIndex = -1;
}

// Used to assign functionality to the controls (left an right big arrows)
function galleryInitCallback(carousel)
{
	$('#NextItem').live('click', function()
	{
		SlideCarousel(carousel);
		return false;
	});

	$('#PrevItem').live('click', function()
	{
		SlideCarousel(carousel);
		return false;
	});
};

//Scrolls the carousel until the picture/video is visible
function SlideCarousel(carousel)
{
	var visible = false,
		scrollDirection = CheckItemVisibility();

	if (scrollDirection < 0)
		carousel.prev();
	else if (scrollDirection > 0)
		carousel.next();
	else
		visible = true;

	if (!visible)
		setTimeout(function() { SlideCarousel(carousel); }, 300);
}
function CheckItemVisibility()
{
	if ($('#scroller_container').length == 0)
		return 0;

	var currentItem = $('#gallery_' + JSGallery[currentItemIndex].id);

	if (currentItem.length == 0)
		return 0;

	var ref = $('#scroller_container');
	var refC = GetXY(ref.get(0));
	var XMin = refC.x + 30,
		XMax = refC.x + ref.outerWidth() - 30;
	
	var currentItemX = GetXY(currentItem.get(0)).x;

	if (currentItemX < XMin)
		return -1;
	else if (currentItemX > XMax)
		return 1;
	else
		return 0;
}

/**************** IMDB RATING ***********/
function GenerateRadios()
{
	if (typeof IMDBRating == 'undefined')
		return;

	var inputArr = [];
	for(var count = parseFloat(0.25); count <= 10; count = parseFloat(count) + 0.25)
	{
		inputArr.push('<input type="radio" class="star-rating {split:4}" value="');
		
		if(count == parseFloat(IMDBRating))
			inputArr.push('" checked="checked" ');		
			
		inputArr.push(count);
		inputArr.push('" disabled="disabled" />');
	}
	
	$('#imdb').append(inputArr.join(''));	
	$('#imdb input.star-rating').rating();
}
