﻿$(function()
{	
	///************************************************* rating */
	$('ul.movieUtils li.movieRating .star-rating').rating({
		focus: function(value, link)
		{
			var tip = $('#star-info');
			tip[0].data = tip[0].data || tip.html();
			tip.html(link.title || 'value: ' + value);
		},
		blur: function(value, link)
		{
			var tip = $('#star-info');
			$('#star-info').html(tip[0].data || '');
		},
		callback: function(value, link)
		{
			RateProduct(value);
		}
	});
	
	$("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 < 670 ? VH : 670), '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').hasClass('boxMovieFull'))
		$('#boxMovie').addClass('boxMovieFull');
	else if (!HDMode && $('#boxMovie').hasClass('boxMovieFull'))
		$('#boxMovie').removeClass('boxMovieFull');
	
	$('#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/NXTPlayer.xap',
		$('#' + containerID).get(0),
		playerID,
		{
			width: width,
			height: height,
			background: 'black',
			version: '3.0.40624.0',
			autoUpgrade: 'true',
			windowless: 'true',
			enableHtmlAccess: 'true',
			enableGPUAcceleration: 'true'
		},
		{},
		initParams,
		null
	);
}

function onPlayerResize(size)
{
	var o = document.getElementById('player'), w = 640, h = 385;

	switch (size)
	{
		case 'normal':
			w = 640;
			h = 385;
			if ($('#boxMovie').hasClass('boxMovieFull'))
				$('#boxMovie').removeClass('boxMovieFull');
			break;

		case 'wide':
			w = 960;
			h = 534;
			if (!$('#boxMovie').hasClass('boxMovieFull'))
				$('#boxMovie').addClass('boxMovieFull');
			break;
	}

	o.setAttribute('width', w);
	o.setAttribute('height', h);
}
function onMovieEnd()
{
}
function onPlayerError(msg)
{
	alert('Player-ul a intampinat o eroare de retea:\n' + msg + '\nPagina se va reincarca!');
	top.location.reload();
}

/****************************** 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 GoToURL(URL)
{
	window.location.href = URL;
}

function CloseFancyBox()
{
	$.fn.fancybox.close();
}

/************************** 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;
}
