(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);;
(function($){
// each ready function will be self contained to short-circuit if the requisite library is not loaded

// https://developers.facebook.com/apps
$(document).ready(function(){
	FB.init({
		appId  : '204482776271176',
		    status : true, // check login status
		    cookie : true, // enable cookies to allow the server to access the session
		    xfbml  : true  // parse XFBML
		    });
});

// five-star
$(document).ready(function(){
    if( $.fn.rating && $.fn.ajaxForm ){
	$('#user-input-star').ajaxForm(function() {
	    alert("Thank you for your vote!");
	});
	$('.user-input-star').rating({
	    callback: function(value, link){
		// 'this' is the hidden form element holding the current value
		// 'value' is the value selected
		// 'element' points to the link element that received the click.
		//  alert(Drupal.settings.ghProductNID);
		//		  alert(Drupal.settings.ghProductToken);
		//		  	    alert("The value selected was '" + value + "'\n\nWith this callback function I can automatically submit the form with this code:\nthis.form.submit();");
		// $.post( '/gh/vote', {value: object} )

		// To submit the form automatically:
		//this.form.submit();

		// To submit the form via ajax:
		$(this.form).ajaxSubmit();
	    }
	});
    }
});

// carousel initialization
$(document).ready(function(){
    if( ! $.fn.jCarouselLite ){
	return;
    }

    // get the carousels
    var carousels = $(".carousel");

    for( var i = 0 ; i < carousels.length ; i++ ){
	// the carousel
	var carousel = $(carousels[i]);
	// the things in the carousel
	var lis = carousel.find('li');
	// the navigation list of items
	var navlist = [];
	// the number of images shown by this carousel
	var num_images = carousel.attr('numimages');

	// jCarouselLite options
	var opts = {
	    // find the "next" button
	    btnNext: ".carousel_nav .next",
	    // find the "previous" button
	    btnPrev: ".carousel_nav .prev",
	    // Scroll to selected item
	    btnGo: navlist,
	    // circular? no.
	    circular: false,
	    // number of visible items
	    visible: parseInt(num_images,10),
	    // start index
	    // start: (carousel.parents('.article_picker').length > 0 ? 1 : 0)
	    start: 0
	};

	if( carousel.attr('carousel-autoscroll') != null ){
	    opts.auto = parseInt(carousel.attr('carousel-autoscroll'), 10);
	}

	if( carousel.attr('carousel-circular') === 'true' ){
	    opts.circular = true;
	}

	if( carousel.attr('carousel-speed') != null ){
	    opts.speed = parseInt(carousel.attr('carousel-speed'), 10);
	}

	// if there are too many images, make it a carousel
	if( lis.length > num_images ){
	    // the carousel nav element
	    var carousel_nav = carousel.siblings(".carousel_nav");
	    // unhide this
	    carousel_nav.removeClass("hidden");
	    // the next button, which we will be inserting things before
	    var next = carousel_nav.find('.next');

	    // generate the icons
	    for( var j = 0 ; j < lis.length + 1 - num_images ; j++ ){
		var navitem = $( document.createElement('a') )
			.attr("href","#")
			.addClass("navitem carousel_item_" + j);
		navlist.push( navitem );

		if( j == opts.start ){
		    navitem.addClass("active");
		}
		// attach the navigation item dot to the carousel element so we can "activate" it later
		$(lis[j]).attr('carousel-nav', j);
		// and add the navigation item
		next.before( navitem );
	    }
	    // halt event propagation
	    carousel_nav.children( 'a' ).click(function(){ return false; });

	    // after the carousel scrolls, this function will get a list of visible elements
	    opts.afterEnd = function( vis ){
		// strip out the last active element
		carousel_nav.children( 'a' ).removeClass( 'active' );
		// and activate the new associated one
		navlist[ $.attr(vis[0], 'carousel-nav') ].addClass('active');
	    };
	} else {
	    // insufficient images; apply .toofew
	    carousel.addClass('toofew');
	}

	// make this a carousel!
	carousel.jCarouselLite( opts );
    }
});

// Initialize product navigator(s)
$(document).ready(function(){
    // get all the navigators
    var navs = $('.plugin_product_navigator');

    // loop through
    navs.each(function(i,nav){
	// modify the tabs
	var anchors = $(nav).find('.pn_tabs a').each(function(i, a){
		$(a).click(function(){
		    // strip the active class from tabs and add to this one
		    anchors.removeClass('active');
		    $(this).addClass('active');
		    // find the related pane tag
		    var target = $(this).attr('href').replace('#','');
		    target = $(nav).find('.pn_gameset') //$('.pn_gameset', nav)
			// clear the currently active pane
			.removeClass('active')
			// get the target pane and apply active to it
			.filter('div[name="'+target+'"]').addClass('active');

		    // reset the See More Products link
		    $('.pn_seemore a', nav).attr('href', target.attr('href'));

		    // and return false to prevent the default action
		    return false;
		});
	    });

	    // activate the first one
	    if( anchors.length > 0 )
	    {
		$(anchors[0]).trigger('click');
	    }
	});

});

// initialize Lightboxes
$(document).ready(function(){
    if( $.fn.colorbox ){
	/*
	// colorbox settings
	var colorbox_settings = {
	    //	maxWidth: "600px",
	    //	maxHeight: "500px",
	    fixed: true,
	    photo: true,
	    innerWidth: "600px",
	    innerHeight: "500px",
	    scalePhotos: true
	};
	*/
	$('a.lightbox').colorbox({
	    initialWidth: '300px',
	    initialHeight: '300px'
	});
    }
});

// jQueryUI selectmenu
$(document).ready(function(){
    if( $.fn.selectBox ){
	$('select').selectBox();
    }
});

     $(document).ready(function(){
			   if( $.browser.safari && !(/chrome/i.test(navigator.userAgent)) ){
			       var btn = $( document.createElement('button') );
			       btn.text("Search Games").attr('id', 'edit-submit--2');
			       $('input[type="submit"][value="Search Games"]').replaceWith( btn );
			   }
		       });

})(jQuery);;

