$(document).ready(function() {

    generateShuffledList();
    // Change the image immediately
    changePlug();
    // Set timer to change picture every x milliseconds
    var imageSwapperTimer = setInterval("changePlug()", 10000);

    $("#featureCarousel").featureCarousel({
        // put all options here
        largeFeatureWidth:290,
        largeFeatureHeight:230,
        smallFeatureWidth:290,
        smallFeatureHeight:230,
        topPadding:105,
        smallFeatureOffset:0,
        autoPlay:10000,
        preload:false,
        counterStyle:2,
        startingFeature:1,
        displayCutoff:8,
        carouselSpeed: 750,
        animationEasing: 'swing'
    });

    // When someone clicks a link on the top navigation bar, toggle the div down
    // and prevent the link from firing (if JS is disabled, it will follow the link
    // as it should)
    $("#navigation a").click(function (event) {
        event.preventDefault();
        var div_id = $(this).attr('id').substring(5);
        toggleDiv(div_id);
        return false;
    });

    // When the close link is clicked within the navigation div's. It will toggle the div
    // But if JS is disabled it will just follow the link as it should
    $(".close a").live("click", function (event) {
        event.preventDefault();
        var div_id = $(this).attr('id').substring(6);
        toggleDiv(div_id);
        return false;
    });

});

var $shuffledList;
var shuffleCounter = 0;
function generateShuffledList() {
    var $plugs = $('#allPlugs').children('a');

    // Shuffle the list of all plugs and save in
    for (var n = 0; n < $plugs.length; n++) {
        // pick random item to move to end of array
        var random = Math.floor(Math.random() * $plugs.length);
        // Swap the two items
        var $temp = $plugs[n];
        $plugs[n] = $plugs[random];
        $plugs[random] = $temp;
    }

    $shuffledList = $plugs;
}

function changePlug() {
    // Get the new plug info
    var $newPlug = $shuffledList.eq(shuffleCounter);
    var newLink = $newPlug.attr('href');
    var newImage = $newPlug.find('img').attr('src');

    // Replace current plug info
    var $currentPlug = $('#currentPlug').children('a');
    $currentPlug.attr('href',newLink);
    $currentPlug.find('img').attr('src',newImage);

    // Wrap around plug index if reached end
    if (shuffleCounter == $shuffledList.length - 1) {
        shuffleCounter = -1;
    }

    shuffleCounter++;
}

function toggleDiv(div_id) {
    var exturl = 'home/categories/'+div_id+'.html';
    var $theDivToDisplay = $('#'+div_id);
    var $otherDivs = $('#categories').children('div').not($theDivToDisplay);

    if ($theDivToDisplay.css('display') == 'none') {
        $otherDivs.slideUp(function () {
            $theDivToDisplay.load(exturl, function () {
                $(this).slideDown();
            });
        });
    } else {
        $theDivToDisplay.slideUp();
    }
}

// study

$(document).ready(function() {
$("#chained").scrollable({circular: true, mousewheel: true}).navigator()
});


// biggerlink
$(function(){
	
		// Supporting target _blank
		$('a[target="_blank"]').click(function(){
			window.open(this.href);
			return false;
		});
		
		// Same using the valid rel attribute
		$('a[rel="external"]').click(function(){
			window.open(this.href);
			return false;
		});
		
		// Adding biggerlink
		$('#beginner ul').biggerlink();
		$('#lmap ul').biggerlink();
		$('#lgmap ul').biggerlink();
		$('#ultimateinner div').biggerlink();
		$('#mobile dl').biggerlink();
		$('#idxmovie dl').biggerlink();
	});
	
	
	// study link
	
	$(function(){
     $(".slink").click(function(){
         window.location=$(this).find("a").attr("href");
         return false;
    });
});

