

function create_jsRotator(xmlFilePath,xmlFile,imagePath,targetDiv) {
	$(document).ready(function() {
		get_jsRotatorData(xmlFilePath,xmlFile,imagePath);
	});
}


function get_jsRotatorData(xmlFilePath,xmlFile,imagePath) {
    //create array of elements
    var rotatorObject = new Object();
    var rotatorList = new Object();
    var menuItems = new Object();
    var rotate = "false";
    $.ajax({
        type: "GET",
        url: '' + xmlFilePath + xmlFile,
        dataType: "xml",
        success: function(xml) {
        // Run the function for tags in the XML file
            $('doc', xml).each(function(i) {
                rotate = $(this).find("rotate").text();
            });
            $('rotator',xml).each(function(i) {

                var rotatorList = new Object();
                 rotatorList["list"] = i;
                 rotatorList["rimage"] = $(this).find("image").text();
                 rotatorList["imageLink"] = $(this).find("imageLink").text();
                 rotatorList["clickableImage"] = $(this).find("imageLink").attr("clickable");
                 rotatorList["bubbleType"] = $(this).find('bubble').attr('bubbleType');
                 rotatorList["fullyClosed"] = $(this).find('bubble').attr('fully_closed');
                 rotatorList["bubbleDisplay"] = $(this).find('bubble').attr('display');
                 rotatorList["windowStickerTitle"] = $(this).find("title").text();
                 rotatorList["menuItems"] = [];

                 $(this).find('menuItem').each(function(a) {
                   var menuItems = new Object();
                    menuItems["inlineImage"] = $(this).find('inlineImage').text();
                    menuItems["header"] = $(this).find('header').text();
                    menuItems["text"] = $(this).find('text').text();
                    menuItems["readMore"] = $(this).find('read_more').text();
                    menuItems["imageGallery"] = $(this).find('imageGalleryLink').text();
                    menuItems["videoGallery"] = $(this).find('videoGalleryLink').text();
                    //push Menu Items Array
                    rotatorList["menuItems"].push(menuItems);
                });//menuItem each
                rotatorList["footerDisplay"] = $(this).find('footer').attr('display');
                rotatorList["footerButtonDisplay"] = $(this).find('footer').find('button').attr('display');
                rotatorList["footerButtonText"] = $(this).find('footer').find('button').find('text').text();
                rotatorList["footerButtonLink"] = $(this).find('footer').find('button').find('link').text();
                //push to Rotator List Array to the full Rotator Object
                rotatorObject[i] = rotatorList;
            });//rotator each
            format_jsRotator(rotatorObject,imagePath,rotate);
        }//end success
    });//end ajax
}//end function

