//coin slider instantiation
$j(document).ready(function() {
		$j('#slideshow').coinslider({ 
      width: 602,
      height: 206,
      spw: 1, // squares per width
      sph: 3, // squares per height
      delay: 4500, // delay between images in ms
      sDelay: 30, // delay beetwen squares in ms
      opacity: 0.7, // opacity of title and navigation
      titleSpeed: 500, // speed of title appearance in ms
      effect: 'straight', // random, swirl, rain, straight
      navigation: false, // prev next and buttons
      links : true, // show images as links
      hoverPause: true // pause on hover
    });
});

$j(document).ready(function(){
	$j('.coffee_map').mobilymap({
		position: 'center',
		popupClass: 'bubble',
		markerClass: 'point',
		popup: true,
		cookies: true,
		caption: false,
		setCenter: true,
		navigation: true,
		navSpeed: 1000,
		navBtnClass: 'navBtn',
		outsideButtons: '.map_buttons a',
		onMarkerClick: function(){},
		onPopupClose: function(){},
		onMapLoad: function(){}
	});
  $j('.point').bind('click', function(){
    if (!$j(this).text() > 0){
       $j(this).next().addClass('empty');
    }
  });
});

/* ------------------- SLIDER for MAP Links ------------------ */
var dashTabs = function() {    
    var _tabs = ["hawaii", "centralamerica", "southamerica", "africaarabia", "asiapacific", "australia"];
    var _skip = ["topprofile"];
    var _arrowW = 31;
    var _active;
        
    $j(window).load(function() {
        var total = 0;
        for (var i = 0; i < _tabs.length; ++i) {
            var tab = $j("#" + _tabs[i]); 
            var w = parseInt((tab).css("margin-left")) + parseInt((tab).css("margin-right")) + (tab).width();
            $j(tab).data("tab-x", total + ((w - _arrowW) / 2));
            total += w;
        }
    });
    
    $j(document).ready(function() {
        for (var i = 0; i < _tabs.length; ++i) {
            var tab = $j("#" + _tabs[i]);
            if ($j(tab).find("a").hasClass("active")) {
                _active = tab;
            } else {
                $j(tab).hover(
                    function() { goto($j(this)); },
                    function() { goto(_active); }
                );
            }
        }
    });

    var goto = function (tab)
    {
        var skip = false;
        for (var i = 0; i < _skip.length; ++i)
            if ($j(tab).attr("id") == _skip[i])
            {
                skip = true;
                break;
            }
        if (skip)
            tab = _active;
        var x = $j(tab).data("tab-x");
        $j(".map_buttons").stop().animate({ backgroundPosition: x + "px 0px" }, "fast");
    };     
    return {
    };
}();

//POP Modal for Promos
$j(document).ready(function(){				   		   
	//When you click on a link with class of poplight and the href starts with a # 
	$j('a.poplight[href^=#]').click(function() {
		var popID = $j(this).attr('rel'); //Get Popup Name
		var popURL = $j(this).attr('href'); //Get Popup href to define size
				
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value

		//Fade in the Popup and add close button
    var baseskinurl = 'http://www.conservatorycoffeeandtea.com/';
		$j('#' + popID).fadeIn().css({ 'width': Number( popWidth ) })
      .prepend('<a href="#" class="close"><img src="' + baseskinurl + 'skin/frontend/default/conservatory/images/popclosebtn.png" class="btn_close" title="Close Window" alt="Close" /></a>');
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($j('#' + popID).height() + 80) / 2;
		var popMargLeft = ($j('#' + popID).width() + 80) / 2;
		
		//Apply Margin to Popup
		$j('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$j('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$j('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		return false;
	});
	
	//Close Popups and Fade Layer
	$j('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	  	$j('#fade , .popup_block').fadeOut(function() {
			$j('#fade, a.close').remove();  
	}); //fade them both out
		
		return false;
	});
})

/*--------------- QTY Increase/Decrease buttons ----------------- */
$j(function() {
    $j(".plus").click(function() {
        var currentVal = parseInt($j(this).parents().next(".qty").val());
        if (currentVal != NaN) {
            $j(this).parents().next(".qty").val(currentVal + 1);
        } 
    });
    $j(".minus").click(function() {
        var currentVal = parseInt($j(this).parents().next(".qty").val());
        if (currentVal == "NaN") currentVal = 0;
        if (currentVal > 0) {
           $j(this).parents().next(".qty").val(currentVal - 1);
        }
    });
});
