
var $j = jQuery.noConflict();

$j(document).ready(function() {    
    loadBanner();
    prepSearch();
});


function prepSearch() {
    $j('#search_words').focus(function(){
        $j('#search_words').val('');
    });
    $j('#search_words').blur(function(){
        if($j('#search_words').val() == '')
            $j('#search_words').val('search');
    });
}

function slideSwitch() {
    var $jactive = $j('#slideshow IMG.active');

    if ( $jactive.length == 0 ) $jactive = $j('#slideshow IMG:last');

    var $jnext =  $jactive.next().length ? $jactive.next()
        : $j('#slideshow IMG:first');

    $jactive.addClass('last-active');

    $jnext.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $jactive.removeClass('active last-active');
        });
}

$j(function() {
    setInterval( "slideSwitch()", 3000 );
});

function startSlideShow(){
    setInterval( "slideSwitch()", 3000 );
}

/* -------------------------- */

function loadBanner() {
    var banners = new Array();
    
    var t = 'http://www.staunchclothing.co.uk/_assets/images/top_banner/';
    
    banners.push("tb_1.jpg");
    banners.push("tb_1_2.jpg");
    banners.push("tb_2.jpg");
    banners.push("tb_3.jpg");
    banners.push("tb_4.jpg");
    banners.push("tb_5.jpg");
    banners.push("tb_6.jpg");
    banners.push("tb_7.jpg");
    banners.push("tb_7_2.jpg");
    banners.push("tb_8.jpg");
    banners.push("tb_8_2.jpg");
    banners.push("tb_9.jpg");
    banners.push("tb_10.jpg");
    banners.push("tb_10_2.jpg");
    banners.push("tb_11.jpg");
    banners.push("tb_12.jpg");
    banners.push("tb_13.jpg");
    banners.push("tb_14.jpg");
    banners.push("tb_15.jpg");
    banners.push("tb_16.jpg");
    banners.push("tb_17.jpg");
    banners.push("tb_18.jpg");
    banners.push("tb_19.jpg");
    banners.push("tb_20.jpg");
    banners.push("tb_21.jpg");
    banners.push("tb_22.jpg");
    banners.push("tb_22_2.jpg");
    
    var index = Math.floor(Math.random()*(banners.length-1))
    var bgImage = banners[index];
    
    $j('#banner').css('background-image',"url('" + t+bgImage + "')");   
    
    // pre-load these images
    for(var i = 0; i<banners.length-1; i++){
        jQuery("<img>").attr("src", t+banners[i]);
    }
}


function equalHeights() {
    ///<summary>
    /// Sets the content and context columns 
    /// to the same height
    /// </summary>  
      
    // var contentHeight = $j("#content").height();
    // var contextHeight = $j("#context").height();

    // if (contentHeight > contextHeight) {
        // content is taller than context
        // $j("#context").height(contentHeight+44); 
    // } else {
        // context is taller than content
        // $j("#content").height(contextHeight-46); 
    // }
}

function selectMenu(el) {
    ///<summary>
    /// Adds the selected menu item class to an element by ID
    ///</summary>
    
    $j(el).addClass("selected");
}

function loadGallery(){
    ///<summary>
    /// Loads the thumbnail gallery to a list
    /// named thumb and adds the onclick image swap
    ///</summary> 
        
    $j("ul.thumb li").hover(function() {
	    $j(this).css({'z-index' : '10'});
	    $j(this).find('img').addClass("hover").stop()
		    .animate({
			    marginTop: '-90px', 
			    marginLeft: '-60px', 
			    top: '30%', 
			    left: '30%', 
			    width: '100px',     // width
			    height: '100px',    // height
			    padding: '12px' 
		    }, 200);
    	
	    } , function() {
	    $j(this).css({'z-index' : '0'});
	    $j(this).find('img').removeClass("hover").stop()
		    .animate({
			    marginTop: '0', 
			    marginLeft: '0',
			    top: '0', 
			    left: '0', 
			    width: '50px', 
			    height: '50px', 
			    padding: '5px'
		    }, 400);
    });
     
    //Swap Image on Click
	$j("ul.thumb li a").click(function() {		
		var mainImage = $j(this).attr("href"); //Find Image Name
		$j("#single_image").attr({ href: mainImage });
		$j("#bigPic img").attr({ src: mainImage });
		return false;		
	});
 
}

function loadProductListMods(){
    ///<summary>
    /// Load the product image list mods
    ///</summary>
    
    $j("#products ul li").hover(function() {
        $j(this).find('.prodPrice').fadeTo(300, 0);       
    }, function(){
        $j(this).find('.prodPrice').fadeTo(300, 1.0);
    });
}

function formatDate(d, f) {
    ///<summary>
    /// format a date as a string
    ///</summary>
    
    var d = new Date(d);
    var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi,
      function($1) {
          switch ($1.toLowerCase()) {
          case 'yyyy': return d.getFullYear();
          case 'mmmm': return months[d.getMonth()];
          case 'mmm':  return months[d.getMonth()].substr(0, 3);
          case 'mm':   return (d.getMonth() + 1);
          case 'dddd': return days[d.getDay()];
          case 'ddd':  return days[d.getDay()].substr(0, 3);
          case 'dd':   return d.getDate();
          case 'hh':   return ((h = d.getHours() % 12) ? h : 12);
          case 'nn':   return d.getMinutes();
          case 'ss':   return d.getSeconds();
          case 'a/p':  return d.getHours() < 12 ? 'a' : 'p';
          }
      }
  );
}

function initFeed(feedURL, count, elID) {
    ///<summary>
    /// Load a number of items from an rss feed as a
    /// child list of an element.
    /// Includes the hiding of a #loading element
    ///</summary>
    
    var feed = new google.feeds.Feed(feedURL);
    feed.setNumEntries(count);
    feed.load(function(result) {

        // if(result.error) {alert('feed load errer');};

          var list = document.getElementById(elID); 

          for(var i = 0; i< result.feed.entries.length; i++)
          {                
            var entry = result.feed.entries[i];
            
            var link = document.createElement('a');
                link.setAttribute('href', entry.link);
                link.appendChild(document.createTextNode(entry.title));
                
            var title = document.createElement('h3');
                title.appendChild(link);
                
            var date = document.createElement('p');
                date.setAttribute('class', 'blog_date');
                date.appendChild(document.createTextNode(formatDate(entry.publishedDate, 'mmmm dd, yyyy')));
                
            var content = document.createElement('div');
                content.innerHTML = entry.content;
                
            var li = document.createElement('li');
                li.appendChild(title);
                li.appendChild(date);
                li.appendChild(content);                    
                list.appendChild(li);
          } 
          $j('#loading').hide();
    });
}

function initSummary(feedURL, elID) {
    ///<summary>
    /// Load a number of items from an rss feed as a
    /// child list of an element.
    /// Includes the hiding of a #loading element
    ///</summary>
    
    var count = 1;
    var feed = new google.feeds.Feed(feedURL);
    feed.setNumEntries(count);
    feed.load(function(result) {

        // if(result.error) {alert('feed load errer');};

          var list = document.getElementById(elID); 

          if(result.feed.entries.length > 0)
          {                
            var entry = result.feed.entries[0];
                            
            var content = document.createElement('div');
                content.innerHTML = entry.content;
                
            var li = document.createElement('li');
                li.appendChild(content);                    
                list.appendChild(li);
          } 
          $j('#loading').hide();
    });
}