function format_jsRotator(rotatorData,imagePath,rotate) {
	
	var imageArray = new Object();
	var isFirst = false;
	
    var rotatorHTML = "<ul>";
    var bubbleHTML = "";
    for (x in rotatorData){
        //the window sticker
        if (rotatorData[x].bubbleType == "standard" && rotatorData[x].bubbleDisplay == "true"){
            if (rotatorData[x].fullyClosed == "false"){
				bubbleHTML = '<div class="heroMenuHover"><div class="podFade"><div class="component pod282 pod282_open" style="left:626px; top:26px;">';
            } else {
				bubbleHTML = '<div class="heroMenuHover"><div class="podFade"><div class="component pod282 pod282_closed" style="left:626px; top:26px;">';
            } // end fullyClosed
				bubbleHTML +=  '<div class="top"></div><div class="title"><h1>'+ rotatorData[x].windowStickerTitle + '</h1></div><div class="hr"></div>';
            //each menu Item
            for (a in rotatorData[x].menuItems){
                if (rotatorData[x].menuItems[a].readMore == "") {
                    bubbleHTML += '<div class="head_wrapper"><div class="heading"><a class="links" href="#">' + rotatorData[x].menuItems[a].header + '</a></div>';
                    bubbleHTML += '<div class="content"><p><span class="nolink_accordion_content_title">' + rotatorData[x].menuItems[a].header + '</span><br />';
                    bubbleHTML += rotatorData[x].menuItems[a].text + '</p>';
                }
                else {
                    if (rotatorData[x].menuItems[a].readMore == "#") { //is a video link
                        bubbleHTML += '<div class="head_wrapper"><div class="heading"><a class="links" href="#">' + rotatorData[x].menuItems[a].header + '</a></div>';
                        bubbleHTML += '<div class="content"><p><a id="' + rotatorData[x].menuItems[a].videoGallery + '" class="mediaPlayerStart" href="#" >' + rotatorData[x].menuItems[a].header + '</a><br />';
                        bubbleHTML += rotatorData[x].menuItems[a].text + '</p>';
                    }
                    else { //is a normal link
                        bubbleHTML += '<div class="head_wrapper"><div class="heading"><a class="links" href="#">' + rotatorData[x].menuItems[a].header + '</a></div>';
                        bubbleHTML += '<div class="content"><p><a href="' + linkPrefix + rotatorData[x].menuItems[a].readMore + '">' + rotatorData[x].menuItems[a].header + '</a><br />';
                        bubbleHTML += rotatorData[x].menuItems[a].text + '</p>';
                        bubbleHTML += '<div class="readmore_button_container"><span><a class="read_more" href="' + linkPrefix + rotatorData[x].menuItems[a].readMore + '">Read More</a></span></div>';
                    }
                }

                //Image Gallery Link?
                if (rotatorData[x].menuItems[a].imageGallery != "" || rotatorData[x].menuItems[a].videoGallery != ""){
                    bubbleHTML += '<div class="galleryLinkWrapper readmore_button_container">';
                    if (rotatorData[x].menuItems[a].imageGallery != ""){
                        bubbleHTML += '<a id="'+rotatorData[x].menuItems[a].imageGallery+'" class="mediaPlayerStart" href="#" >View Photos</a>';
                    }
                    //Video Gallery Link?
                    if (rotatorData[x].menuItems[a].videoGallery != ""){
                        bubbleHTML += '<span><a id="'+rotatorData[x].menuItems[a].videoGallery+'" class="mediaPlayerStart read_more" href="#" >View Videos</a></span>';
                    }
                    bubbleHTML += '</div>';
                }

                bubbleHTML += '</div></div>';
            }//end menuItems for

            //windowSticker button
            if (rotatorData[x].footerButtonLink != "" && rotatorData[x].footerDisplay == "true"){ //if there isnt a link defined, dont show the button
                bubbleHTML += '<div class="head_wrapper"><div class=""><a class="links" href="' + linkPrefix + rotatorData[x].footerButtonLink + '">' + rotatorData[x].footerButtonText + '</a></div></div>';
            } //end if footerbutton link exists


            bubbleHTML += '<div class="podfooter"></div>';
            bubbleHTML += '</div></div></div>';//closing tags
        }//end standard window sticker

        //Used on the motor trend page
        if (rotatorData[x].bubbleType == "bubbleB" && rotatorData[x].bubbleDisplay == "true"){
            bubbleHTML = '<div class="heroMenuHover"><div class="podFade"><div class="component pod282 pod282_closed bubbleB" style="left:626px; top:26px;">';
            bubbleHTML += '<div class="top"></div>';
            bubbleHTML += '<div class="title"><h1>'+ rotatorData[x].windowStickerTitle + '</h1></div>';
            bubbleHTML += '<div class="hr"></div>';
            for (a in rotatorData[x].menuItems){
                bubbleHTML += '<div class="head_wrapper">';

                //check for inlineImage
                if(rotatorData[x].menuItems[a].inlineImage != ""){
                    bubbleHTML += '<div class="content" style="height:200px">';
                    bubbleHTML += '<img src="'+ imagePath + rotatorData[x].menuItems[a].inlineImage+'" height="" width="" alt="'+rotatorData[x].menuItems[a].header+'" />';
                } else {
                    bubbleHTML += '<div class="content" style="height:70px">';
                }
                bubbleHTML += '<p><span class="nolink_accordion_content_title bubbleB">' + rotatorData[x].menuItems[a].header + '</span><br />';
                bubbleHTML += rotatorData[x].menuItems[a].text + '</p>';
                //Image Gallery Link?
                if (rotatorData[x].menuItems[a].imageGallery != "" || rotatorData[x].menuItems[a].videoGallery != ""){
                    bubbleHTML += '<div class="galleryLinkWrapper">';
                    if (rotatorData[x].menuItems[a].imageGallery != ""){
                        bubbleHTML += '<a id="'+rotatorData[x].menuItems[a].imageGallery+'" class="mediaPlayerStart" href="#" >View Photos</a>';
                    }
                    //Video Gallery Link?
                    if (rotatorData[x].menuItems[a].videoGallery != ""){
                        bubbleHTML += '<a id="'+rotatorData[x].menuItems[a].videoGallery+'" class="mediaPlayerStart" href="#" >View Videos</a>';
                    }
                    bubbleHTML += '</div>';
                }


                bubbleHTML += "</div></div>"; //content and head_wrapper
            } //end menuItems for loop
            bubbleHTML += '<div class="podfooter"></div>';
            bubbleHTML += "</div></div></div>";
        }

        if (rotatorData[x].bubbleType == "bubbleC" && rotatorData[x].bubbleDisplay == "true"){
            bubbleHTML = '<div class="heroMenuHover"><div class="podFade"><div class="component pod282 pod282_open bubbleC" style="left:626px; top:26px;">';

            //windowSticker button
            if (rotatorData[x].footerButtonLink != "" && rotatorData[x].footerDisplay == "true"){ //if there isnt a link defined, dont show the button
                bubbleHTML += '<div class="button"><span class="blue"><a class="blue" href="' + linkPrefix + rotatorData[x].footerButtonLink + '">' + rotatorData[x].footerButtonText + '</a></span><div class="clear"></div></div>';
            } //end if footerbutton link exists


            bubbleHTML += '</div></div></div>';//closing tags  heroMenuHover .podfade .component, etc
        }//end bubbleC window sticker

        var theImg = "";
        var linkId = get_linkNameRotator(rotatorData[x].rimage);
        if(!isFirst){
        	theImg = '<img id="img_'+linkId+'" src="'+imagePath + rotatorData[x].rimage +'" height="429px" width="950px" alt="'+rotatorData[x].rimage+'" />';
        	isFirst = true;
        }
    	var imageObject = new Object();
    	imageObject["imageURL"] = rotatorData[x].rimage;
    	imageObject["linkId"] = linkId;
    	imageArray["index"] = x;
    	imageArray[x] = imageObject;
    	
        //the main rotator image and its click-ability
        if (rotatorData[x].clickableImage == "true"){
        	rotatorHTML += '<li><a class="'+linkId+'" href="' + linkPrefix + rotatorData[x].imageLink + '">' + theImg + '</a>'+bubbleHTML+'</li>';
        } else {
            rotatorHTML += "<li>" + theImg + bubbleHTML + "</li>";
        }

        bubbleHTML = "";

    }//end for x in rotatorData


    rotatorHTML += "</ul>";
    $("#flash_content950 .rotator").empty().html(rotatorHTML);
	var imagesCount = $(".rotator").find("ul").children("li").size();
	if(imagesCount > 1){
		$(".heroButton").css("display","block");
		rotate = "true";
	}else{
		$(".heroButton").css("display","none");
		rotate = "false";
	};
	
	var firstTime = true;
    if(rotate == "true"){
     $(".rotator").jCarouselLite({
            btnNext: ".next",
            btnPrev: ".prev",
            speed: 700,
            visible: 1,
            beforeStart:function(){
            	if (firstTime) {
            		for (var i=0; i <= imageArray.index; i++) {
	            		var imageURL = imageArray[i].imageURL;
	            		var linkId = imageArray[i].linkId;
	            		var exist = $("img#img_"+linkId).length;
	            		if(exist == 0){
	            			var img = $("<img/>").attr({id: "img_"+linkId, src: imagePath + imageURL, height: '429px', width: '950px', alt: imageURL});
	            			$("a."+linkId).append(img);
	            		}
            		}
            		firstTime = false;
            	}
            }
        });
    } else {
        $(".rotator").css("margin-left", "0px");
    }

        var pod282_open = $(".rotator" + " .pod282_open").size();
            if (pod282_open !=0) {
                $(".rotator" + ' .pod282_open').css({'visibility':'visible', 'overflow':'hidden'});
                if($.browser.msie){
                    $(".rotator" + ' .pod282_open').accordion({
                        header: '.heading',
                        selectedClass: 'open',
                        event: 'click',
                        active: 0,
                        collapsible: false,
                        autoHeight: false,
                        clearStyles: true,
                        animated: false
                    });
                }
                else{
                    $(".rotator" + ' .pod282_open').accordion({
                        header: '.heading',
                        selectedClass: 'open',
                        event: 'click',
                        active: 0,
                        collapsible: false
                    });
                }
            }

        var pod282_closed = $(".rotator" + " .pod282_closed").size();
            if (pod282_closed !=0) {
                $(".rotator" + ' .pod282_closed').css({'visibility':'visible'});
                $(".rotator" + ' .pod282_closed').accordion({
                    header: '.heading',
                    selectedClass: 'open',
                    event: 'click',
                    active: false,
                    collapsible: true
                });
            }

        $('.handrail').hover(function() {
            $('.heroMenuHover').hide();
        }, function () {
            $('.heroMenuHover').fadeIn();
        });
		
        $('.rotator').on('click', "#img_950x430_BRZ-Homepage-Tile", function(){
		s=s_gi(s_account); 
		s.linkTrackVars="None"; 
		s.linkTrackEvents="None"; 
		s.tl(this,'o','subaru:la_auto_show_2011:click_to_brz_microsite'); 
		});

}

function get_linkNameRotator(image){
	var imageName = image;
    var lastDot = imageName.lastIndexOf(".");
    var linkId = imageName.substr(0, lastDot);
    return linkId; 
}
