/*

Javascript for Aethercomm
Created by Rareview Interactive
http://www.rareview.com

*/

function update_items()
{
	var s = '';
	if(products !== 1) { s = 's'; }
	$('.viewRfqs span').html(products + ' RFQ' + s);
}


/* =jQuery
============================================================================== */

$(document).ready(function() {

	/* Submit tables
	-------------------------------------------------------------------------- */
/*
	$('#botChange select').blur(function(){
		document.forms["botChange"].submit();
	});
*/

	/* Add RFQ
	-------------------------------------------------------------------------- */

	$('.addRfq').click(function(){
		var id = $(this).attr('id').substr(1);

		$.post(url + "products/add/" + id, {}, function(data) {
			data = parseInt(data);
			$("#p" + id).parent().find(".productAdded").fadeIn('fast').delay(700).fadeOut('fast');
			products = data;
			update_items();
		}, 'html');

		return false;
	});

	/* Change Quantity RFQ
	-------------------------------------------------------------------------- */

	$(".quanItem").blur(function() {
		var id = $(this).attr('id').substr(1);
		var count = $(this).val();

		$('#l' + id).fadeIn('fast').delay(800);

		$.ajax({
			type: 'POST',
			url: url + "products/write/" + id + "/" + count,
			async: false,
			data: {},
			success: function(data) {
				if(data !== '1') {
					alert("Server Error");
				}

				$('#l' + id).fadeOut('fast');
			},
			datatype: 'html'});
		return false;
	});

	/* Remove RFQ Row
	-------------------------------------------------------------------------- */

	$(".productRemove a").click(function(){
		var id = $(this).attr('id').substr(1);
		var s = '';
		products--;
		if(products !== 1) { s = 's'; }

		$.ajax({
			type: 'POST',
			url: url + "products/write/" + id + "/0",
			async: false,
			data: {},
			success: function(data) {
				if(data !== '1') {
					alert("Server Error");
				}

				$('#totalProducts').html(products + ' item' + s + ' total');
				update_items();

				if(products == 0)
				{
					window.location = url + "account/rfqs";
				}

				$('#r' + id).fadeOut('slow');
			},
			datatype: 'html'});
		return false;
	});

	/* Default form values clear
	-------------------------------------------------------------------------- */
	var default_l_freq = $('#l_freq').val();
	$('#l_freq').focus(function() {
		if( this.value == default_l_freq ) {
			$(this).val("");
		}
	});
	var default_h_freq = $('#h_freq').val();
	$('#h_freq').focus(function() {
		if( this.value == default_h_freq ) {
			$(this).val("");
		}
	});

	/* Homepage rotate banners
	-------------------------------------------------------------------------- */
		
		var images = ['intro-bg.jpg', 'intro-bg2.jpg', 'intro-bg3.jpg', 'intro-bg4.jpg', 'intro-bg5.jpg', 'intro-bg6.jpg', 'intro-bg7.jpg'];
		$('#intro').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});
		
	
	/* Superfish Dropdown Menu
	-------------------------------------------------------------------------- */
    
    	$('ul.sf-menu').superfish({ 
            animation: {height:'show'},
            speed: 0,
            autoArrows: false,
            dropShadows: false,
            delay: 400
        });
        
	/* Search Layover
	-------------------------------------------------------------------------- */
	
		$('#search input').keyup(function(){
			$('#search').addClass('active');
			var terms = $('#search input').val();

			$.ajax({
				type: 'POST',
				url: url + "products/search/" + terms,
				data: { search: 1 },
				success: function(data) {
					$('#searchLayover').html(data);
					$('#searchLayoverContainer').show();
				},
				datatype: 'html'});
			});
		
		$('#search input').blur(function(){
			$('#search').wait(500).removeClass('active');
			$('#searchLayoverContainer').wait(500).hide();
		});
        
    /* Toggle Reps
	-------------------------------------------------------------------------- */
        
        $('.toggleRep').click(function(){
        	$(this).parent().toggleClass('active');
        	return false;
        });
        
	/* Billing Address - disable if same as Shipping
	-------------------------------------------------------------------------- */
        
        var sameAsBilling = $("#billing_copy");
        if (sameAsBilling.is(":checked")) {
            $("#shippingContainer input, #shippingContainer select").attr("disabled", "true");
            $("#shippingContainer li, #shippingContainer .ui-selectmenu").fadeTo("fast", 0.4);
        }
        sameAsBilling.click(function() {
            if (sameAsBilling.is(":checked")) {
                $("#shippingContainer input, #shippingContainer select").attr("disabled", "true");
                $("#shippingContainer li, #shippingContainer .ui-selectmenu").fadeTo("fast", 0.4);
            } else {
                $("#shippingContainer input, #shippingContainer select").removeAttr("disabled");
                $("#shippingContainer li, #shippingContainer .ui-selectmenu").fadeTo("fast", 1);
            }
        });  
        
	/* Custom Form Selects
	-------------------------------------------------------------------------- */    
        
        if ($('.customSelect').length > 0) {
	        $('.customSelect').selectmenu({
				style:'dropdown',
				maxHeight:250
	    	});
	    }

	/* Tabs
	-------------------------------------------------------------------------- */
        if ($('#interiorTabs').length > 0) {
			if($('#accountPassword .errorMsg').length > 0 || $('#accountPassword .successMsg').length > 0)
			{
				$('#interiorTabs').tabs({selected: 1});
			}
			else if(typeof tabSel !== 'undefined')
			{
				$('#interiorTabs').tabs({selected: tabSel});
			}
			else
			{
				$('#interiorTabs').tabs();
			}
        }
	    
	/* More/Less
	-------------------------------------------------------------------------- */

		// more
		$('.showMoreText').live('click', function() {
		   $(this).hide().next('.hiddenDesc').show();
		});
		
		// less 
		$('.showLessText').live('click', function() {
		   $(this).parent().prev('.showMoreText').show().next('.hiddenDesc').hide();
		});
		
	
	/* Pop up window
	-------------------------------------------------------------------------- */
		
		$('.popup').popupWindow({ 
			height:600, 
			width:800, 
			top:50, 
			left:50,
			scrollbars:1
		}); 

		
	/* Company Tour Carousel
	-------------------------------------------------------------------------- */

		var tourCount = $('#companyTour img').length;
		var tourPhotoName = $('#companyTour img:visible').attr('alt');
		var currTourPhoto = 1;
	
		var afterEndCallback = function(visible){
			tourPhotoName = $('img',visible).attr('alt');
			currTourPhoto = $('span',visible).attr('id').substring(1);
			setCount();
		}
	
		var setCount = function() {
			$('#tourCounter strong').text(tourPhotoName);
			$('#tourCounter span').text('Photo '+currTourPhoto+' of '+tourCount);
		}
		
		if ($('#companyTour').length > 0) {
			$('#companyTour').jCarouselLite({
	    		btnNext: '#nextPhoto',
	    		btnPrev: '#prevPhoto',
	    		speed: 900,
	    		visible: 1,
	    		circular: false,
	    		easing: 'easeInOutExpo',
	    		afterEnd: afterEndCallback
	    	});
	    }
	
		setCount();
			    
}); // end document ready
