$j(function() {

	//--set number of seconds before slideshow transition
	var timer = 8 * 1000,
		clicked;
			
	//--AJAX call for getting xml file
	$j.ajax({
	    type: "GET",
	    url: xmlUrl,
	    dataType: "xml",
	    success: function(xml) {
	        var lastOccur = xmlUrl.lastIndexOf('=');
            var len = xmlUrl.length;
            var start = parseInt(xmlUrl.substring(lastOccur+1));
	    	var titleList = [],
		    	quoteList = [],
		    	statementList = [],
		    	imageList = [],
		    	moreLinkList = [],
                blurbList = [],
		    	$counter = 0
		    ;
	    	
	    	$j('galleryitem', xml).each(function() {
	    		var galleryitem = $j(this);
                var item;
                var productList = [], productlinkList = [];
				titleList.push($j('title', galleryitem).text());
				quoteList.push($j('quote', galleryitem).text());
				statementList.push($j('statement', galleryitem).text());
				imageList.push($j('image', galleryitem).text());
				moreLinkList.push($j('morelink', galleryitem).text());
				blurbList.push($j('blurb', galleryitem).text());
				$j('.paging ul').append('<li><a href="#" rel="' + ($counter + 1) + '"></a></li>');
				$counter++;
	        });
	        
			activateFirst(titleList, quoteList, statementList, imageList, moreLinkList,blurbList,start) // Activate first elements
			//rotateSwitch(titleList, quoteList, statementList, imageList, moreLinkList,blurbList); //Run slideshow
			
			//On Click
			$j(".paging a").bind('click', function() {
			    //--Activate the clicked paging
			    $active = $j(this);
			    //--Set clicked var to the current slide number
			    clicked = $active.attr('rel');
			    //--Stop the rotation
			    //clearInterval(play);
			    //--Trigger rotation immediately
			    rotate(titleList, quoteList, statementList, imageList, moreLinkList,blurbList);
			    //--Resume rotation timer
			   // rotateSwitch(titleList, quoteList, statementList, imageList, moreLinkList,blurbList);
			    //--Prevent browser jump to link anchor
			    return false;
			});
		},
	    error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});


	//--Slide switching and paging function
	rotate = function (title, quote, statement, image, moreLink, blurb) {
	    var curImgNum = $active.attr("rel") - 1; //Get number of the current active paging element
		
		//--Paging
		$j(".paging a").removeClass('active'); //Remove all active class
		$j(".paging a").parent().removeClass('active'); //Remove all active class
   		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
   		var parent = $active.parent();
   		parent.addClass('active');

        if(!isNaN(curImgNum)){
            //--Change slide content div
            $j('#slideshow div').css({'display' : 'none'}).html('<h1>' + quote[curImgNum] + '</h1><span>' + statement[curImgNum] + '</span>').fadeIn('slow');
            //--Change url of the moreLink
            $j('.link').bind('click', function() {
                var fullPath = moreLink[curImgNum];
                window.location = fullPath;
            });
            $j('#productimage').html('<img src="'+image[curImgNum]+'" alt="" width="103" hight="103" border="0">');
            var content = blurb[curImgNum];
            $j('#relatedproduct').html('<ul>'+content+'</ul>');
        }
        //--Unset clicked variable
		clicked = null;
	}
	
	//--Rotation and Timing Event
	/*rotateSwitch = function(title, quote, statement, image, moreLink,blurb){
	    play = setInterval(function(){ //Set timer
	        $active = $j('.paging a.active').parent().next().children('a'); //Move to the next paging link
	        if ( $active.length === 0) { //If paging reaches the end...
	            $active = $j('.paging li:first a'); //go back to first
	        }
	        rotate(title, quote, statement, image, moreLink,blurb); //Trigger the paging and slider function
	    }, timer); //Timer speed
	};*/
	
	//--Activate the first elements for the start and set some variables
	activateFirst = function(title, quote, statement, image, moreLink, blurb,start) {
		//$("#slideshow div:first").addClass("active-image");//--Activate the first image in the slideshow
		$j(".paging a:eq("+start+")").addClass("active");//--Activate the first paging link
		$j(".paging li:eq("+start+")").addClass("active");//--Activate the first paging link's parent li
		//--Change slide content div
		$j('#slideshow div').css({'display' : 'none'}).html('<h1>' + quote[start] + '</h1><span>' + statement[start] + '</span>').fadeIn('slow');
        //--Change url of the moreLink
		$j('.link').click(function() {
			var fullPath = moreLink[start];
			window.location = fullPath;
		});
        $j('#productimage').html('<img src="'+image[start]+'" alt="" width="103" hight="103" border="0">');
        var content = blurb[start];
        $j('#relatedproduct').html('<ul>'+content+'</ul>');
	};
				
});
