// Replace heads text with Cufon vectors
Cufon.replace('h1');
Cufon.replace('h2');

// Replace calendar text with Cufon vectors
Cufon.replace('.calendar .years');
Cufon.replace('.calendar li');

// Rich Menu
Cufon.replace('.cmp-rich-menu .main-text');

// Initialize
$(function() {
	// Scroller
	$('.scroll-bar').each(function() {
		// Check if content is long enough too allow scroll
		var items = $(this).parent().parent().find('ul');
		var scrollMax = $(this).parent().parent().find('.list').height();
		var scrollHeight = (items.height() - scrollMax);
		// FIXME hide if scrollHeight is to little
		//if(scrollHeight <= 0) $(this).parent().hide();
		scrollHeight +=28;
		// Add hover effect
		$(this).bind('mouseover', function() {
			$(this).addClass('hover');
		});
		$(this).bind('mouseleave', function() {
			$(this).removeClass('hover');
		});
		// Draggable
		$(this).draggable({
			containment: 'parent',
			axis:'y',
			drag:function(event, ui) {
				$(this).addClass('hover');
				var items = $(this).parent().parent().find('ul');
				var scrollMax = $(this).parent().parent().find('.list').height();
				var top = $(this).position().top;
				var scroll = top / scrollMax;
				var scrollHeight = (items.height() - scrollMax) + 28;				
				items.attr({ style:'top:' + -scroll * scrollHeight + "px" });
			},
			stop:function(event, ui) {
				$(this).removeClass('hover');
				var items = $(this).parent().parent().find('ul');
				var scrollMax = $(this).parent().parent().find('.list').height();
				var top = $(this).position().top;
				var scroll = top / scrollMax;
				var scrollHeight = (items.height() - scrollMax) + 28;
				items.attr({ style:'top:' + -scroll * scrollHeight + "px" });				
			}
		});
	});
	
	// SpriteButtons
	$('.sprite-button').each(function() {
		$(this).bind('mouseenter mouseleave', function() { $(this).toggleClass('sprite-button-hover'); });
		$(this).bind('mousedown mouseup', function() { $(this).toggleClass('sprite-button-active'); });
	});
	
	// Newsletter
	$('#consumer-newsletter-submit').bind('click', function() {
		$('#consumer-newsletter-teaser').fadeOut(400);
		$('#consumer-newsletter-input').fadeOut(400);
		$('#consumer-newsletter-submit').fadeOut(400);
		$('#consumer-newsletter-success').fadeIn(400);
		// Add to newsletter
		$('#consumer-newsletter').submit();
	});
	
	// Rich Menu
	$('.cmp-rich-menu li').each(function() {
		$(this).bind('mouseenter mouseleave', function() {
			// Mouse-Leave
			$(this).parent().find('li').each(function() {
				$(this).removeClass('current');
			});
			// Set/Disable Current status
			$(this).addClass('current');
			// Update Heads
			Cufon.replace('.cmp-rich-menu .main-text');
		});
		// Move Arrows
		$(this).bind('mouseenter', function() {
			$(this).find('.arrow').animate({ left:"280px", opacity:1 });
		});
		$(this).bind('mouseleave', function() {
			$(this).find('.arrow').animate({ left:"290px", opacity:0 });
		});
	});
	
	// Activate first entry in Rich-Menu
	var first = $('.cmp-rich-menu li:first');
	first.addClass('current');
	Cufon.replace('.cmp-rich-menu .main-text');
	
	// Feedback-Email
	$('#send-feedback-form').bind('click', function() {		
		$.ajax({
			type:'POST',
			url:baseUrl + '/send-feedback.php',
			data:{
				name:escape($('form:feedback-form [name=name]').val()),
				mail:escape($('form:feedback-form [name=mail]').val()),
				message:escape($('#message').val())
			},
			success:function(data) {
				// TODO Insert sent ok
				$('.feedback-form').fadeOut('slow', function() {
					$('.feedback-form-response').fadeIn('slow');
				});
			}
		});
	});
	
	// Active-area on mouse-interaction
	$('.active-area').each(function() {
		$(this).bind('mouseover', function() {
			$(this).find('.icon').addClass('sprite-button-hover');
		});
		$(this).bind('mouseleave', function() {
			$(this).find('.icon').removeClass('sprite-button-hover');
		});
	});
	
	// Flags
	$('#flags').bind('mouseover', function() {
		// Show flags with timeout (show flags)
		$('#flags-container').delay(300).animate({ width:"81px" }, 400);
	});
	$('#flags').bind('mouseleave', function() {
		// Hide flags
		$('#flags-container').animate({ width:"25px" }, 400);
	});
	
	// Stars
	var artistStarHeight = '-32px';
	var modelsStarHeight = '-16px';
	var starNr;
	$('.stars').each(function() {
		$(this).attr('starNr', 1);
		$(this).find('.star').each(function() {
			var starNr = Number($(this).parent().attr('starNr'));
			$(this).attr('star', starNr++);
			$(this).parent().attr('starNr', starNr);
			$(this).bind('mouseover', function() {
				if($(this).parent().hasClass('artist')) {
					showStars($(this).parent(), Number($(this).attr('star')), artistStarHeight);
				} else if($(this).parent().hasClass('models')) {
					showStars($(this).parent(), Number($(this).attr('star')), modelsStarHeight);
				}
			});
			$(this).bind('mouseleave', function() {
				var maxStars = $(this).parent().attr('maxStars') || 0;
				if($(this).parent().hasClass('artist')) {
					hideStars($(this).parent(), maxStars, artistStarHeight);
				} else {
					hideStars($(this).parent(), maxStars, modelsStarHeight);
				}
			});
			$(this).bind('click', function() {
				var maxStars = Number($(this).attr('star'));
				$(this).parent().attr('maxStars', maxStars);
				var voteType;
				if($(this).parent().hasClass('artist')) {
					showStars($(this).parent(), maxStars, artistStarHeight);
					voteType = 'artist';
				} else {
					showStars($(this).parent(), maxStars, modelsStarHeight);
					voteType = 'model';
				}
				// Set votes
				$.ajax({
					type:'POST',
					url:baseUrl + '/page/member/vote',
					data:{
						type:voteType,
						setcode:$(this).parent().attr('setcode'),
						code:$(this).parent().attr('code'),
						vote:maxStars,
						artist:$(this).parent().attr('artist')
					}
				});
			});
			var maxStars = Number($(this).parent().attr('maxStars'));
			if($(this).parent().hasClass('artist')) {
				showStars($(this).parent(), maxStars, artistStarHeight);
			} else if($(this).parent().hasClass('models')) {
				showStars($(this).parent(), maxStars, modelsStarHeight);
			}
		});
	});
	
	// Overlay for covers if more than x covers watched and not a member
	$('#cmp-photo-details-set-overlay').each(function() {
		var isMember = false;
		var watchedCovers = 0;
		
		// Set background to opacity=0.75
		$(this).find('.bg').fadeTo(0, 0.75);
		
		// Read isMember cookie
		if(readCookie('userdata[isMember]')) isMember = true;
		if(readCookie('userdata[watchedCovers]')) watchedCovers = readCookie('userdata[watchedCovers]');
		
		watchedCovers++;
		setCookie('userdata[watchedCovers]', watchedCovers, 1000 * 60 * 60 * 24 * 30);
		
		if(watchedCovers > 5 && !isMember) $(this).fadeIn(2500);
	});
	
	// Clear f_username and f_password
	$('#f_usermail').bind('focus', function() {
		if($(this).val() == 'Your Email...') $(this).val('');
	});
	$('#f_usermail').bind('blur', function() {
		if($(this).val() == '') $(this).val('Your Email...');
	});
	$('#f_username').bind('focus', function() {
		if($(this).val() == 'Your Username...') $(this).val('');
	});
	$('#f_username').bind('blur', function() {
		if($(this).val() == '') $(this).val('Your Username...');
	});
	
	// Picture (in galleries)
	var picture = $('#picture');
	var pictureHeight = picture.height();
	
	var container = $('#picture-container');	
	container.height(pictureHeight);
	container.css('top', 60);
	var containerWidth = container.width();
	var containerHeight = container.height();
	
	$('#picture-options-bg').css({ opacity:0.65 });
	var options = $('#picture-options');
	var optionsActiveAnimation = false;
	
	$('#pull-menu').bind('mouseover', function() {
		if(!optionsActiveAnimation) {
			optionsActiveAnimation = true;
			options.animate({ top:60 }, 200, 'easeOutExpo', function() {
				optionsActiveAnimation = false;
			});
		}
	});
	$(this).bind('mousemove', function(e) {
		var x = e.clientX;
		var y = e.clientY;
		
		// FIXME sub-menus should stop close process if mouse is over
		var slideOutHeight = 155;
		if (y >= slideOutHeight && !optionsActiveAnimation) {
			optionsActiveAnimation = true;
			options.animate({ top:-5 }, 300, 'easeOutExpo', function() {
				optionsActiveAnimation = false;
			});
		}
	});

});

