var xhr;

$.fn.cycle.transitions.scrollVertUp = function($cont, $slides, opts) {
	$cont.css('overflow','hidden');
	opts.before.push(function(curr, next, opts, fwd) {
	$.fn.cycle.commonReset(curr,next,opts);
	opts.cssBefore.top = fwd ? (next.cycleH-1) : (1-next.cycleH);
	opts.animOut.top = fwd ? -curr.cycleH : curr.cycleH;
	});
	opts.cssFirst = { top: 0 };
	opts.cssBefore= { left: 0 };
	opts.animIn = { top: 0 };
	opts.animOut = { left: 0 };
};

$(document).ready(function(){		// Toggling on a committee button 

	//Hide (Collapse) the toggle containers on load
	$(".hidden").hide(); 

	//Slide up and down on click
	$(".open").click(function(){
		$(this).next(".hidden").slideToggle("fast");
	});
	
	// Scrolling parking thingy.
	$("#choose-zoom").click(function(e) {
		$('#zoom div').animate({opacity: 0 }, 500);
		
		var id;
		var addX = 0;
		var addY = 0;
		
		e.preventDefault();
		if($(e.target).is('a')) {
			id = $(e.target).parent().attr('id');
			var pos = $(e.target).parent().position();
			var mouseX = pos.left;
	   		var mouseY = pos.top;
	   		var addX = $('#zoom').width() / 2;
	   		var addY = $('#zoom').height() / 2;
		}
		else {
			var mouseX = e.layerX;
	   		var mouseY = e.layerY;	
		}
	   	var zoomX = (mouseX / parseInt($(this).width())) * parseInt($('#zoom img').width() - addX);
	   	var zoomY = (mouseY / parseInt($(this).height())) * parseInt($('#zoom img').height() - addY);
	   	$('#zoom').scrollTo({ left: zoomX, top: zoomY}, 800, {
	   		onAfter: function() {
	   			if(id!=0) {
	   				$('#zoom div#z' + id).pulse({
					    opacity: [0, 1],
					}, {
						duration: 200,
						times: 3	
					});	
	   			}
	   		}	
	   	});			   			
	});
	
	$('input:text').example(function() { return $(this).attr('title'); });
	
	// Category cycle.
	$('#bd-categories-cycle').cycle({ 
	    fx:     'scrollVertUp',
	    prev:   '#c-prev', 
	    next:   '#c-next',
	    timeout: 0,
	    speed: 500,
	    nowrap: true
	});
	
	$('#bd-retailers-cycle').livequery(function() {
		$(this).cycle({ 
		    fx:     'scrollVertUp',
		    prev:   '#r-prev', 
		    next:   '#r-next',
		    timeout: 0,
		    speed: 500,
		    nowrap: true
		});
	});	
	
	$('#bd-categories-cycle a').click(function() {
		var ahref = $(this).attr('href');		
		$('#bd-retailers').append( $('#ajax-load').clone().addClass('loader-clone') );
		$('.loader-clone').show();
		
		$('#bd-categories-inner').animate({opacity:0}, 200, function() {
			xhr = $(this).load(ahref + ' #bd-categories-inner div', function() { 
				$(this).animate({opacity:1}, 200, function() {
					$('.loader-clone').remove();	
				});
				if( $('#bd-retailers-cycle ul').length > 1 ) {
					$('#retailer-pager').show();
				}
				else {
					$('#retailer-pager').hide();
				}
			});
		});
		return false;
	});
	
	$('#bd-retailers-cycle a').live('click', function() {
		var ahref = $(this).attr('href');		
		$('#retailerDetails').append( $('#ajax-load').clone().addClass('loader-clone') );
		$('.loader-clone').show();
		$('#retailerDetails').load(ahref + ' #bd-details', function() {
			$('.loader-clone').remove();
		});
		return false;
	});
	
	// Form messages
	$('#form-msg-area').animate({opacity:1}, 7000, function() {
		$(this).slideUp(300, function() {
			$(this).empty().show();
		});
	});

});

// Navigation roll over animation //
$(document).ready(function() {

	$("#top-nav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag

	$("#top-nav li").each(function() { //For each list item...
		var linkText = $(this).find("a").html(); //Find the text inside of the <a> tag
		$(this).find("span").show().html(linkText); //Add the text in the <span> tag
	}); 

	$("#top-nav li").hover(function() {	//On hover...
		$(this).find("span").stop().animate({
			marginTop: "-23" //Find the <span> tag and move it up 40 pixels
		}, 150);
	} , function() { //On hover out...
		$(this).find("span").stop().animate({
			marginTop: "0"  //Move the <span> back to its original state (0px)
		}, 150);
	});

	// Slideshow image fading //
    $('.slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});

});


// Jquery image fading on rollover

$(function() {
// OPACITY OF BUTTON SET TO 50%
$(".latest_img").css("opacity","0.5");
		
// ON MOUSE OVER
$(".latest_img").hover(function () {
										  
// SET OPACITY TO 100%
$(this).stop().animate({
opacity: 1.0
}, "slow");
},
		
// ON MOUSE OUT
function () {
			
// SET OPACITY BACK TO 50%
$(this).stop().animate({
opacity: 0.5
}, "slow");
});
});


$(function() {
// OPACITY OF BUTTON SET TO 50%
$("#text p").css("opacity","0.5");
		
// ON MOUSE OVER
$("#text p").hover(function () {
										  
// SET OPACITY TO 1000%
$(this).stop().animate({
opacity: 1.0
}, "slow");
},
		
// ON MOUSE OUT
function () {
			
// SET OPACITY BACK TO 50%
$(this).stop().animate({
opacity: 0.5
}, "slow");
});
});


$(function() {
// OPACITY OF BUTTON SET TO 50%
$("#div").css("opacity","0.5");
		
// ON MOUSE OVER
$("#div").hover(function () {
										  
// SET OPACITY TO 100%
$(this).stop().animate({
opacity: 1.0
}, "slow");
},
		
// ON MOUSE OUT
function () {
			
// SET OPACITY BACK TO 50%
$(this).stop().animate({
opacity: 0.5
}, "slow");
});
});



// LOGIN WINDOW 
        $(document).ready(function() {

            $(".signin").click(function(e) {
                e.preventDefault();
                $("fieldset#signin_menu").toggle();
                $(".signin").toggleClass("menu-open");
            });

            $("fieldset#signin_menu").mouseup(function() {
                return false
            });
            $(document).mouseup(function(e) {
                if($(e.target).parent("a.signin").length==0) {
                    $(".signin").removeClass("menu-open");
                    $("fieldset#signin_menu").hide();
                }
            });            

        });
