function resizeForSmallBrowsers() {
    if (jQuery(window).height() < 650) {
        jQuery("#navigation").css("margin-top", "28px");
    }
}

function enforceHttps() {
    if (window.location.pathname.toLowerCase().substr(0,9) == '/shopping') {
    	if (window.location.protocol != "https:") {
	    // flip to https when on shopping cart
    	    window.location.href = window.location.href.replace('http://','https://');
	}
	// else DO NOTHING
    } else {
    	if (window.location.protocol == "https:") {
	    // flip to http when NOT on shopping cart
	    window.location.href = window.location.href.replace('https://','http://');
	}
        // else DO NOTHING
    }
}

jQuery(document).ready(function(){
    // MS 11/8/2011
    // ################## LIVE SERVER ONLY ##################################################################################
    enforceHttps();
    // ################## LIVE SERVER ONLY ##################################################################################
    
    resizeForSmallBrowsers();

    if (jQuery(".contentSlider").length > 0) {
        jQuery(".contentSlider").bxSlider({
	    controls: false,
	    infiniteLoop: true,
	    pager: true,	
	    auto: true,
	    pause: 8000,
	    speed: 1000,
	    //randomStart: true,
	    onAfterSlide: function(currentSlide){
            	jQuery(".bzsSliderBorder")
            	    .unbind()
            	    .bind("click", function(){ jQuery('.contentSlider li').eq(currentSlide + 1).click(); });
            }
        });
    }


    
    if(jQuery(".sidebarNavigation").length > 0) {
    
    	if (jQuery(".breadcrumb a[href='/Plants/Hanging-Baskets']").length > 0) {
    	    //jQuery(".sidebarNavigation td[id$='_1']").css("background-color", "red");
    	    jQuery(".sidebarNavigation td[id$='_1']")
    	    	.addClass("CMSTreeMenuItem")
    	    	.removeClass("CMSTreeMenuSelectedItem")
    	    	
    	    jQuery(".sidebarNavigation td[id$='_2']").remove();
    	    //jQuery("body").append("here");
    	}
    
        // if id ends with "_1"... 
        jQuery(".sidebarNavigation td[id$='_1'],.sidebarNavigation td[id$='_2']").addClass("subMenuItem");

        jQuery(".sidebarNavigation td").each(function(){
            jQuery(this).html(jQuery(this).html().replace(/&[^;]+;/g, ''));        
        });
        
        
        // split the series names
        jQuery(".sidebarNavigation td.CMSTreeMenuItem a:contains(':')").each(function() {
           jQuerya = jQuery(this);
           aItemName = jQuerya.text().split(":", 2);
           jQuerya.html(aItemName[0] +"<span>"+ aItemName[1] +"</span>");
	})
        
    }
    
    // Add last LI for rounded corner
    jQuery("#menuElem li ul").append('<li class="last"></li>');
    //---/ End Add last LI
    
    
    // Dollar Menu Slider / Slider
    var currentPosition = 0;
    var item = jQuery('.item');
    var slideWidth = 505;
    var numberOfItems = item.length;
    
    if (jQuery('#menuContainer').parent().hasClass('grid')) {
        slideWidth = 450;
    }    

    if (numberOfItems <= 1) {
        jQuery('.control').hide();
    }


    if (numberOfItems > 0) {
    
        jQuery("#DollarMenuHomePageLink,.dollarMenu,.dollarMenuFull, .dollarMenuPage").show();
    }


    
    // Set #menuInner width equal to total width of all items
    jQuery('#menuInner').css('width', slideWidth * numberOfItems);
    
    // Create event listeners for .controls clicks
      jQuery('.control').bind('click', function(){
        // Determine new position
          currentPosition = (jQuery(this).attr('id')=='arrowRight')
        ? currentPosition+1 : currentPosition-1;
    
          // Hide / show controls
          manageControls(currentPosition);
          
          // Move slideInner using margin-left
          jQuery('#menuInner').animate({
            'marginLeft' : slideWidth*(-currentPosition)
          });
          
          // manageControls: Hides and shows controls depending on currentPosition
          function manageControls(position){
              // Hide left arrow if position is first slide
              if(position==0){ jQuery('#arrowLeft').hide() }
              else{ jQuery('#arrowLeft').show() }
              // Hide right arrow if position is last slide
              if(position==numberOfItems-1){ jQuery('#arrowRight').hide() }
              else{ jQuery('#arrowRight').show() }
              }

    });

    jQuery('#menuInner').show();
    
    var showUserHelp = function() {
    	if (jQuery("#arrowRight:visible").length > 0) {
    	    var offset = jQuery("#arrowRight").offset();
    	    jQuery("body").prepend('<div id="UserHelper" style="z-index:1000;position:absolute;font-size:10pt;'+
    	    	'top:' + ( offset.top + 40) +'px;'+
    	    	'left:'+ ( offset.left - 28) +'px;'+
    	    '"><img id="HelpWords" style="z-index:1000;display:none;" src="/CountrysideGreenhouse/media/files/dollarMenu_moreItems.png"/><br/><img style="float:right;display:none;" alt="Click the Right arrow to view more Dollar Menu Items" id="HelpImage" src="/CountrysideGreenhouse/media/files/dollar_menu_arrow_help_left.png" /></div>');
    	    jQuery("#HelpWords").show();
    	    jQuery("#HelpImage").show(50);
    	}
    }

    var hideUserHelp = function() {
    	jQuery("#UserHelper").remove();
    }

    // show some tooltips about the left/right arrows
    if (jQuery("div.dollarMenuFull:has(span.control) > *").length > 0) {
	    jQuery("div.dollarMenuFull:has(span.control) > *").hoverIntent({
		over: showUserHelp,
		timeout: 250,
		interval: 500,
		out: hideUserHelp
	    });
    }
    
    
    //---/ End Dollar Menu Slider / Slider


    // Pretty Photo / Scrollable
    jQuery("#plantImages:not(:has(div))").remove();
    
    if (jQuery("a[rel^='prettyPhoto']").length > 0) {
        jQuery("a[rel^='prettyPhoto']").prettyPhoto();
        jQuery("#plantImages ul").wrap("<div class='item'>")
        jQuery("#plantImages .item").wrapAll("<div class='scrollable'>");
        jQuery("#plantImages .item").wrapAll("<div class='items'>");
        jQuery("#plantImages .scrollable").before('<a class="prev browse left"></a>');
        jQuery("#plantImages .scrollable").after('<a class="next browse right"></a>');


        jQuery(".scrollable").scrollable();    
        jQuery(".items img").click(function() {    
            // see if same thumb is being clicked
            if (jQuery(this).hasClass("active")) { return; }

            // thumbnail URL
            var url = jQuery(this).attr("src").replace("?maxsidesize=40", "?maxsidesize=175");

            // change the image
            jQuery("a[rel^='prettyPhoto']").attr("href", url.replace("?maxsidesize=175", ""));
            jQuery("a[rel^='prettyPhoto'] img").fadeTo("fast", .5).attr("src", url).fadeTo("fast", 1);

            // activate item
            jQuery(".items img").removeClass("active");
            jQuery(this).addClass("active");  
        })


        if(jQuery("#plantImages .item li").length <= 3) {
            jQuery("a.left").addClass("disabled");
            jQuery("a.right").addClass("disabled");
        }
    }
    //---/ End Pretty Photo / Scrollable
    
    
    // News Fade
    if(jQuery("#News").length > 0) {
            jQuery('#News').innerfade({
            speed: 500,
            timeout: 8000,
            type: 'random',
            containerheight: '250px'
        });
    }
    //---/ End News Fade
    
    
    // Specials Fade
    if(jQuery("#sidebarContainer #featuredSpecials ul").length > 0) {
        jQuery('#sidebarContainer #featuredSpecials ul').innerfade({
            speed: 500,
            timeout: 6000,
            type: 'random',
            containerheight: '100px'
        });
    }
    //---/ End Specials Fade


    // hide the "Skip Payment" button"
    if(jQuery("input[id*='_cartElem_btnBack'][value='Skip payment']").length > 0) {
    	jQuery("input[id*='_cartElem_btnBack'][value='Skip payment']").hide();
    }

});

jQuery(window).resize(function() {
    resizeForSmallBrowsers();
});

