jQuery(document).ready(function($) {

	// transfer thumbnail img src to mainimg
	$('.gallery_thumbs li a').bind('click', function() {
		$('.gallery_thumbs a.active').removeClass('active');
		$(this).addClass('active');
		$('.gallery_mainimg img').attr('src', $(this).attr('href')).attr('alt', $(this).find('img').attr('alt'));
		$('.gallery_mainimg p').html($(this).find('img').attr('alt'));
		return false;
	});
	
	/*
	if ($('.gallery_thumbs > a').length > 1) {
		$('.gallery_thumbs div.left_arrow a').bind('click', function() {
			$('.gallery_thumbs a.active').prev('a').click();
			return false;
		});
		$('.gallery_thumbs div.right_arrow a').bind('click', function() {
			$('.gallery_thumbs a.active').next('a').click();
			return false;
		});
	} else {
		$('.gallery_thumbs div.left_arrow a, .gallery_thumbs div.right_arrow a').hide();
	}
	*/

	// are there more than 3 thumbnails?
	if ($('.gallery_thumbs').find('ul').children().length > 3) {
		// show controls
		$('.gallery_thumbs div.left_arrow, .gallery_thumbs div.right_arrow').show();
	} else {
		$('.gallery_thumbs div.left_arrow, .gallery_thumbs div.right_arrow').hide();
	}
	
	//search placeholder text//
	var defaultSearchText = $('#search_input').val();
	$('#search_input').focus(function() { 
		if($('#search_input').val() == defaultSearchText)  $('#search_input').val("");
	});  
	$('#search_input').blur(function() {   
		if($('#search_input').val() == "" )  $('#search_input').val( defaultSearchText )
	});  

	//Character Carousel//
	if ($('#characters').find('ul').children().length > 7) {
		$('#characters').slider({width: 735});
		$('a[href*="#left"]').click(function() { $('#characters').slider('right'); return false; });
		$('a[href*="#right"]').click(function() { $('#characters').slider('left'); return false; });
	} else {
		$('.nav_left, .nav_right').hide();
	}

	//Featured Section slider//
	setInterval(function() {
		$('#featured-banner .featured-inner').animate({top: '-=' + $('#featured-banner .featured-inner section:first').outerHeight()}, function() {
			$('#featured-banner .featured-inner').append($('#featured-banner .featured-inner section:first')).css({top: 0});
		});
	}, 5000);


	//Modal//
	$( ".games_promo" ).live("click", function() {
		
		var url = $(this).find(".title").attr("href");
		var iframe_container = $('#game-iframe');
		
		iframe_container.find("iframe").attr("src", url);
		
		iframe_container.dialog({
			width: 'auto',
			height: 'auto',
			modal: true,
			minWidth: 700,
			minHeight: 500
		});
		return false;
	});

	$( ".play_results .search_results_item" ).live("click", function() {
		
		var url = $(this).find("a").attr("href");
		var iframe_container = $('#game-iframe');
		
		iframe_container.find("iframe").attr("src", url);
		
		iframe_container.dialog({
			width: 'auto',
			height: 'auto',
			modal: true,
			minWidth: 700,
			minHeight: 500
		});
		return false;
	});

    /* Carousels */
    jQuery(".carousel").each(function() {
        var section = jQuery(this);
        var isVertical = section.hasClass("carousel-vertical");
        var sectionWrapper = section.find(".carousel-wrapper");
        var sectionContent = section.find(".carousel-content");
        var sectionItems = sectionContent.find(".carousel-item");
        var scrollPrev = section.find(".carousel-prev").show();
        var scrollNext = section.find(".carousel-next").show();
        var itemIncrement = isVertical ? sectionItems.eq(0).outerHeight() : sectionItems.eq(0).outerWidth();
        var index = 0;
        var max = Math.round((isVertical ? sectionWrapper.outerHeight() : sectionWrapper.outerWidth()) / itemIncrement);
        var skip = section.hasClass("carousel-skip") ? max : 1;
        var total = sectionItems.length;
        var scrolling = false;

        var highlightButtons = function() {
            if (max >= total) {
                scrollPrev.addClass("disabled");
                scrollNext.addClass("disabled");
            } else if (index <= 0) {
                scrollPrev.addClass("disabled");
                scrollNext.removeClass("disabled");
            } else if (index + max >= total) {
                scrollPrev.removeClass("disabled");
                scrollNext.addClass("disabled");
            } else {
                scrollPrev.removeClass("disabled");
                scrollNext.removeClass("disabled");
            }
        };

        var prev = function(direction) {
            if (!scrolling && index > 0) {
                scrolling = true;
                index -= skip;
                highlightButtons();
                var css = {};
                css[direction] = "+=" + (itemIncrement * skip);
                sectionContent.animate(css, 200, "swing", function() {
                    scrolling = false;
                });
            }
            return false;
        };

        var next = function(direction) {
            if (!scrolling && index + max < total) {
                scrolling = true;
                index += skip;
                highlightButtons();
                var css = {};
                css[direction] = "-=" + (itemIncrement * skip);
                sectionContent.animate(css, 200, "swing", function() {
                    scrolling = false;
                });
            }
            return false;
        };

        section.bind("click", function(e) {
            var source = jQuery(e.target);
            if (isVertical && source.hasClass("carousel-prev")) {
                return prev("top");
            } else if (isVertical && source.hasClass("carousel-next")) {
                return next("top");
            } else if (!isVertical && source.hasClass("carousel-prev")) {
                return prev("left");
            } else if (!isVertical && source.hasClass("carousel-next")) {
                return next("left");
            }
        });

        highlightButtons();
    });

});
