if(!$.browser.msie) {
    Cufon.set('fontFamily', 'TradeGothic').replace('#menuCol h3')('h1');
    Cufon.set('fontFamily', 'ChollaSlab').replace('h2.title')('h3.title')('h3.result');
}


$(document).ready(function() {
    if($.browser.msie && $('#disableCufon').length < 1) {
        Cufon.set('fontFamily', 'TradeGothic').replace('#menuCol h3')('h1');
        Cufon.set('fontFamily', 'ChollaSlab').replace('h2.title')('h3.title')('h3.result');
    }

	if(document.getElementById('searchResults')) {
		var sr = new searchResults();
	}
	if(document.getElementById('homeArea')) {
		var ha = new homeArea();
		recentGames();
	}
	else if(document.getElementById('mainArea')) {
		recentGames();
		var bm = new barMenu();
	}
	if(document.getElementById('tabTeaser')) {
		var tt = new tabTeaser();
	}
	if(document.getElementById('gameTeaser') || document.getElementById('tt_schedule')) {
		var gt = new gameTeaser();
	}
	if(document.getElementById('gameSchedule')) {
		var gs = new gameSchedule();
	}
	if(document.getElementById('slot')) {
		var sl = new slot();
	}
	
	
	if($.browser.msie && $.browser.version < 7) {
		if(document.getElementById('gameSum')) {
			gameSum();
		}
		if(document.getElementById('grid')) {
			gridHover();
		}
	}
	
    // Temporary Javascript for Debugging
    if(window.location.href.substr(7,9)=="localhost")
    {
        $('img').each(function(index) {
            var src = $(this).attr('src');
            $(this).attr('src','http://174.121.28.98'+src);
        });
    }
	
});

function searchResults() {
	var searchResults = $('#searchResults');
	var globalSearch = $('#globalSearch');
	
	
    function setTrigger() {
        if(globalSearch.val().length > 0 && !globalSearch.hasClass('defaultValue')) {
            $('#triggerSearch').val(globalSearch.val()).addClass('userValue');
        }
        else {
            $('#triggerSearch').val('Search').removeClass('userValue');
        }
    }
	function closeCompact() {
		if(searchResults.hasClass('compactSearch')) {
		    setTrigger();
			searchResults.hide();
			//$('body').unbind();
		}
	}
	function exeSearch() {
	    var SearchText = globalSearch.val().trim();
	    if(SearchText.length > 0 && !globalSearch.hasClass('defaultValue')) {
	        // Clears the searchbox of the current searchs
			//$('body').unbind();
	        globalSearch.val('Search').removeClass('userValue').addClass('defaultValue');
		    searchResults.removeClass('compactSearch').addClass('fullSearch');
		    btnGo_Click(SearchText, $('#globalSearch_lang').val());
		}
	}
	
	$('#triggerSearch').focus(function() {
	    searchResults.removeClass('fullSearch').addClass('compactSearch').show();
	    globalSearch.focus().select();
	}).click(function(e) {
		e.stopPropagation();
    });
    $('body').click(function() {
        closeCompact();
    });
	
	globalSearch.defaultVal('Search').keypress(function(e) {
		if(e.which == 13) {
			e.preventDefault();
			exeSearch();
			globalSearch.blur();
		}
	}).click(function(e) {
		e.stopPropagation();
	});
	
	$('#globalSearch_go').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		exeSearch();
	});
	
	$('#globalSearch_close').click(function(e) {
		e.preventDefault();
		$('#searchResults .paging').html('');
		$('#searchResults .results').html('');
		setTrigger();
		searchResults.hide();
	});
}

