﻿(function() {

   "use strict"

	/// General scripts for day to day running on site.
	var Peach = {}; 

	Peach = {

		/// <summary> 
		/// Utilizes jQuery feature detection to return the Internet Explorer browser number.
		/// </summary> 
		GetIEVersion: function () {
			var $browser = 0;
			// The browser is IE 6 - 8.
			if (!jQuery.support.leadingWhitespace) {

				// IE 6 & 7.
				if (!jQuery.support.boxModel) {

					!jQuery.support.opacity && !window.XMLHttpRequest ? $browser = 6 : $browser = 7;
				}
				else {
					$browser = 8;
				}
			}
			return $browser;
		},

		/// <summary> 
		/// Create our banners and set time expirations by creating conditional statements
		/// </summary> 
		CreateBanner: function () {
    
    var bannerDetails = {},
        today = new Date(),
        todayFormatted = [
            today.getFullYear(),
            "-",
            (today.getMonth() + 1),
            "-",
            today.getDate(),
            "-",
            today.getHours(),
            today.getMinutes()
        ].join("");

    if (todayFormatted < "2011-12-1-1200") {
        //assign our banner details in this array 
        bannerDetails = {
            banners: [
                {
                        url: "http://www.tractorland.co.uk/Kids-Tractor-Clothes/Tractor-Ted-Wellies.aspx",
                        image:"images/offers_banners/SP1-wellies.jpg",
                        text:"Wellies"
                },                                            
                {
                    url: "http://www.tractorland.co.uk/Kids-Tractor-Ted-gifts/Breakfast-Set.aspx",
                        image:"images/offers_banners/600x300px-break&tin.jpg",
                        text:"New Breakfast set - Buy Now"
                },                                            
                {
                        url: "http://www.tractorland.co.uk/Kids-Tractor-Dvds/Tractor-Ted-Mighty-Maize-Machine.aspx",
                        image:"images/offers_banners/new_dvd.jpg",
                        text:"Mighty Maize"
                }                                                                 
        ]};
    }
	
	 if (todayFormatted > "2011-12-1-1200") {
        //assign our banner details in this array 
        bannerDetails = {
            banners: [		
               {
                        url: "http://www.tractorland.co.uk/kids-tractor-shop.aspx",
                        image:"images/offers_banners/TRA_Homepage-Banner_Free-Mug_v01.jpg",
                        text:"Free Mug"
                },    			
                {
                        url: "http://www.tractorland.co.uk/Kids-Tractor-Clothes/Tractor-Ted-Wellies.aspx",
                        image:"images/offers_banners/SP1-wellies.jpg",
                        text:"Wellies"
                },                                            
                {
                    url: "http://www.tractorland.co.uk/Kids-Tractor-Ted-gifts/Breakfast-Set.aspx",
                        image:"images/offers_banners/600x300px-break&tin.jpg",
                        text:"New Breakfast set - Buy Now"
                },                                            
                {
                        url: "http://www.tractorland.co.uk/Kids-Tractor-Dvds/Tractor-Ted-Mighty-Maize-Machine.aspx",
                        image:"images/offers_banners/new_dvd.jpg",
                        text:"Mighty Maize"
                }                                                      
        ]};
    }
	
	if (todayFormatted > "2012-01-04-1200") {
        //assign our banner details in this array 
        bannerDetails = {
            banners: [	

                {
                        url: "http://www.tractorland.co.uk/Kids-Tractor-Shop.aspx",
                        image:"images/offers_banners/film_star.jpg",
                        text:"Wellies"
                },                
				 			
               
				{
                        url: "/Kids-Tractor-Dvds-and-Books/Tractor-Ted-in-Wintertime-DVD.aspx",
                        image:"images/offers_banners/TRA_Homepage-Banner_wintertimeDVD-Offer.jpg",
                        text:"DVD"
                }				
                                                                 
        ]};
    }

    
	
	//append each banner to carousel
			$.each(bannerDetails.banners, function(key,val){

				var imgsrc,
					linkUrl,
					altTxt,
					$img,
					$anch,
					$list,
					$imgLocation = jQuery('#carousel ul');

					//assign the data for each image in the array
					imgsrc = val.image;
					linkUrl = val.url;
					altTxt = val.text;

					// Create your image tag in memory so we are not hitting the DOM.
					$img = jQuery('<img/>').attr({
						src: imgsrc,
						alt: altTxt,
						border: 0
					});

					// Create your anchor tag in memory. Note we've chained our constructor.
					$anch = jQuery('<a/>').attr({
						href: linkUrl
					});
					
					//create the list item to add to the unordered list
					$list = jQuery('<li/>');

					// Append the items to their respective parents. Voila we hit the Dom only once.
					$anch.append($img);
					$list.append($anch);
					$imgLocation.append($list);

			});

		},
		
		startFlash: function() {

			var flashObjects = {
					"flashcontainer": {
						"width": "600px",
						"height": "300px",
						"url": "/media/flash/homebanner/apollo_promo.swf",
						"flashVars" : 'xmlLink=/media/flash/homebanner/apollo.xml'
					},
					"smallPlayer1": {
						"width": "280px",
						"height": "190px",
						"url": "/VideoPlayer/player_1.swf?videoNum=0",
						"flashVars" : ''
					},
					
					"smallPlayer2": {
						"width": "280px",
						"height": "190px",
						"url": "/VideoPlayer/player_1.swf?videoNum=5",
						"flashVars" : ''
					}
				};		
								
				//our each function
				
				$.each(flashObjects, function(key, value){
				
				//console.log(value.url);
				
					var divName = key,
						$movie,
						$pMovie,
						$pVars,
						$pHeight,
						$pWidth;
						
						$pMovie = value.url;
						$pVars = value.flashVars;
						$pHeight = value.height;
						$pWidth = value.width;
						
						$movie = $("#" + divName);
						
						//using the jQuery SWFObject plugin
						$($movie).flash(
							{
								// test_flashvars.swf is the flash document
								swf : $pMovie,
								// these arguments will be passed into the flash document
								flashvars : $pVars,
								height : $pHeight,
								width : $pWidth
							}
						);					
						
									
				});		
	},

		/// <summary> 
		/// Creates the slide in and out effect for the banners
		/// </summary> 
		BannerRotation: function () {

			var $carousel = $("#carousel"),
				myArray = $("ul li", $carousel),
				regWidth = $carousel.width(),
				combinedWidth = 0,
				$img = $("ul li img", $carousel),
				$imgList = $('ul', $carousel),
				imgWidth = $img.width(),
				imgHeight = $img.height(),
				len = 0,
				callback = function () {
					jQuery("#carousel ul").stop().animate({
						marginLeft: -(regWidth)
						}, 1,
									function () {
										var $this = jQuery(this);
										//after animation completes do this
										$this.find("li:last").after($this.find("li:first"));
										$this.animate({
											marginLeft: 0
										}, "slow");
									});
				};
									
			

			//begin code to get length of unordered list items - gallery fade/scroll images
			len = myArray.length;

			while (len--) {
				combinedWidth = combinedWidth + $(myArray[len]).width();
			}			
		
			//set the total width of the carousel
			$imgList.css("width", combinedWidth);


			$imgList.each(function () {
				//set interval for X-seconds
				var t = setInterval(callback, 8500);
			});

		}
		

	};

	/// <summary> 
	/// Launch the scripts when all elements are loaded in the DOM.
	/// </summary>
	jQuery(document).ready(function () {

		Peach.CreateBanner();
		Peach.BannerRotation();
	    //Peach.startFlash();

	});
	
	jQuery(window).load(function(){
	
		//Peach.startFlash();
		
	});

})();
