
/*
**  MAC Common
*/

;(function($) {

	$.fn.MacCommon = function() {

		var $this = $(this);

		// init
		function _init() 
		{
			//console.log('MacCommon :: init');
			if ($this.length)
			{
				_initTop();
				_initImagePattern();
			}
		};

		function _initTop () 
		{
			var top = $this.find('.top a');
			top.attr('href', 'javascript:;');
		 	top.click (
				function(){ 
					$this.scrollTo(0, 500);
				}
			);
		}

		function _initImagePattern() 
		{
			var image = $this.find('.image-pattern');
			image.append('<div class="pattern"></div>');
		}

		_init();

		return this;
	};

})(jQuery);

/*
**  MAC Menu
*/

;(function($) {

	$.fn.MacMenu = function() {

		var $this = $(this),
		$header = $this.find('#header'),
		$menu = $header.find('.sections'),
		$menuItems = $menu.find('li.item .item-container'),
		$buttonContainer = $this.find('#entity .entity-container'),
		$button = $buttonContainer.find('.open');

		// init
		function _init() 
		{
			//console.log('MacMenu :: init');
			if ($button.length)
			{
				_initMenuClosed();
			}
			 else
			{
				_enableMenu();	
			}
		};

		function _initMenuOpened() 
		{
			$menu.bind('mouseleave', _closeMenu);
			$button.css('background-position', 'left -200px');
			$button.show();
		};

		function _initMenuClosed() 
		{
			$button.bind('mouseenter', _openMenu);
			$button.css('background-position', 'left 0px');
			$button.show();
		};

		// enable / disable
		function _enableMenu ()
		{
			$menuItems.each(
				function() 
				{
					$(this).bind('mouseenter',
						function () {
							$(this).parent().addClass('item-hover');
							Cufon.refresh();
						});
						
					$(this).bind('mouseleave',
						function () {
							$(this).parent().removeClass('item-hover');
							Cufon.refresh();
						}
					);
				}
			);
		}
		
		function _disableMenu ()
		{
			$menuItems.each(
				function() 
				{
					$(this).unbind('mouseenter');
					$(this).unbind('mouseleave');
				}
			);
		}

		// open
		function _openMenu() 
		{
			$button.unbind('mouseenter');
			$menu.slideDown('slow', _onOpenMenuDone);
			_enableMenu();
		};

		function _onOpenMenuDone() 
		{
			_initMenuOpened();
		};

		// close
		function _closeMenu() 
		{
			_disableMenu();
			$menu.unbind('mouseleave');
			$menu.slideUp('normal', _onCloseMenuDone);
		};

		function _onCloseMenuDone() 
		{
			_initMenuClosed();
		};

		_init();

		return this;
	};

})(jQuery);


/*
**  MAC Search
*/

;(function($) {

	$.fn.MacSearch = function() {

		var $this = $(this);

		// init
		function _init() 
		{
			//console.log('MacSearch :: init');
			if ($this.length)
			{
				_initSearch();
			}
		};

		function _initSearch () 
		{
			$('#search-window').jqm({
				trigger: '.search-trigger', 
				onShow: _onShowEvent
			});

			$('#search-window form').validate({
			 submitHandler: function(form) {
				_submitSearchForm(form.value.value);
				return false;
			 }});
		}

		function _onShowEvent (hash)
		{
			$('#search-window form input.value').attr('value', '');
			$('#search-window div.search-window-results').html('').css('display', 'none');
			hash.w.show();
		}

		function _submitSearchForm (value)
		{
			//_gaq.push(['_trackEvent', 'Search', 'Submit', value]);
			var html = '<img src="/themes/default/images/ajax-loader.gif" />';
			$('#search-window div.search-window-results').html(html);
			var url = $('#search-window form').attr('action') + "&value=" + value;

			$.ajax({ url: url,
				success: function(data) {
					$('#search-window div.search-window-results').html(data);
					$('#search-window div.search-window-results').fadeIn();
				}
			});
		}

		_init();

		return this;
	};

})(jQuery);