/**
 * Read a value from cookie
 * 
 * @param string cookieName
 * @return string The value for the cookie
 */
function readCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

/**
 * Set a cookie (name-value pair)
 * 
 * @param cookieName
 * @param value
 * @param lifetime Lifetime of cookie in ms
 * @return void
 */
function setCookie(cookieName, value, lifetime) {
	var a = new Date();
	a = new Date(a.getTime() + lifetime);
	document.cookie = cookieName + '=' + value + '; expires=' + a.toGMTString() + ';';
}

/**
 * Show Stars
 * 
 * @param Element starsNode
 * @param int starsNr
 * @param string starHeight
 * @return void
 */
function showStars(starsNode, starsNr, starHeight) {
	var starCount = 0;
	starsNode.find('.star').each(function() {		
		if(++starCount <= starsNr) {
			$(this).find('img').css('top', starHeight);
		} else {
			$(this).find('img').css('top', '0');
		}
	});
}

/**
 * Hide Stars
 * 
 * @param Element starsNode
 * @param int starsNr
 * @return void
 */
function hideStars(starsNode, starsNr, starHeight) {
	var starCount = 0;
	starsNode.find('.star').each(function() {		
		if(starCount >= starsNr) {
			$(this).find('img').css('top', '0');
		} else {
			$(this).find('img').css('top', starHeight);
		}
		starCount++;
	});
}