function homeArea() {
	
	var status = 'def';
	var anim = false;
	var timer = null;
	var hover = '';
	var s = {
	    canvas:300,
	    col:600,
	    delay:350,
	    barUp:'easeInCirc',
	    barDown:'easeOutCirc',
	    slideOpen:'easeInOutQuad',
	    slideClosed:'easeInOutQuad'
	    };
	var mensBar = $('#mensBar');
	var womensBar = $('#womensBar');
	var mensCanvas = $('#mensCanvas');
	var womensCanvas = $('#womensCanvas');
	var mensCol = $('#mensCol');
	var womensCol = $('#womensCol');
	
	function defToMens() {
		anim = true;
		status = 'mens';
		mensBar.stop();
		mensCol.stop();
		womensCanvas.stop().animate({ top: -684 }, s.canvas, s.barUp, function() {
			womensBar.addClass('barUp');
			mensBar.animate({ left: 0 }, s.col, s.slideOpen);
			mensCol.animate({ left: 136 }, s.col, s.slideOpen, function() {
				anim = false;
				if(hover == 'womens') {
					mensToWomens();
				}
			});
		});
	}
	function mensToDef() {
		anim = true;
		status = 'def';
		mensBar.stop().animate({ left: 544 }, s.col, s.slideClosed);
		mensCol.stop().animate({ left: 680 }, s.col, s.slideClosed, function() {
			womensBar.removeClass('barUp');
			womensCanvas.animate({ top: 0 }, s.canvas, s.barDown), function() {
				anim = false;
			};
		});
	}
	function defToWomens() {
		anim = true;
		status = 'womens';
		mensBar.stop();
		womensCol.stop();
		mensCanvas.stop().animate({ top: -684 }, s.canvas, s.barUp, function() {
			mensBar.addClass('barUp');
			mensBar.animate({ left: 0 }, s.col, s.slideOpen);
			womensCol.animate({ left: 0 }, s.col, s.slideOpen, function() {
				anim = false;
				if(hover == 'mens') {
					womensToMens();
				}
			});
		});
	}
	function womensToDef() {
		anim = true;
		status = 'def';
		mensBar.stop().animate({ left: 544 }, s.col, s.slideClosed);
		womensCol.stop().animate({ left: 544 }, s.col, s.slideClosed, function() {
			mensBar.removeClass('barUp');
			mensCanvas.animate({ top: 0 }, s.canvas, s.barDown, function() {
				anim = false;
			});
		});
	}
	function womensToMens() {
		anim = true;
		status = '';
		mensBar.removeClass('barUp');
		mensCanvas.animate({ top: 0 }, s.canvas, s.barDown, function() {
			mensCol.css({ left: -544 }).animate({ left: 136 }, s.col, s.slideOpen, function() {
				womensCanvas.animate({ top: -684 }, s.canvas, s.barUp, function() {
				womensBar.addClass('barUp');
					womensCol.css({ left: 544 });
					anim = false;
					status = 'mens';
					if(hover == 'def') {
						mensToDef();
					}
					else if(hover == 'womens') {
						mensToWomens();
					}
				});
			});
		});
	}
	function mensToWomens() {
		anim = true;
		status = '';
		womensBar.removeClass('barUp');
		womensCanvas.animate({ top: 0 }, s.canvas, s.barDown, function() {
			mensCol.css({ zIndex: 19 });
			womensCol.css({ left: 680 }).animate({ left: 0 }, s.col, s.slideOpen, function() {
				mensCanvas.animate({ top: -684 }, s.canvas, s.barUp, function() {
					mensBar.addClass('barUp');
					mensCol.css({ zIndex: 21, left: 680 });
					anim = false;
					status = 'womens';
					if(hover == 'def') {
						womensToDef();
					}
					else if(hover == 'mens') {
						womensToMens();
					}
				});
			});
		});
	}

	$('#menuCol').hover(function() {
		hover = 'def';
		clearTimeout(timer);
		timer = null;
		if(status == 'mens') {
			timer = setTimeout(function() { mensToDef(); }, s.delay);
		}
		else if(status == 'womens') {
			timer = setTimeout(function() { womensToDef(); }, s.delay);
		}
	}, function() {
		clearTimeout(timer);
		timer = null;
		hover = '';
	});
	mensBar.hover(function() {
		hover = 'mens';
		clearTimeout(timer);
		timer = null;
		if(status == 'def') {
			timer = setTimeout(function() { defToMens(); }, s.delay);
		}
		else if(!anim && status == 'womens') {
			timer = setTimeout(function() { womensToMens(); }, s.delay);
		}
	}, function() {
		clearTimeout(timer);
		timer = null;
		hover = '';
	});
	womensBar.hover(function() {
		hover = 'womens';
		clearTimeout(timer);
		timer = null;
		if(status == 'def') {
			timer = setTimeout(function() { defToWomens(); }, s.delay);
		}
		else if(!anim && status == 'mens') {
			timer = setTimeout(function() { mensToWomens(); }, s.delay);
		}
	}, function() {
		clearTimeout(timer);
		timer = null;
		hover = '';
	});

	
}