/*
**  MAC Home
*/

;(function($) {

	$.fn.MacHome = function() {

		var $this = $(this),
		$background = $this.find('#background');

		// init
		function _init() 
		{
			//console.log('MacHome :: init');
			if ($this.length)
			{
				$('#background').fullBg();
			}
		};

		_init();

		return this;
	};

})(jQuery);

/*
**  MAC News
*/

;(function($) {

	$.fn.MacNews = function() {

		var $this = $(this),
		$entity = $this.attr('data-entity'),
		$cookieName,
		$button = $this.find('.news-button'),
		$content = $this.find('.news-content'),
		$news = $content.find('ul.news'),
		$pager = $content.find('#news-pager');

		// init
		function _init() 
		{
			
			//console.log('MacNews :: init');
			if ($this.length > 0)
			{
				$cookieName = 'MAC_NEWS_ENTITY_' + $entity.toUpperCase() + '_INIT';
				_initCycle();
				if ($.cookie($cookieName) == '-1')
				{
					_closeNews();	
				}
				 else
				{
					_openNewsWithoutTween();
				}
			}
		}

		function _initButtonOpened() 
		{
			$button.css('cursor', 'pointer');
			$button.bind('click', _closeNews);
			$button.addClass('news-button-closed');

		};

		function _initButtonClosed() 
		{
			$button.css('cursor', 'pointer');
			$button.bind('click', _openNews);
			$button.removeClass('news-button-closed');
		};

		function _initCycle()
		{
			$cycle = $news.cycle({ 
				speed:   1000, 
 				timeout: 3000,
				pause: 'li.item',
				pager: '#news-pager'
			});
		}

		// disable
		function _disableButton ()
		{
			$button.unbind('click');
		}

		// open
		function _openNews() 
		{
			_disableButton();
			$content.slideDown('normal', _onOpenNewsDone);
		};

		function _openNewsWithoutTween() 
		{
			_disableButton();
			$content.show(); 
			_onOpenNewsDone();
		};

		function _onOpenNewsDone() 
		{
			$news.cycle('resume');
			$pager.show();
			_initButtonOpened();
			$.cookie($cookieName, '1', { expires: 1 });
		};

		// close
		function _closeNews() 
		{
			_disableButton();
			$news.cycle('pause');
			$pager.hide();
			$content.slideUp('normal', _onCloseNewsDone);
			$.cookie($cookieName, '-1', { expires: 1 });
		};

		function _onCloseNewsDone() 
		{
			_initButtonClosed();
		};

		_init();

		return this;
	};

})(jQuery);

/*
**  MAC Mosaic
*/

;(function($) {

	$.fn.MacMosaic = function() {

		var $this = $(this),
		$mosaic = $this.find('.mosaic'),
		$mosaicItems = $this.find('.mosaic li.item');

		// init
		function _init() 
		{
			//console.log('MacMosaic :: init');
			if ($this.length)
			{
				_initMosaicItems();
			}
		}

		function _initMosaicItems()
		{
			$mosaicItems.each(
				function() 
				{
					$(this).bind('mouseenter', function(){
						$(this).addClass('item-hover');
					});
					$(this).bind('mouseleave', function(){
						$(this).removeClass('item-hover');
					});
				}
			);
		}
		_init();
		return this;
	};

})(jQuery);

/*
**  MAC Agenda
*/

