//Fill URL Form on Product Page for Shipping Quote
function fillURL() {
	document.getElementById('concerning').value = location.href;
	document.getElementById('prodLink').value = location.href;
	document.getElementById('prodLinkts').value = location.href;
	document.getElementById('prodLinkmo').value = location.href;
}

// jQuery Functions
var $j = jQuery.noConflict();

//create todays date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!
var yyyy = today.getFullYear();
if(mm<10){mm='0'+mm};//add a 0 to month if less than 10
if(dd<10){dd='0'+dd};//add a 0 to day if less than 10
//var todaysDate = mm+"/"+dd+"/"+yyyy;
var todaysDate = yyyy+"-"+mm+"-"+dd;
//document.write(todaysDate);

//jQuery Carousel
function mycarousel_initCallback(carousel)
{
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

// Best Price Email Mod
function ShowHideEmail(item,item2){
	$j(item).animate({"height": "toggle"}, { duration: 200 });
	$j(item2).hide();
}

function showHideCookie(thepanel, thebutton, thePlusMinus, thecookie) {	
	var initialState = "expanded";
	var activeClass = "hidden";
	var visibleText = '&#45;';
	var hiddenText = '&#43;';

	var panel = $j(thepanel);
	var button = $j(thebutton);	
	var plusMinus = $j(thePlusMinus);
	
	if($j.cookie(thecookie) == undefined || $j.cookie(thecookie) == "expanded") {
		$j.cookie(thecookie, initialState);
		plusMinus.html(visibleText);
	} 
	var state = $j.cookie(thecookie);
	if(state == "collapsed") {
		panel.hide();
		button.addClass(activeClass);
		plusMinus.html(hiddenText);
	}
	button.click(function(){
		if($j.cookie(thecookie) == "expanded") {
			$j.cookie(thecookie, "collapsed");
			button.addClass(activeClass);
			plusMinus.html(hiddenText);

		} else {
			$j.cookie(thecookie, "expanded");
			button.removeClass(activeClass);
			plusMinus.html(visibleText);
		}
		panel.animate({"height": "toggle"}, { duration: 200 });
		return false;
	});
}

function fader(item) {
	$j(item).hover(function(){
	   $j(this).stop().fadeTo(300, 0.5);
	},function(){
	   $j(this).stop().fadeTo(300, 1.0);
	});
}

// anyslider
$j(function () {        
	$j('.anythingSlider').anythingSlider({
    	easing: "swing",        // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 3000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 300,             // How long the slide transition takes
        hashTags: false,                 // Should links change the hashtag in the URL?
        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
        pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
        startText: "Autoplay is Off",                // Start text
    	stopText: "Autoplay is On"
     });       
});

// Mega Menu
$j(function() {
	$j("#mega_menu").megaMenu();
});

$j(document).ready(function() {
	
	$j("a[rel='colorbox']").colorbox({maxWidth:"85%", maxHeight:"85%", scalePhotos:true, transition:"elastic", speed: 350}).show();
	
	// Mega Menu Toggle
	$j("#megaMenuClick").click(function () {
	  var currentDisplay = $j("#mega_div").css("display"); // save the current Display value
	  $j("#mega_div").slideToggle("fast", function () { 
			if (currentDisplay == "none") {
			  $j("#mega_div").css('display', 'block');
			} else {
			  $j("#mega_div").css('display', 'none');
			}
		});
		$j(".catToggle").toggleClass("opened");
		return false;
	});
	$j("#mega_div .close").click(function(){
		$j(".catToggle").toggleClass("opened");
		$j("#mega_div").slideToggle("fast", function() {
			$j("#mega_div").css('display', 'none');
		});
		return false; //Prevent the browser jump to the link anchor
	});
	
	
	// Brands in category toggle
	/*var $qfb = $j("#QuickFindBrand .brand_links"),
    qfbOldH = $qfb.height(),
    qfbNewH;

	$qfb.css('height', 'auto');
	qfbNewH = $qfb.height();
	$qfb.css('height', qfbOldH);
	
	if (qfbNewH > qfbOldH) {
		$j("#QuickFindBrand .expand_brand").html('<div class="expand_brand_box"></div>');
		$j("#CatDescription").css('margin-top', '60px');
		$j('#QuickFindBrand').mouseover(function() {
			$j(".expand_brand_box").css('background-position', '0 -13px');
			$qfb.stop().animate({
				'height': qfbNewH
			}, 200);
		});
		$j('#QuickFindBrand').mouseout(function() {
			$j(".expand_brand_box").css('background-position', '0 0');
			$qfb.stop().animate({
				'height': qfbOldH
			}, 200);
		});
	}
	if (qfbNewH < qfbOldH) {
		$j("#QuickFindBrand .brand_links").css('height', '28px');
		$j("#CatDescription").css('margin-top', '55px');
	}
	if (qfbNewH == qfbOldH) {
		$j("#CatDescription").css('margin-top', '80px');
	}*/
	
	// Clearance Slider (home page)
	$j('#clearance_slider').bxSlider({
		auto: true,
		pause: 4000,
		pager: true,
		pagerSelector: '#clearance-pager',
		randomStart: true
	});
	
	$j.featureList(
		$j("#featured_tabs li a"),
		$j("#featured_output li"), {
			start_item	:	0
		}
	);
	
	showHideCookie("#SubBrands", "#brandTab", ".plusMinusTxt", "brandPanelState");
	showHideCookie("#SubCategories", "#catTab", ".plusMinusTxt2", "catPanelState");

	fader(".jqueryHighlight");
	fader(".fade");
	
	// Left Side Carousel "Top Products"
    $j('#mycarousel').jcarousel({
        vertical: true,
        scroll: 2,
		auto: 2,
		wrap: 'last',
        initCallback: mycarousel_initCallback
    });
	
	// Quick Links Carousel in Categories
	/*$j('#catCar').jcarousel({
        vertical: true,
        scroll: 2
    });*/
	
	// Holiday Featured Products Carousel
	$j('#featuredCarousel').jcarousel({
        auto: 4.5,
        wrap: 'last',
		initCallback: mycarousel_initCallback
    });
	
	// Hot Deals Products Carousel
	$j('#hotDealsCarousel').jcarousel({
        /*auto: 4.5,*/
        /*wrap: 'last',*/
		initCallback: mycarousel_initCallback
    });
	// New Arrivals Products Carousel
	$j('#newArrivalsCarousel').jcarousel({
        /*auto: 4.5,*/
       /* wrap: 'last',*/
		initCallback: mycarousel_initCallback
    });
	// Sale & Clearance Products Carousel
	$j('#saleClearanceCarousel').jcarousel({
        /*auto: 4.5,*/
        /*wrap: 'last',*/
		initCallback: mycarousel_initCallback
    });
	
	
	$j("#SaleClearanceSlider").slideToggle();
	$j(".view_more_slider_deals").click(function(){
		$j(".catSliderToggle").toggleClass("opened");
		$j("#NewArrivalsSlider").slideToggle("fast");
		$j("#SaleClearanceSlider").slideToggle("fast");
		return false; //Prevent the browser jump to the link anchor
	});
	
	//Parse XML of Event Calendar   
	$j.ajax({
            type: "GET",
            url: "http://www.jimlaabsmusic.com/xml/calendar.xml",
            dataType: "xml",
            success: function(xml) {
                $j(xml).find('events').each(function(){
				    //var dater = $j(this).attr("date");
                    var title = $j(this).find('title').text();
					var when = $j(this).find('when').text();
                    var description = $j(this).find('description').text();
					
					//if the event is past, don't show (uses date from <when> tag in xml
					if (when < todaysDate) {
                    	//show nothing
					} else {
						$j('<h2 class="events_title"></h2>').html(title).appendTo('#events_wrap');
						$j('<div class="events_when"></div>').html(when).appendTo('#events_wrap');
						//$j('<div></div>').html(dater).appendTo('#events_wrap');
                    	$j('<div class="events_description"></div>').html(description).appendTo('#events_wrap');
					}
                });
            }
	});
	
	// Shipping Quote
	$j('#contactform').submit(function(){
		var action = $j(this).attr('action');
		$j('#submit')
			.before('<img src="images/loading.gif" class="loader" />')
			.attr('disabled','disabled');
 
		$j.post(action, { 
			name: $j('#name').val(),
			email: $j('#email').val(),
			phone: $j('#phone').val(),
			concerning: $j('#concerning').val(),
			prodTitle: $j('#prodTitle').val(),
			message: $j('#message').val()
		},
			function(data){
				$j('#contactform #submit').attr('disabled','');
				$j('.response').remove();
				$j('#contactform').before('<span class="response">'+data+'</span>');
				$j('#contactform img.loader').fadeOut(500,function(){$j(this).remove()});
			}
		);
 
		return false;
 
	});
	
	// Best Price email jquery
	$j('#bestPriceEmail').submit(function(){
		var action2 = $j(this).attr('action');
		$j('#submit2')
			
			.attr('disabled','disabled');
 
		$j.post(action2, { 
			email2: $j('#email2').val(),
			prodLink: $j('#prodLink').val(),
			prodTitle2: $j('#prodTitle2').val(),
			offer: $j('#offer').val()
		},
			function(data){
				$j('#bestPriceBox #submit2').attr('disabled','');
				$j('.response2').remove();
				$j('#bestPriceBox').before(data);

				if(data=='<span class="response2sent">Sent!</span>') { 
					$j("#bestPriceBox").animate({"height": "toggle"}, { duration: 200 });
					window.setTimeout(function() {
							$j('.response2sent').fadeOut('fast',function(){
									$j('.response2sent').remove();
							});
					}, 1000);
				}
				if(data=='<span class="response2">Use a valid email!</span>') { 
					window.setTimeout(function() {
							$j('.response2').fadeOut('fast',function(){
									$j('.response2').remove();
							});
					}, 1000);
				}
			}
		);
 
		return false;
 
	});
	
	// Trade N Save email jquery
	$j('#tradeSaveEmail').submit(function(){
		var action3 = $j(this).attr('action');
		$j('#submitts')
			
			.attr('disabled','disabled');
 
		$j.post(action3, { 
			emailts: $j('#emailts').val(),
			prodLinkts: $j('#prodLinkts').val(),
			prodTitlets: $j('#prodTitlets').val(),
			tradets: $j('#tradets').val()
		},
			function(data){
				$j('#tradeSave #submitts').attr('disabled','');
				$j('.responsets').remove();
				$j('#tradeSave').before(data);

				if(data=='<span class="responsetssent">Sent!</span>') { 
					$j("#tradeSave").animate({"height": "toggle"}, { duration: 200 });
					window.setTimeout(function() {
							$j('.responsetssent').fadeOut('fast',function(){
									$j('.responsetssent').remove();
							});
					}, 1000);
				}
				if(data=='<span class="responsets">Use a valid email!</span>') { 
					window.setTimeout(function() {
							$j('.responsets').fadeOut('fast',function(){
									$j('.responsets').remove();
							});
					}, 1000);

				}
				if(data=='<span class="responsets">Please put your trade in!</span>') { 
					window.setTimeout(function() {
							$j('.responsets').fadeOut('fast',function(){
									$j('.responsets').remove();
							});
					}, 1000);
				}
			}
		);
 
		return false;
 
	});
	
	
	// Make Offer email jquery
	$j('#makeoffer').submit(function(){
		var action5 = $j(this).attr('action');
		$j('#submitmo')
			
			.attr('disabled','disabled');
 
		$j.post(action5, { 
			emailts: $j('#emailmo').val(),
			prodLinkts: $j('#prodLinkmo').val(),
			prodTitlets: $j('#prodTitlemo').val(),
			tradets: $j('#trademo').val()
		},
			function(data){
				$j('#makeoffer #submitmo').attr('disabled','');
				$j('.responsemo').remove();
				$j('#makeoffer').before(data);

				if(data=='<span class="responsemosent">Sent!</span>') { 
					$j("#makeoffer").animate({"height": "toggle"}, { duration: 200 });
					window.setTimeout(function() {
							$j('.responsemosent').fadeOut('fast',function(){
									$j('.responsemosent').remove();
							});
					}, 1000);
				}
				if(data=='<span class="responsemo">Use a valid email!</span>') { 
					window.setTimeout(function() {
							$j('.responsemo').fadeOut('fast',function(){
									$j('.responsemo').remove();
							});
					}, 1000);

				}
				if(data=='<span class="responsemo">Please put your trade in!</span>') { 
					window.setTimeout(function() {
							$j('.responsemo').fadeOut('fast',function(){
									$j('.responsemo').remove();
							});
					}, 1000);
				}
			}
		);
 
		return false;
 
	});
	
	
	//jQuery UI Tabs
	$j('#tabs').tabs();
	function runEffect(){
		$j("#tabs").effect('highlight',{},500);
	};
	$j("#tabs").ready(function() {
			runEffect();
			return false;
	});
	
});
