//setup cufon

Cufon.replace('#header #top_nav > ul > li > a, #browse_menu li a, #sideNav li a');
Cufon.replace('#home .introBody, .home #ideas h2, #content .ideasRow .info h2 a, #idea .ideaBody h2, .register label, .intro h1, #why_page #description li.first, #ideaSubmit label:not(dd.checkbox label), p.hint, .social_links span');
Cufon.replace('.idea h4 a', { fontFamily: 'FoundryFormSansMed' });
Cufon.replace('.cf');
Cufon.replace('#slogan h1');
//Cufon.replace('a.see-all');

if ($.support.opacity) {
	Cufon.replace('.green h2:not(.nocf), a.button', { textShadow: '#538013 0px -1px' });
	Cufon.replace('.event a.button, .blue h2', { textShadow: '#005b83 0px -1px' });
	Cufon.replace('.black h2', { textShadow: '#000000 0px -1px' });
	Cufon.replace('a.b_button', { textShadow: '#538013 0px -1px' });
} else {
	Cufon.replace('.green h2:not(.nocf), a.button');
	Cufon.replace('.event a.button, .blue h2');
	Cufon.replace('.black h2');
	Cufon.replace('a.b_button');
};




//fix the main nav rolloffs
$(function() {
	$('#header #top_nav > ul > li').hover(
		function() {
			Cufon.refresh('#header #top_nav > ul > li > a');
		},
		function() {
			Cufon.refresh('#header #top_nav > ul > li > a');
		}
	);
});

//enable the toggle labels in login rollover
$(function() {
	$('#header #login_inline p.input').click(function() {
		$(this).find('label').fadeOut();
		$(this).find('input').focus();
	})
	$('#header #login_inline p.input input').focus(function() {
		$(this).prev('label').fadeOut();
	});
	$('#header #login_inline p.input input').blur(function() {
		if (!$(this).val()) $(this).prev('label').fadeIn();
	});
	$('#header #login_inline p.input input').each(function() {
		if ($(this).val()) {
			$(this).prev('label').css('display', 'none');
		}
	});
	
	$('#search-form input').each(function() {
		if ($(this).val()) {
			$(this).prev('label').css('display', 'none');
		}
	});
	
	$('#header #register_inline p.input').click(function() {
		$(this).find('label').fadeOut();
		$(this).find('input').focus();
	})
	
	$('#search-form').click(function() {
		$(this).find('label').fadeOut();
		$(this).find('input').focus();
	})
	
	$('#search-form input').focus(function() {
		$(this).prev('label').fadeOut();
	})
	
	$('#search-form input').blur(function() {
		if (!$(this).val()) $(this).prev('label').fadeIn();
	});
	
	$('#header #register_inline p.input input').focus(function() {
		$(this).prev('label').fadeOut();
	});
	$('#header #register_inline p.input input').blur(function() {
		if (!$(this).val()) $(this).prev('label').fadeIn();
	});
	$('#header #register_inline p.input input').each(function() {
		if ($(this).val()) {
			$(this).prev('label').css('display', 'none');
		}
	});
});


//fix for IE pulldown menus
//http://richa.avasthi.name/blogs/tepumpkin/2008/01/11/ie7-lessons-learned/
// $(function() {zIndexWorkaround();});
// function zIndexWorkaround()
// {
// 	var counter = 1000;
//     // If the browser is IE,
//     if(isIE())
//     {
//         /*
//         ** For each div with class menu (i.e.,
//         ** the thing we want to be on top),
//         */
//         $("#browse_menu, #region_chooser > ul, #login_inline, #register_inline").parents().each(function() {
//             var p = $(this);
//             var pos = p.css("position");
// 
//             // If it's positioned,
//             if(pos == "relative" ||
//                pos == "absolute" ||
//                pos == "fixed")
//             {
// 				
//                 /*
//                 ** Add the "on-top" class name when the
//                 ** mouse is hovering over it,
//                 */
//                 p.mouseover(function() {
// 					counter++;
// 					$(this).css('z-index', counter);
//                 });
// 
//             }
//         });
//     }
// }

//check if the browser is IE
function isIE() {
	if(navigator.userAgent.match(/MSIE \d\.\d+/))
	return true;
	return false;
}

var height = document.documentElement.clientHeight;
var width = document.documentElement.clientWidth;

if(loggedIn == undefined) {
	var loggedIn = 0;
}

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