;(function($) {

	$.fn.MacAgenda = function(params) {

		params = $.extend( {month: 1}, params);

		var $this = $(this),
		$months = $this.find('.months'),
		$monthsItems = $months.find('li.item'),
		$eventsItems = $this.find('.events li.item'),
		$eventsItemArray = new Array(),
		$monthsItemArray = new Array();

		// init
		function _init() 
		{
			//console.log('MacMenu :: init');
			if ($this.length)
			{
				_initEvents();
				
				if ($this.hasClass('agenda-mac'))
				{
					_initMounths();
					_changeMounthFilter(params.month);
				}
			}
		}

		function _initEvents()
		{
			$eventsItems.each(
				function() 
				{
					$eventsItemArray.push(this);
				}
			);
		}

		function _initMounths() 
		{
			$monthsItems.each(
				function() 
				{
					var value = Number($(this).attr('data-month'));
					var enable = 0;

					if (Boolean(_isEventsFromMounth(value)))
					{
						$(this).bind('mouseenter', function(){
							$(this).addClass('item-hover');
						});
						$(this).bind('mouseleave', function(){
							$(this).removeClass('item-hover');
						});
						$(this).bind('click', function(){
							_changeMounthFilter(value);
						});
						$(this).addClass('enable');
						enable = 1;
					}
					  else 
					{
						$(this).addClass('disable');
						enable = 0;
					}
					
					$monthsItemArray.push(new Array(value, enable));
				}
			);
			$months.show();
		};

		// filters
		function _isEventsFromMounth(value)
		{
			var count = 0;
			for (var i=0; i<$eventsItemArray.length; i++)
			{
				var item = $eventsItemArray[i];
				var month_start = Number($(item).attr('data-month-start'));
				var month_end = Number($(item).attr('data-month-end'));
				if ((value >= month_start) && (value <= month_end))
				{
					count = 1;
					break;
				}
			}
			return (count)? true: false;
		}

		function _selectMounthFilters(value) 
		{
			$monthsItems.each(
				function() 
				{
					var month = Number($(this).attr('data-month'));
					if ( month == value)
					{
						$(this).addClass('selected');
					}
					 else
					{
						$(this).removeClass('selected');
					}
				}
			);
		};

		function _getVerifiedMounthFilter(value) 
		{
			var index = _getIndexMounthFilter(value);
			if ($monthsItemArray[index][1]) {
				return value;
			}
			 else
			{
				return _getNextVerifiedMounthFilter(index);
			}
		};

		function _getIndexMounthFilter(value) 
		{
			var index = -1;
			for (var i=0; i<$monthsItemArray.length; i++)
			{
				if ($monthsItemArray[i][0] == value)
				{
					index = i;
					break;
				}
			}
			return index;
		};

		function _getNextVerifiedMounthFilter(index) 
		{
			var nextIndex = 0;
			var $tempArray = $monthsItemArray.splice(index, $monthsItemArray.length - 1);
			for (var i=0; i<$tempArray.length; i++)
			{
				if ($tempArray[i][1])
				{
					nextIndex = $tempArray[i][0];
					break;
				}
			}
			return nextIndex;
		};

		// filtering
		function _changeMounthFilter(value)
		{
			value = _getVerifiedMounthFilter(value);
			_filterByMounth(value);
			_selectMounthFilters(value);
		}

		function _filterByMounth (value)
		{
			$eventsItems.each(
				function() 
				{
					$month_start = Number($(this).attr('data-month-start'));
					$month_end = Number($(this).attr('data-month-end'));
					if ((value >= $month_start) && (value <= $month_end))
					{
						 $(this).slideDown();
					}
					 else
					{
						$(this).slideUp();
					}
				}
			);
		}

		_init();

		$.fn.changeMounthFilter = function(month)
		{
			_changeMounthFilter(month);
		}
		
		return this;
	};

})(jQuery);

/*
**  MAC Grid
*/

;(function($) {

	$.fn.MacGrid = function() {

		var $this = $(this),
		$mosaic = $this.find('.mosaic'),
		$vg,
		$time = 200,
		$delay = 50;

		// init
		function _init() 
		{
			//console.log('MacMenu :: init');
			if ($this.length)
			{
				_initGrid();
			}
		}

		function _initGrid ()
		{
			if ($mosaic.length>0)
			{
				if ($this.hasClass('grid-no-fade'))
				{
					$time = 0,
					$delay = 0;
				}

				$vg = $mosaic.vgrid({
					easeing: "easeOutQuint",
					time: ($time*2),
					delay: 0,
					fadeIn: {
						time: $time,
						delay: $delay
					},
					onStart: function() {
						//
					},
					onFinish: function() {
						//
					}
				});
			}
		}
		_init();
		return this;
	};

})(jQuery);

