/**
 *
 * Esquire
 * Author: Stefan Petre www.eyecon.ro
 *
 * Copyright Stefan Petre
 *
 */

(function($){
	EYE.register (
		function () {
			Cufon.replace('#mainMenu a.mainItem, #header form button, '+
			'div.articlesList h2, div.articlesList h1[class!=label], #subscribeForm h4' +
			', .sideBox h4');
			Cufon.replace('#currentIssue a, #contactMenu a', {hover: true});
			if (document.getElementById('featured')) {
				Cufon.replace('#featured h2');
			}
			if (document.getElementById('issuesList')) {
				Cufon.replace('#issuesList .issueSummary h2');
			}
		}
	);
	EYE.slideshow = function () {
		var el, current, total, timer,
			go = function(to) {
				clearTimeout(timer);
				if (current) {
					$('#slideshowImages img').eq(current).animate({opacity: 0}, 800);
				}
				current = to;
				$('#slideshowImages img').eq(current).animate({opacity: 1}, 800);
				$('#slideshowNavigation li').removeClass('active').eq(current).addClass('active');
				setTimer();
			},
			setTimer = function() {
				timer = setTimeout(goNext, 5000);
			},
			goNext = function () {
				next = current + 1;
				if (next >= total) {
					next = 0;
				}
				go(next);
			},
			click = function(ev) {
				var targetEl = $(ev.target).closest('a');
				if (targetEl.size() == 1) {
					go($('#slideshowNavigation a').index(targetEl));
					targetEl.blur();
					return false;
				}
			},
			mouseMove = function (ev) {

			};
		return {
			init: function() {
				if ((el = document.getElementById('slideshow'))) {
					$("#slideshowFlashContainer").flash({
						src: '/es_header_4_4.swf',
						width: '960',
						height: '358',
						wmode: 'opaque',
						id: 'slideshowFlash',
						name: 'slideshowFlash',
						allowScriptAccess: 'sameDomain'
					},{
						update: false
					});
					if ($('#slideshowFlashContainer').is('.flash-replaced')) {
						$('#slideshowImages, #cover').hide();
					}
					else {
						$("#slideshowNavigation").bind('click', click);
						total = $('#slideshowImages img').css('opacity', 0).size();
						go(0);
						setTimer();
					}
				}
			}
		};
	}();
	EYE.register(EYE.slideshow.init);

	EYE.article = function(){
		var inited = false, imageSegments = 0, currentSegment = 0,
			photoClick = function() {
				currentSegment = Math.max(
					0,
					Math.min(
						imageSegments,
						currentSegment + (this.id == 'articlePhotosPrev' ? -4 : 4)
					)
				);
				$('#articlePhotosContent').stop().animate({left: - 140 * currentSegment}, 800);
				return false;
			},
			expandCollapse = function() {
				if ($(this).toggleClass('expanded').is('.expanded')) {
					$('#articleInfo').css({width: 620});
					$('#articleImage img').attr('width', 620);
					expanded = true;
				} else {
					$('#articleInfo').css({width: 293});
					$('#articleImage img').attr('width', 293);
					expanded = false;
				}
				this.blur();
				return false;
			};
		return {
			init: function() {
				if (inited == false && document.getElementById('article')) {
					inited = true;
					EYE.captcha.init();
					EYE.rating.init();
					EYE.chapters.init();
					Cufon.replace('#articleCategory');
					if (document.getElementById('articlePhotos')) {
						imageSegments = $('#articlePhotosContent a').fancybox().size() - 4;
						if (imageSegments > 0) {
							$('#articlePhotosNext, #articlePhotosPrev').show().bind('click', photoClick);
						}
					}
					$('#pictorialControl a')
						.bind('click', expandCollapse);
				}
			}
		};
	}();
	EYE.register(EYE.article.init);

	EYE.rating = function() {
		var click = function(ev) {
			if ($(ev.target).is('a')) {
				$('#articleRatingStars').hide();
				$('#ratingLoading').show();
				$.ajax({
					data: {
						plugin: 'rateArticle',
						rating: 5 - $('#articleRatingStars a').index(ev.target),
						article: $('#article, #pictorial').attr('rel')
					},
					type: 'POST',
					url: '/ajaxserver.php',
					complete: function(){
						$('#articleRatingStars').show();
						$('#ratingLoading').hide();
					},
					success: function(resp){
						var errors = $('errorResponse', resp);
						if (errors.size() > 0 ) {
							EYE.error.show($('#articleRating').get(0), errors.text(), 3);
						} else {
							$('#articleRatingStars>span:first').css('width', 12*parseInt($('success', resp).text(), 10));
						}
					}
				});
				return false;
			}
		};
		return {
			init: function() {
				$('#articleRating').bind('click', click);
			}
		};
	}();

	EYE.pictorial = function () {
		var inited = false, current = -1, maxImages, expanded = false,
			timer,
			goTo = function (newCurrent) {
				if (newCurrent == current) {
					return;
				}
				if (expanded) {
					$('#pictorialWrapper img')
						.eq(current).stop(true).css({opacity: 0}).end()
						.eq(newCurrent).stop(true).css({opacity: 1});
				} else {
					$('#pictorialWrapper img')
						.eq(current).stop(true).animate({opacity: 0}, 500).end()
						.eq(newCurrent).stop(true).animate({opacity: 1}, 500);
				}
				$('#pictorialNavigator select').val(newCurrent+1);
				$('#articleImages a')
					.filter('.active').removeClass('active').end()
					.eq(newCurrent).addClass('active');
				current  = newCurrent;
				$('#pictorialChapters div.pictorialChapter').hide().eq(current).show();
				$('#pictorialWrapper').focus();
			},
			click = function(ev) {
				if (this.id == 'articlePhotosPrev' || this.id == 'articlePhotosNext') {
					var newCurrent = Math.max(0, Math.min(maxImages - 1, current + (this.id == 'articlePhotosPrev' ? -1 : 1)));
					goTo(newCurrent);
					this.blur();
					$('#pictorialWrapper').focus();
					return false;
				} else if(this.id == 'pictorialWrapper') {
					var newCurrent = Math.max(0, Math.min(maxImages - 1, current + 1));
					goTo(newCurrent);
					return false;
				} else {
					var targetEl = $(ev.target).closest('a');
					$('#pictorialWrapper').focus();
					if (targetEl.size() == 1) {
						var newCurrent = $('#articleImages a').index(targetEl);
						goTo(newCurrent);
						targetEl.blur();
						return false;
					}
				}
			},
			change = function() {
				var newCurrent = Math.max(0, Math.min(maxImages - 1, this.value-1));
				goTo(newCurrent);
			},
			expandCollapse = function() {
				if ($(this).toggleClass('expanded').is('.expanded')) {
					$('#articleImages').hide();
					$('#pictorialPhotos').css({width: 620});
					$('#pictorialWrapper').css({width: 620, height: 882});
					expanded = true;
				} else {
					$('#pictorialWrapper').css({width: 379, height: 539});
					$('#articleImages').show();
					$('#pictorialPhotos').css({width: 379});
					expanded = false;
				}
				this.blur();
				$('#pictorialWrapper').focus();
				return false;
			},
			keyPress = function(ev) {
				var newCurrent = -1;
				switch(ev.keyCode) {
					//end
					case 35:
						newCurrent = maxImages-1;
						break;
					//home
					case 36:
						newCurrent = 0;
						break;
					//left
					case 37:
					case 38:
						newCurrent = Math.max(0, Math.min(maxImages - 1, current - 1));
						break;
					//right
					case 39:
					case 40:
						newCurrent = Math.max(0, Math.min(maxImages - 1, current + 1));
						break;
				}
				if (newCurrent > -1) {
					goTo(newCurrent);
					return false;
				}
			};
		return {
			init: function() {
				if (inited == false && document.getElementById('pictorial')) {
					inited = true;
					EYE.captcha.init();
					EYE.rating.init();
					EYE.chapters.init();
					Cufon.replace('#articleCategory');
					maxImages = $('#pictorialWrapper')
						.focus().bind('keypress', keyPress).bind('click', click)
						.find('img').css('opacity', 0).size();
					goTo(0);
					$('#articlePhotosPrev, #articlePhotosNext, #articleImages').show().bind('click', click);
					$('#pictorialNavigator select').bind('change', change);
					$('#pictorialControl a')
						.bind('click', expandCollapse);
				}
			}
		};
	}();
	EYE.register(EYE.pictorial.init);

	EYE.captcha = function() {
		var refreshCapt = function() {
			$(this).parent().find('img:first').get(0).src = '/includes/captcha/captcha.php?rand='+parseInt(Math.random()*10000, 10);
			return false;
		}
		return {
			init: function() {
				$('#captchaRefresh').bind('click', refreshCapt);
			}
		}
	}();

	EYE.chapters = function() {
		var current = 1, total;
		var change = function() {
			current = Math.max(1, Math.min(total,this.value));
			goTo();
		};
		var click = function() {
			current = Math.max(1, Math.min(current + (this.id == 'articleNext' ? 1: -1)));
			goTo();
			this.blur();
			return false;
		};
		var goTo = function(stop) {
			$('#articleChapters div.articleChapter').addClass('hidden')
				.eq(current - 1).removeClass('hidden');
			var offset = $('#article').offset();
			if (!stop) {
				$('html, body').animate({scrollTop: offset.top}, 1000);
			}
			$('#articleChapter select').val(current);
			if (current == 1) {
				$('#articlePrev').hide();
			} else {
				$('#articlePrev').show();
			}
			if (current == total) {
				$('#articleNext').hide();
			} else {
				$('#articleNext').show();
			}
		};
		return {
			init: function(){
				if (document.getElementById('articleChapter')) {
					total = $('#articleChapter select').bind('change', change).find('option').size();
					$('#articleNext, #articlePrev').bind('click', click);
					goTo(true);
				}
			}
		}
	}();

	EYE.issues = function () {
		var inited = false, items = 0, current = 0,
			click = function (ev) {
				var targetEl = $(ev.target).closest('a').get(0);
				if (targetEl) {
					if (targetEl.id == 'issuesCarouselLeft' || targetEl.id == 'issuesCarouselRight') {
						current = Math.max(0, Math.min(items, current + (targetEl.id == 'issuesCarouselLeft' ? -4 : 4)));
					}
					else {
						var index = $('#issuesCarouselWrapper a').index(targetEl);
						$('#issuesItems>div.issueItem').hide().eq(index).show();
						current = Math.max(0, Math.min(items, index - 1));
					}
					goTo();
					targetEl.blur();
					return false;
				}
			},
			goTo = function () {
				$('#issuesCarouselWrapper').stop().animate({left: - 120 * current}, 800);
			};
		return {
			init: function() {
				if (inited === false) {
					inited = true;
					if (document.getElementById('issuesCarousel')) {
						items = $('#issuesCarouselWrapper a').size()
						$('#issuesCarouselWrapper').css('width', items * 120);
						$('#issuesCarousel').bind('click', click);
						items -= 4;
						$('#issuesItems>div.issueItem').eq(0).show();
					}
				}
			}
		};
	}();
	EYE.register(EYE.issues.init);

	EYE.error = function() {
		var timer,
			hide = function() {
				$('#tooltipError').fadeOut(800, function(){
					$(this).css({
						top: -400,
						left: -400
					}).find('div').empty();
				});
			};
		return {
			show: function (el, message, delay) {
				var tooltip = $('#tooltipError')
								.find('div').text(message).end()
								.css({
									opacity: 0,
									display: 'block'
								});
				var offset = $(el).offset();
				tooltip.css({
					top: offset.top - tooltip.height() - 10,
					left: offset.left + el.offsetWidth/2 - tooltip.width()/2
				}).animate({opacity: 1},800);
				setTimeout(hide, delay*1000);
			}
		}
	}();
})(jQuery);