jQuery.noConflict();

//dropdown function
	jQuery.fn.dropdown = function (options) {
	  var settings = jQuery.extend({
	    timeout: 0.2
	  }, options);
	  var closetimer = null;
	  var ddmenuitem = null;
	  
	  var pos = jQuery(this).position();
	  var width = jQuery(this).width();
	
	  jQuery(this).parent().hover(dropdown_open, dropdown_timer);
	  document.onclick = dropdown_close;
	
	  function dropdown_open()
	  {
		var ddWidth = jQuery('.dropmenu').width();
		
	    dropdown_canceltimer();
	    dropdown_close();
	    ddmenuitem = jQuery('.dropmenu').css('display', 'block');
		
		jQuery('.dropmenu').css({ width: ddWidth});
	  }
	
	  function dropdown_close() {
	    if (ddmenuitem) {
	
	      ddmenuitem.css('display', 'none');
	    }
	  }
	
	  function dropdown_timer() {
	    closetimer = window.setTimeout(dropdown_close, settings.timeout * 1000);
	  }
	
	  function dropdown_canceltimer() {
	    if (closetimer) {
	      window.clearTimeout(closetimer);
	      closetimer = null;
	    }
	  }
	
	  return this;
	};

//carousel
jQuery( document ).ready( function($){
	//carousel data for my stores
	var boxTopS = '<div class="box"><div class="box-t"><div class="box-b">';
	var carTopS = '<div class="box-sq"><div class="box-sq-t"><div class="box-sq-b"><div class="brands-list"><ul>';
	$('.index-yourstores').each(function (i) {
		var featureTitle = $( '.rcg_content .bre_zone3Title', this).text();
		$('#content').append(boxTopS);
		$('.box-b').append('<a class="right small" href="shop-stores.jsp?startIndex=1&sort=sd">View All</a><h2>'+featureTitle+'</h2>');
		$('.box-b').append(carTopS);
		var innerText = '';
		$('.index-yourstores .rcg_content div div div div').each(function (j) { 
				var brandImg = $('a:first-child', this).html();
				var brandLink = $('a:first-child', this).attr("href");
				var brandBR = $('strong', this).text();
				innerText += '<li><p class="logo"><a href="' + brandLink + '">' + brandImg + '</a></p><p><strong>' + brandBR + '</strong></p></li>';
		});
		$('.box-b .brands-list ul').append(innerText);
	});
	//extract the carousel data from drupal for the homepage
	var boxTop = ['<div class="box"><div class="box-t"><div class="box-b0">','<div class="box"><div class="box-t"><div class="box-b1">'];
	var carTop = '<div class="box-sq"><div class="box-sq-t"><div class="box-sq-b"><div class="deals-list"><ul>';
	$('.index-featureditems').each(function (i) {
		var featureTitle = $( '.rcg_content .bre_zone3Title', this).text();
		$('#content').append(boxTop[i]);
		$('.box-b' + i).append('<h2>'+featureTitle+'</h2>');
		$('.box-b' + i).append(carTop);
		var innerText = '';
		$('.index-featureditem', this).each(function (j) { 
				var itemName = $('.bre_merchantName', this).text();
				var itemImg = $('.bre_zone1Details .bre_productImage', this).html();
				var itemBR = $('.bre_zone1Details .bre_productBR', this).text();
				itemBR = itemBR.replace(/BondRewards/ig, "");
				innerText += '<li><p class="image">' + itemImg + '</p><p><strong>' + itemBR + '</strong><br/>'+ itemName + '</p></li>';
		});
		$('.box-b' + i+ ' .deals-list ul').append(innerText);
	});
	
	//homepage hacks
	var currentTitle = $('.brands #block-jsp-15 .content .legendLeftContainer div:nth-child(2) .legendLeftBoxFieldset .legendLeftBoxFieldsetLegend').text();
	$('.brands #block-jsp-15 .content .legendLeftContainer div:nth-child(2) .legendLeftBoxFieldset').prepend("<div class='curTitle'>" + currentTitle + "</div>");
	$('.brands-list ul li:last').css({border:0});
	
	//initialize carousel
	$('.brands-list ul').jcarousel({
		scroll: 1
	});
	$('.deals-list ul').jcarousel({
		scroll: 1,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	$('.steps ul').jcarousel({
		scroll: 1,
		auto: 5,
		wrap: 'last',
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemVisibleOutCallback: callbackOut,
		itemVisibleInCallback: callbackIn
	});
	
	//shopping page tab navigation
	var tabContainers = $('#az, #featured');
	$('div.shop ul.tabNav a').click(function () {
		tabContainers.hide().filter(this.hash).show();
		
		$('div.shop ul.tabNav a').removeClass('active');
		$(this).addClass('active');
		return false;
	});
	
	//popup
	function overlay_height() {
		body_height = $(document).height();
		$('.overlay').css('height', body_height);
	}
	function hide_popups() {
		$('.popup').hide();
		$('.overlay').fadeOut();
	}
	overlay_height();
	
	function center_popup(popup) {
		body_width = parseInt($('body').width());
		window_height = parseInt($(window).height());
		popup_width = parseInt($(popup).css('width'));
		popup_height = parseInt($(popup).height());
		left_position = parseInt((body_width/2) - (popup_width/2));
		top_position = $(document).scrollTop() + parseInt((window_height/2) - (popup_height/2));
		popup.css({'left': left_position, 'top' : Math.abs(top_position)});
	}
	$('.lightbox').live('click', function() {
		$('<div />').addClass('overlay').appendTo('body').show();
		$('<div />').addClass('popup').appendTo('body').show();
		center_popup($('.popup'));
		$('.popup').load('../joinnow-popup.jsp');
	});
	
	$('.popup .close').live('click', function() {
		$(this).parent().hide();
		$('.overlay').fadeOut();
	});
	
});

jQuery(document).ready(function($) {
		//dropdown menu
	$('.dropdown').dropdown();
	//shopstyles hack 
	$('#block-jsp-15 .content .legendLeftContainer div:nth-child(2)').css({display:"block"});
	$('#block-jsp-15 .content .legendLeftContainer .legendLeftBox .legendLeftBoxFieldset .ftBrand').css({display:"block"});
});

//carousel with autoscrolling
	function mycarousel_initCallback(carousel) {
		  jQuery('.alliance-stores').show();	
		  jQuery('.steps ul li').css({visibility:"visible"});
		  
		// Disable autoscrolling if the user clicks the prev or next button.
		jQuery('.steps-nav a').bind('click', function() {
		carousel.startAuto(0);
		var index = jQuery(this).parent().find('a').index(this);
	    carousel.scroll(index + 1);
	    
	    jQuery('.steps-nav a').removeClass('active');
	    jQuery('.steps-nav a').eq(index).addClass('active');
	
	    return false;
		});
	
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
			}, function() {
			carousel.startAuto();
		});
	};
	
	function callbackIn(carousel, li, index) {
		jQuery('div.steps-nav a:nth-child(' +index + ')').addClass('active');
	};
	function callbackOut(carousel, li, index) {
		jQuery('div.steps-nav a:nth-child('+ index+ ')').removeClass('active');
	};