var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).parents("li").find('.left-sub-outer').eq(0).css('display', 'block');
}

function jsddm_open_main() {
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).parents("li").find('.sub-outer').eq(0).css('display', 'block');
}

function jsddm_close() {
	if (ddmenuitem) {
		ddmenuitem.css('display', 'none');
	}
}

function jsddm_timer() {
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(function() {
	$("#fixtures-form").jqTransform();
});

$(function() {
	var zIndexNumber = 2000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
});

$(function() {
	$('.coming-soon-content a').click(function() {
		window.open(this.href);
		return false;
	});
});

$(document).ready(function() {
	var options = {
        insert : '#main_image',
        history : false,
        onImage : function(image,caption,thumb) {
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) {
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);
			var _li = thumb.parents('li');
			_li.siblings().children('img.selected').fadeTo(500,0.7);
			_li.siblings().children('img.selected').css({
				width: 81,
				height: 55
			})
			thumb.fadeTo('fast',1).addClass('selected');
			image.attr('title','Next image >>');
		},
		onThumb: function(thumb){
			var _li = thumb.parents('li');
			var _fadeTo = _li.is('.active') ? '1' : '0.7';
			thumb.css({
				display: 'none',
				opacity: _fadeTo
			}).fadeIn(1500);
			thumb.hover(function(){
				thumb.fadeTo('fast', 1);
				thumb.css({
					width: 96,
					height: 65
				})
			}, function(){
				_li.not('.active').children('img').fadeTo('fast', 0.7);
				_li.not('.active').children('img').css({
					width: 81,
					height: 55
				})
			});
		}
	};

	$("#show-login").live("click", function(e) {
		e.preventDefault();
		$("#login-button").hide("slow", function() {
	    	$("#search-form").hide("slow", function() {
				$("#search-button").show("slow", function() {
					$("#top-login-form").show("slow");
				});
			});
		});
	});

	$("#show-search").live("click", function(e) {
		e.preventDefault();
	    $("#top-login-form").hide("slow", function() {
			$("#search-button").hide("slow", function() {
				$("#search-form").show("slow", function() {
					$("#login-button").show("slow");
				});
			});
		});
	});

	$('.galleria').galleria(options);

	$("a[rel=gallery_group]").fancybox({
		overlayOpacity: 0.7
	});

	$('.fancy').fancybox({
		overlayOpacity: 0.7
	});

	$('.coming-soon, .coming-soon a').live("click", function(e) {
		e.preventDefault();
	});

	$('#left-inner a').hover(jsddm_open, jsddm_timer);
	$('#menu a').hover(jsddm_open_main, jsddm_timer);

	$(".reply-link a").live("click", function(e) {
	    e.preventDefault();
	    var comment_id = $(this).parent().parent().parent().find(".comment-id").get(0).innerHTML;
	    $("#new-comment").remove().appendTo($(this).parent().parent().parent().find(".new-reply"));
	    $("input[name=newc_comment_id]").val(comment_id);
	});

	$(".flag a").live("click", function(e) {
	    e.preventDefault();
	    $(this).parent().find("form").submit();
	});

	var comment_id = $("input[name=newc_comment_id]").val();
	if (comment_id != "") {
	    $("#comment"+comment_id).find(".reply-link a").trigger("click");
	}

	$("#user-bar .login").live("click", function(e) {
	    e.preventDefault();
	    $("#user-bar .user").hide();
	    $("#user-bar .login-form").show();
	});

	$("#new-comment .login-link a").live("click", function() {
	    $("#user-bar .user").hide();
	    $("#user-bar .login-form").show();
	});

	$('#category-select-outer ul').find('a').click(function(){
		window.location.replace('http://www.shekicks.net/fixtures-and-results/category/'+$(this).attr('index'));
	});

	$('#league-select-outer ul').find('a').click(function(){
		var currLocation = ""+window.location;
		var bits = currLocation.split("/");
		if (bits[6]) {
			window.location.replace('http://www.shekicks.net/fixtures-and-results/league/'+$(this).attr('index')+"/"+bits[6])
		} else {
			window.location.replace('http://www.shekicks.net/fixtures-and-results/league/'+$(this).attr('index'))
		}
	});

	$('#poll a').click(function(e){
		e.preventDefault();
		$.ajax({
			url: "http://www.shekicks.net/polls/cast_vote?option="+$(this).attr('index'),
			type: "GET",
			cache: false,
			success: function() {
				location.reload();
			}
		});
	});

	$(".open_external").attr("target", "_blank");
});
document.onclick = jsddm_close;