/*
**  MAC Media
*/

;(function($) {

	$.fn.MacMedia = function() {

		var $this = $(this),
		$pp;

		// init
		function _init() 
		{
			//console.log('MacMedia :: init');
			if ($this.length)
			{
				_initPrettyPhoto();
			}
		}

		function _initPrettyPhoto()
		{
			$pp = $("a[rel^='prettyPhoto']").prettyPhoto({
						animation_speed: 'fast', /* fast/slow/normal */
						slideshow: 5000, /* false OR interval time in ms */
						autoplay_slideshow: false, /* true/false */
						opacity: 0.80, /* Value between 0 and 1 */
						show_title: true, /* true/false */
						allow_resize: true, /* Resize the photos bigger than viewport. true/false */
						default_width: 700,
						default_height: 600,
						counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
						theme: 'pp_default', /* pp_default / light_rounded / dark_rounded / light_square / dark_square / facebook */
						horizontal_padding: 20, /* The padding on each side of the picture */
						hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
						wmode: 'opaque', /* Set the flash wmode attribute */
						autoplay: true, /* Automatically start videos: True/False */
						modal: false, /* If set to true, only the close button will close the window */
						deeplinking: false, /* Allow prettyPhoto to update the url to enable deeplinking. */
						overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
						keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
						changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
						callback: function(){}, /* Called when prettyPhoto is closed */
						ie6_fallback: true,
						markup: '<div class="pp_pic_holder"> \
									<div class="ppt">&nbsp;</div> \
									<div class="pp_top"> \
										<div class="pp_left"></div> \
										<div class="pp_middle"></div> \
										<div class="pp_right"></div> \
									</div> \
									<div class="pp_content_container"> \
										<div class="pp_left"> \
										<div class="pp_right"> \
											<div class="pp_content"> \
												<div class="pp_loaderIcon"></div> \
												<div class="pp_fade"> \
													<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
													<div class="pp_hoverContainer"> \
														<a class="pp_next" href="#">next</a> \
														<a class="pp_previous" href="#">previous</a> \
													</div> \
													<div id="pp_full_res"></div> \
													<div class="pp_details"> \
														<div class="pp_nav"> \
															<a href="#" class="pp_arrow_previous">Previous</a> \
															<p class="currentTextHolder">0/0</p> \
															<a href="#" class="pp_arrow_next">Next</a> \
														</div> \
														<p class="pp_description"></p> \
														{pp_social} \
														<a class="pp_close" href="#">Close</a> \
													</div> \
												</div> \
											</div> \
										</div> \
										</div> \
									</div> \
									<div class="pp_bottom"> \
										<div class="pp_left"></div> \
										<div class="pp_middle"></div> \
										<div class="pp_right"></div> \
									</div> \
								</div> \
								<div class="pp_overlay"></div>',
						gallery_markup: '<div class="pp_gallery"> \
											<a href="#" class="pp_arrow_previous">Previous</a> \
											<div> \
												<ul> \
													{gallery} \
												</ul> \
											</div> \
											<a href="#" class="pp_arrow_next">Next</a> \
										</div>',
						image_markup: '<img id="fullResImage" src="{path}" />',
						flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
						quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
						iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
						inline_markup: '<div class="pp_inline">{content}</div>',
						custom_markup: '',
						social_tools: false//'<div class="pp_social"><div class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="facebook"><iframe src="http://www.facebook.com/plugins/like.php?locale=en_US&href='+location.href+'&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:23px;" allowTransparency="true"></iframe></div></div>' /* html or false to disable */
					});
		}

		_init();

		return this;
	};

})(jQuery);