// main jQuery class
var Webex = function($) {
	
	return {
		init : function() {
			if($.browser.mozilla){
				$('body').addClass('firefox');


			  	var version = $.browser.version.substring(0,3);
				if(version == '1.8') {
					majorVersion = 2;
				} else if (version == '1.9') {
					majorVersion = 3;
				}

				$('body').addClass('firefox'+majorVersion);
			}
			
			if($.browser.safari){
				$('body').addClass('safari');
			}
			
			if ($('#flashMessenger').length > 0)
			{
				$('#flashMessenger').click(function()
				{
					$('#flashMessenger').fadeOut();
				});
			}
			
			$('#login .loginShow').live('click', function() {
				$('#login .loginForm').css({display: 'block', height: 0}).animate({height: 83});
				return false;
			});
			
			$('#login .loginHide').live('click', function() {
				$('#login .loginForm').animate({height: 0}, 200, 'linear', function() {
					$(this).css({display: 'none'});
				});
				return false;
			});
			
			//sorter
			$('#sort select').change(function() {
				var sort = $(this).val();
				var category = $('.categories').eq(0).attr('id');
				
				if(category != '') {
					window.location = '/ideas/category/slug/'+category+'/sort/'+sort;
				} else {
					window.location = '/ideas/rate/sort/'+sort;
				}	
			});
			
			$('#sortPromo select').change(function() {
				var sort = $(this).val();
				var promo = $('.categories').eq(0).attr('id');
				
				if(promo != '') {
					window.location = '/ideas/'+promo+'/sort/'+sort;
				} else {
					window.location = '/ideas/rate/sort/'+sort;
				}	
			});
			
			//region drop down
			$('#region_chooser').hover(function() {
				$('#region_chooser ul').show();
			}, function() {
				$('#region_chooser ul').hide();		
			});
		
			$('#content .thumb.idea').mouseenter(ideaHoverEvent);
			
			initLike();
			initShare();
			initRating();
		}, 
		p_idea : function() {
			initJoin();
			initFlag();
			initDeleteComments();
		}
	};
	
	function initLike() {
		$('a.like').live('click', function() {
			var button = $(this);
			if(!$(this).hasClass('disabled')) {
				var href = $(this).attr('href');

				var box = $(this).parent().parent().find('.likes');

				$.ajax({
					url: href, 
					dataType: 'json',
					beforeSend: function() {
						box.fadeOut();
					},
					success: function(json) {
						box.html(json.message).fadeIn();

						
						button.addClass('disabled').addClass('liked').removeClass('like');
						
						// if (!$('body').hasClass('jobtraining')) {
						// 	if(button.find('img').length > 0) {
						// 		button.find('img').attr('src', '/img/liked_icon_sm.png');
						// 	}				
						// }

						
						if(json.pulse == 1 && box.pulse) {
							box.pulse({
								backgroundColors: ['#cc5b35','#ba2f00'],
								speed: 500
							});				
						}
					},
					error: function() {
						box.fadeIn();
					}
				});
			}
			
			

			
			return false;
		});
		
		// do nothing when you "relike" something
		$('a.liked').live('click', function() {
			return false;
		})
	}
	
	function initJoin() {
		$('a.join').live('click', function() {
			var link = $(this);
			if(!link.hasClass('disabled')) {
				var href = link.attr('href');

				$.ajax({
					url: href, 
					dataType: 'json',
					success: function(json) {
						// $('#countTeam').text(json.message);
						link.after("<p style=\"margin-top: 20px;\">"+json.message+"</p>");
						// if(json.message != 'Error') {
						// 	$('#team').show();
						// 	if($('#team ul li').length > 0) {
						// 		$('#team ul li:first').before(json.avatar);
						// 	} else {
						// 		$('#team ul').html(json.avatar);
						// 	}
						// 
						// 	$(this).addClass('disabled');		
						// }
					}
				});
			}
			
			return false;
		});
	}
	
	function initRating() {
		$('.rating').stars({
			oneVoteOnly: true,
			disabled: $(this).hasClass('disabled'),
			callback: function(ui, type, value) {
				var ratingId = $(ui.element).attr('id').split('-')[2];
				$.ajax({
					url: '/ideas/judge',
					data: {
						id: ratingId,
						rating: value
					},
					dataType: 'json',
					success: function(json) {
						ui.element.parent().find('.likes').html(json.message);
					}
				});
			}
		});
		
	}
	
	function initFlag() {
		$('a.flag').live('click', function() {
			var link = $(this);
			if(!link.hasClass('disabled')) {
				var href = link.attr('href');

				$.ajax({
					url: href, 
					dataType: 'text',
					success: function(data) {
						link.text(data);

						$(this).addClass('disabled');	
					}
				});
			}
			
			return false;
		});
	}
	
	function initShare() {
		$('a.share').click(function() {
			var href = $(this).attr('href');
			var title = $(this).attr('title');
			return addthis_open(this, '', href, title);
		});
	}
	
	function initDeleteComments() {
		$('a.deleteComment').click(function() {
			var href = $(this).attr('href');
			var id = $(this).attr('id').split('_')[1];
			var commentBox = $('#comment_'+id);
			$.ajax({
				url: href, 
				dataType: 'json',
				success: function(json) {
					if(json.success == 'TRUE') {
						commentBox.fadeOut();
					}
				}
			});
			
			return false;
		})
	}
	
}(jQuery);

var ideaHoverEvent = function(e) {
	e.stopPropagation();
	var className = 'right';
	
	var bigSize = $(this).parents('#editors_picks').length > 0 ? true : false;
	
	var ideaId = $(this).attr('id');
	
	if($(this).find('#follower').length> 0) {
		return false;
	}

	$('.idea').css('z-index', 0).removeClass('hover');
	$("#follower").fadeOut().remove();
	
	if (bigSize) {
		var offsetY = -20;
		var offsetX = -20;
	} else {
		var offsetY = -10;
		var offsetX = -10;
	}


	$(this).find('.popup').after('<div id="follower"></div>');
	
	$(this).css('z-index', '1000').addClass('hover');
	
	var popup = $('#'+ideaId+ ' .popup').clone();
	
	var position = $(this).offset();
	
	if((position.left + 134) > (width / 2)) {	
		if (bigSize) {	
			offsetX = -240;
		} else {
			offsetX = -150;
		}

		className = 'left';
	}
	
	$("#follower").css({
        top: (offsetY) + "px",
        left: (offsetX) + "px",
		zIndex: -1
    }).attr('class', className);


	$('#follower').html(popup).fadeIn();

	// 
	// $('#follower .popup p').mouseenter(function() {
	// 	$(this).wait(500).queue(function() {
	// 		$('#follower').hide();
	// 	});
	// });
	// 
	$("#follower").mouseleave(function() {
		$("#follower").fadeOut().remove();
		$('#'+ideaId).removeClass('hover');
	});
}

$(document).ready(Webex.init);