/**
 * Show the login window
 * 
 * @param string redir Is optional, if set, change the hidden field "return"
 * @return void
 */
function showLoginWindow(redir) {
	$("#modal-window").modal({opacity:80, onOpen: function (dialog) {
		dialog.overlay.fadeIn(150, function () {
			dialog.container.slideDown(100, function () {
				dialog.data.fadeIn(100, function() {
					$('#username').focus();
				});
			});
		});
	}});
	// Redir
	if(arguments.length == 1) {
		$('#return').val(redir);
	}
	// Refresh Header
	Cufon.refresh();
}

/**
 * Goto Page
 * 
 * @param	int pageNr
 * @return	void
 */
function gotoPage(pageNr) {
	// Hide pages (except the one with pageNr)
	$('.gallery-thumbs-page').each(function() {
		if(('page_' + pageNr) != $(this).attr('id')) {
			$(this).hide();
		}
	});
	// Show Page
	$('#page_' + pageNr).show('slow');
}

/**
 * Next Page
 * 
 * @return void
 */
function nextPage() {
	if(currentPage < maxPages) {
		gotoPage(currentPage++);
	}
}

/**
 * Show all pages together
 * 
 * @return void
 */
function showAllPages() {
	// Show all pages
	$('.gallery-thumbs-page').each(function() {
		$(this).show();
	});
}

/**
 * Scroll to comments
 * 
 * @return void
 */
function scrollToComments() {
	$(window).scrollTo({ top:'100%', left:0 }, 450);
}