
(function($) {
	$.fn.initCycle = function(options) {
			if (typeof (options) == 'undefined') {
				options = {};
				$fx = $.fn.initCycle.defaults.fx;
				$timeout = $.fn.initCycle.defaults.timeout;
			}	else {
				if (typeof (options.fx) == 'undefined'){
					$fx = $.fn.initCycle.defaults.fx;
				}else{
					$fx =  options.fx;
				}
				if (typeof (options.timeout) == 'undefined'){
					$timeout = $.fn.initCycle.defaults.timeout;
				}else{
					$timeout =  options.timeout;
				}
				
				//$timeout = options.timeout;
			}	
			$carousel	= $(this).find('.list_items');
			$nav		= $(this).find('.nav');
			$next		= $(this).find('.next');
			$previous	= $(this).find('.previous');
			$pager		= $(this).find('.pager');

			if($carousel.children().length > 1) {
				$nav.show();
				$carousel.cycle({
					fx: 	$fx,
					speed:	300,
					timeout: $timeout,
					pager: $pager,
					next:  $next,
					prev:  $previous,
					cleartypeNoBg:	true
				});
			} 
	};

	$.fn.initCycle.defaults = {
		fx: 'scrollHorz',
		timeout: 0
	};

})(jQuery);

	var player;
	function playerReady(obj) {
		player = window.document[obj.id];
	}
	function createPlayer(theFile, theImage, theType, uniqueID) {
		var flashvars = {
				file:theFile,
				image:theImage,
				type:theType,
				autostart:"true"
		}
		var params = {
				allowfullscreen:"true",
				allowscriptaccess:"always",
				wmode:"opaque"
		}
		var attributes = {
				id:"player"+uniqueID,
				name:"player"+uniqueID
		}
		swfobject.embedSWF(APP_URL+"graphics/MediaPlayer/player.swf", "player_holder"+uniqueID, "460", "365", "9.0.115", false, flashvars, params, attributes);
	}

	function callPlayer(videoUrl, imageURL, typeFile, titleFile, uniqueHolderID){

		$j.fancybox({
			'hideOnContentClick': false,
			'content'	    :	$j('<div id="player_holder'+uniqueHolderID+'"></div>'),
			'title'		    :	titleFile,
			'titlePosition'	:	'outside',
			'padding'		:	0,
			'margin'		:	20,
			'width'			:	460,
			'height'		:	365,
			'hideOnOverlayClick':false,
			'overlayColor'	:	'#000',
			'autoDimensions':	false,
			'overlayOpacity':	0.8,
			onComplete	:	function() {
				createPlayer(videoUrl, imageURL, typeFile, uniqueHolderID);
			},
			onCleanup	:	function() {
				//$j('#player_holder{{objId}}').remove();
				swfobject.removeSWF("player"+uniqueHolderID);
			}
		});
	}


function loadGalleryPopup(theURL, theID){
		$j.ajax({
		cache: false,
		url:  theURL,
		dataType: "html",
		success: function(data) {
			$j.fancybox({
				'hideOnContentClick': false,
				'padding'		:	0,
				'margin'		:	20,
				'width'			:	670,
				'height'		:	600,
				'hideOnOverlayClick':true,
				'autoScale'		:	false,
				'overlayColor'	:	'#000',
				'autoDimensions':	false,
				'overlayOpacity':	0.8,
				'titlePosition'	:	'over',
				'content'		:  $j('<div class=\"popupContainer\" id=\"popupContainer'+theID+'\">'+$j(data).filter('#MainContainer').html()+'</div>'),
				onComplete		:	function(){
					if ($j('#popupContainer'+theID+' #gallery_navigation').length){
						var gallery = $j($j('#popupContainer'+theID+' #gallery_navigation')).galleriffic({
							numThumbs:					7,
							preloadAhead:              1,
							renderSSControls:          false,
							renderNavControls:         false,
							imageContainerSel:			$j('#popupContainer'+theID+' #image_holder'),
							enableBottomPager:			true,
							bottomPagerContainerSel:	$j('#popupContainer'+theID+' #bottom'),
							prevPageBottomPagerSel:		$j('#popupContainer'+theID+' #bottom_pager .prev'),
							nextPageBottomPagerSel:		$j('#popupContainer'+theID+' #bottom_pager .next'),
							controlsContainerSel:      $j('#popupContainer'+theID+' #controls'),
							captionContainerSel:		$j('#popupContainer'+theID+' #caption'),
							loadingContainerSel:       $j('#popupContainer'+theID+' #loading'),
							prevPageLinkText:			'',
							nextPageLinkText:			'',
							syncTransitions:           false,
							onSlideChange:             function(prevIndex, nextIndex) {
								$j('#popupContainer'+theID+' #photo-index').html('	Photo '+ (nextIndex+1) +' / '+ this.data.length);
								//alert ($j('#popupContainer'+theID+' #bottom').html());
								//$j.fancybox.resize();
							},
							onTransitionOutStart:		function(){
								var containerH = this.$imageContainer.height();
								var captionH = this.$captionContainer.height();
								var newLoadingH = containerH + captionH;
								//this.$loadingContainer.find('.animation').height(containerH);
								this.$loadingContainer.height(newLoadingH);
							},
							onTransitionIn:		function(newSlide, newCaption, isSync){
								$j.fancybox.resize();
								newSlide.fadeTo(this.getDefaultTransitionDuration(isSync), 1.0);
							},
							onSlideShowPause:			function(){
								//alert("haleluja");
								this.$controlsContainer.find('a.play').show();
								this.$controlsContainer.find('a.pause').hide();
							},
							onSlideShowPlay:			function(){
								//alert("Started");
								this.$controlsContainer.find('a.play').hide();
								this.$controlsContainer.find('a.pause').show();
							},
							onPageTransitionOut:       function(callback) {
								this.fadeTo('fast', 0.0, callback);
							},
							onPageTransitionIn:        function() {
								this.fadeTo('fast', 1.0);
							}
						});
						gallery.$controlsContainer.find('a.next').click(function(e) {
							gallery.next(false);
							e.preventDefault();
						});
						gallery.$controlsContainer.find('a.prev').click(function(e) {
							gallery.previous();
							e.preventDefault();
						});
						gallery.$controlsContainer.find('a.play').click(function(e) {
							gallery.play();
							$j(this).hide();
							gallery.$controlsContainer.find('a.pause').show();
							e.preventDefault();
						});
						gallery.$controlsContainer.find('a.pause').click(function(e) {
							gallery.pause();
							$j(this).hide();
							gallery.$controlsContainer.find('a.play').show();
							e.preventDefault();
						});

					}
				}

			});
		}
	});
	//return false;
}



