﻿function AlterExternalLinkTargets()
{
    $("#layoutContainer a[rel='external']").attr("target", "_blank");
}

function RetrievePhotoGalleryImages()
{
	$.ajax({
	   type: "GET",
	   url: "http://www.japensgroomingsalon.com/handlers/image-gallery-handler.ashx?ic=10",
	   dataType: "xml",
	   cache: false,
	   contentType: "text/xml",
	   success: function(data)
	   {
			var images = $(data).find("image");
			var html = [];
			
			images.each(function(index, image)
			{
				html.push('<img class="shadow" width="110" height="73" alt="Shop Picture" src="/images/shop-pictures/group1/' + $(image).find("tn").text() + '" />')
			});		
			
			$("#petPhotosContainer a.petPhotosGalleryLink:first").html(html.join('')).cycle({timeout:5000,random:true});	 
	   },
	   error: function(xhr, status, error) 
	   {
	     alert(xhr.status);
	   }
	});
	
}

function ParseAnimalNewsFeed()
{	
	var date = new Date(); // grabbing the date as of this second it gets run
	$.ajax({
	   type: "GET",
	   url: "http://www.japensgroomingsalon.com/handlers/feed-handler.ashx?_=" + date.getTime(), //add the time stamp and specified format to the end
	   dataType: "json",
	   cache: false,
	   contentType: "application/json",
	   success: function(data) {
            var articles = data["articles"];

            if (articles != null)
            {
                var html = [];

                var countArticles = articles.length;				

                for (var i = 0; i < countArticles; i++)
                {
                    var article = articles[i];

                    html.push('<div class="newsItem"><p class="title"><a href="' + article.url + '" title="Read More:  ' + article.title + '" rel="external">' + article.title + '</a>  <span class="pubDate">' + article.publish_date + '</span></p><div class="summary"><p class="summaryHeader">Summary:</p>' + article.summary + '</div></div>');
                }

				var newsFeedContainer = $('#newsFeedContainer');

		        newsFeedContainer.html(html.join('')).children('div').css('opacity', 0);
		        newsFeedContainer.cycle({timeout:8000,random:true});

                AlterExternalLinkTargets();
            }
	   },
	   error: function(xhr, status, error) 
	   {
	     alert(xhr.status);
	   }
	});
}

$(document).ready(function ()
{
    var currentPage = (location.pathname == "/") ? ('/default.aspx') : (location.pathname);

    $("#cph div.navigation a[href$='" + currentPage + "']").addClass('currentPage');
    
    var announcementGallery = $('#announcementGallery');
    
    //announcementGallery.children('div').css('opacity', 0);
    announcementGallery.cycle({timeout:6000,speed:2000});
	
	RetrievePhotoGalleryImages();

	if (currentPage != 'animal-news-articles.aspx')	
	{
		ParseAnimalNewsFeed();
	}   

    AlterExternalLinkTargets();
});