function gameSum() {
	$('div.gameSum').hover(function() {
		$(this).addClass('hover');
	},
	function() {
		$(this).removeClass('hover');
	});
}
function menuBar() {
	$('#mainArea div.barMenu').hover(function() {
		$(this).addClass('hover');
	},
	function() {
		$(this).removeClass('hover');
	});
}
function gridHover() {
	$('#grid li.image a').hover(function() {
		$(this).addClass('hover');
	},
	function() {
		$(this).removeClass('hover');
	});
}
function recentGames() {
	$('div.recentGames li').each(function() {
		$(this).addClass('fade');
	}).hover(function() {
		$(this).removeClass('fade');
	},function() {
		$(this).addClass('fade');
	});
}

function tabTeaser() {
	var currentId = $('#tabTeaser a.selected').attr('href');
	var startActive = false;
	if(currentId && currentId.length > 1) {
	    startActive = true;
	}
	else {
	    currentId = null;
	}
	
	$('#tabTeaser a').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		var newId = $(this).attr('href');
		if((currentId && currentId.length > 1 && newId != currentId) || !startActive) {
		    $('#tabTeaser a').removeClass('selected');
		    $(currentId).removeClass('active').unbind();
		}
		if(!startActive && !currentId) {
		}
		if(newId != currentId) {
			currentId = newId;
			$(currentId).addClass('active').click(function(e) { e.stopPropagation(); });
			$(this).addClass('selected');
		}
		else if(!startActive) {
			currentId = null;
		    //$('body').unbind();
		}
	}).each(function() {
	    var padding = (64 - $(this).height()) / 2;
	    $(this).css({ paddingTop: padding, paddingBottom: padding });
	});
    $('body').click(function() {
        $('#tabTeaser a').removeClass('selected');
        $(currentId).removeClass('active').unbind();
	    currentId = null;
        //$('body').unbind();
    });
}

function gameTeaser() {
	var current = $('#gameTeaser li.selected a').attr('href');
	
	$('#gameTeaser a').click(function(e) {
		e.preventDefault();
		$('#gameTeaser li').removeClass('selected');
		$(current).removeClass('active');
		current = $(this).parents('li').addClass('selected').end().attr('href');
		$(current).addClass('active');
	});
}

function gameSchedule() {
	$('body').append('<div id="gsDisplay" class="gameSum"></div');
	
	$('#gameSchedule a.gsFlyout, #tt_schedule a.gsFlyout').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		var $this = $(this);
		var pos = $this.offset();
		$('#gsDisplay').html($this.next().html()).css({
			display: 'block',
			left: pos.left + 50,
			top: pos.top
		});
		Cufon.replace('#gsDisplay h3.result',{fontFamily: 'ChollaSlab'});
	});
	$('body, #tabTeaser a, #triggerSearch, div.tabTeaser div.teaser').click(function() {
		$('#gsDisplay').hide();
	});
}

function slot() {
	var lis = $('#slot li');
	var ceiling = lis.length;
	if(ceiling < 2) {
		lis.eq(0).css('top',0);
		return;
	}
	var current = 0;
	var timer = null;
	
	function moveUp() {
		var next = current + 1;
		if(next == ceiling) { next = 0; }
		lis.eq(current).animate({ top: -120 }, 400, 'easeInOutSine', function() { $(this).css('top',120); });
		lis.eq(next).animate({ top: 0 }, 400, 'easeInOutSine');
		current = next;
	}
	$('#slot').hover(function() {
		clearInterval(timer);
		timer = null;
	},
	function() {
		timer = setInterval(function() { moveUp(); }, 4000);
	});
	
	lis.eq(0).css('top',0);
	timer = setInterval(function() { moveUp(); }, 4000);
}

function barMenu() {
    var bar = $('#mainArea div.barUp');
    var menu = bar.find('div.menu');
    var height = menu.find('ul').height() + 76;
    menu.css('display','none');
    bar.hover(function() {
        menu.stop().css('display','block').animate({ height: height }, 400, 'easeOutSine');
    },function() {
        menu.stop().animate({ height: 0 }, 300, 'easeOutSine', function() { menu.css('display','none'); });
    });
}
