var video = null;
var tourScenes = null;
var stillScenes = null;
var slideshow = null;
var externalLinks = null
var jarLocation = null;
var myLocation = "";
sound = null;

if ( typeof ( soundManager ) != "undefined" ) {
	soundManager.url = "../Viewers/soundmanager/soundmanager2.swf";
	soundManager.debugMode = false;
}
var standardCopyright = "Copyright Arizona Imaging LLC. All Rights Reserved.";
var noJavaMessage = '';//'<div class="noJava"><br/><br/>This scene requires Java<sup><small>TM</small></sup> to view. Please click the the "Java Powered" image below to download and install Java<sup><small>TM</small></sup>.<br/><br/>Thank you for choosing Arizona Imaging!<br/><br/><a href="http://www.java.com?cid=2436"><img border="0" style="margin: 8px;" alt="Java Get Powered" title="Java Get Powered" src="http://java.com/im/get_powered_sm_ani.gif"></a></div>';

if(isMLS()) {
    jarLocation = "http://09media.azimaging.net/Viewers";
    myLocation = "http://09media.azimaging.net" + location.pathname;
} else {
    jarLocation = "../Viewers";
}

function ExternalLink(name, link) {
    this.name = name;
    this.link = link;
}

function Tour(id, address, city, state, zip, country, mlsid) {
    this.id = id;
    this.address = address;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.country = country;
    this.mlsid = mlsid;
    this.type = "RES"
}

function CommercialTour(id, address, city, state, zip, country, idText, showId, showAddress) {
    this.id = id;
    this.address = address;
    this.city = city;
    this.state = state;
    this.zip = zip;
    this.country = country;
    this.mlsid = idText;
    this.showId = showId;
    this.showAddress = showAddress;
    this.type = "COM";
}

function Agent(name, company, directPhone, officePhone, email, website, banner, agentPhoto) {
	if(isMLS()) {
    	this.name = 'Arizona Imaging  480-314-5862';
    } else {
		this.name = name;
	}
    this.company = company;
    this.directPhone = directPhone;
    this.officePhone = officePhone;
    this.email = email;
    this.website = website;
    this.banner = banner;
    this.agentPhoto = agentPhoto;
    this.name2 = name;
}

function Ipix360(name, filename, toolbar, spinspeed, spinstyle) {
    this.name = name;
    this.filename = filename;
    this.toolbar = toolbar;
    this.spinspeed = spinspeed;
    this.spinstyle = spinstyle;
    this.getApplet = generateIpix360Applet;
    this.width = "100";
    this.height = "100";
    this.splashfile = null;
    this.type = 'ipix';
}

function ExternalFrame(name, page) {
    this.name = name;
    this.page = page;
    this.getApplet = generateFrame;
    this.type = 'frame';
}

function Pano(name, assetFolder, configFile) {
    this.name = name;
    this.assetFolder = assetFolder;
    this.configFile = configFile;
    this.getApplet = generatePanoApplet;
    this.width = "100";
    this.height = "100";
    this.panoSkin = null;
    this.type = 'pano';
}

function StillPhoto(name, filename) {
    this.name = name;
    this.filename = filename;
    this.width = "100";
    this.height = "100";
    this.getImage = generateStillImage;
}

function TourScenes(width, height, splashfile, panoSkin) {
    this.imageArray = new Array();
    this.getContent = renderTourScenes;
    this.width = width;
    this.height = height;
    this.splashfile = splashfile;
    this.panoSkin = panoSkin;
}

function Slideshow(width, height) {
    this.imageArray = new Array();
    this.width = width;
    this.height = height;
    this.getContent = renderSlideshow;
    this.getApplet = generateSlideshowApplet;
}

function Video(videoFile, width, height) {
    this.videoFile = videoFile;
    this.width = width;
    this.height = height;
    this.getContent = renderVideo;
    this.getApplet = generateVideoApplet;
}

function NewVideo(videoFile, width, height) {
    this.videoFile = videoFile;
    this.width = width;
    this.height = height;
    this.getContent = renderNewVideo;
    this.getApplet = generateNewVideoApplet;
}

function addExternalLink(name, link) {
    if(externalLinks == null) {
        externalLinks = new Array();
    }
    externalLinks[externalLinks.length] = new ExternalLink(name, link);
}

function addSlideshowScene(slideshow, scene) {
    slideshow.imageArray[slideshow.imageArray.length] = scene;
}

function setSlideshowScenes(slideshow, stillScenes) {
	slideshow.imageArray = stillScenes.imageArray.slice();
}

function addPanoScene(tourScenes, scene) {
    scene.width = tourScenes.width;
    scene.height = tourScenes.height;
    if(scene.type == 'ipix') {
        scene.splashfile = tourScenes.splashfile;
    }
    if(scene.type == 'pano') {
        scene.panoSkin = tourScenes.panoSkin;
    }
    tourScenes.imageArray[tourScenes.imageArray.length] = scene;
}

function StillPhotoScenes(width, height) {
    this.imageArray = new Array();
    this.getContent = renderStillPhotoScenes;
    this.width = width;
    this.height = height;
}

function addStillScene(stillScenes, still) {
    still.width = stillScenes.width;
    still.height = stillScenes.height;
    stillScenes.imageArray[stillScenes.imageArray.length] = still;
}

function renderTourScenes() {
    var navigationCode = '';
    if(this.imageArray.length > 0) {
        navigationCode += '<div class="heading">Scenes</div>';
        for(var i = 0; i < this.imageArray.length; i++) {
            navigationCode += '<a href="#" onclick="switchPano('+i+'); return false;">'+this.imageArray[i].name+'</a><br/>';
        }
    }
    return navigationCode;
}

function renderStillPhotoScenes() {
    var navigationCode = '';
    if(this.imageArray.length > 0) {
        navigationCode += '<div class="heading">Still Photos</div>';
        for(var i = 0; i < this.imageArray.length; i++) {
            navigationCode += '<a href="#" onclick="switchStillPhoto('+i+'); return false;">'+this.imageArray[i].name+'</a><br/>';
        }
    }
    return navigationCode;
}

function renderSlideshow() {
    var navigationCode = '';
    navigationCode += '<div class="heading"><a href="#" onclick="showSlideshow(); return false;">Slideshow</a></div>';
    return navigationCode;
}

function renderVideo() {
    var navigationCode = '';
    navigationCode += '<div class="heading"><a href="#" onclick="showVideo(); return false;">Video</a></div>';
    return navigationCode;
}

function renderNewVideo() {
    var navigationCode = '';
    navigationCode += '<div class="heading"><a href="#" onclick="showVideo(); return false;">Video</a></div>';
    return navigationCode;
}

function switchPano(idx) {
    setDiv("image", tourScenes.imageArray[idx].getApplet());
    //if(!isJavaInstalled()) {
    //    setDiv("image", noJavaMessage);
    //}
    setDiv("copyright", standardCopyright);
}

function switchStillPhoto(idx) {
    setDiv("image", stillScenes.imageArray[idx].getImage());
    setDiv("copyright", standardCopyright);
}

function generateFrame() {
    var frameCode = '<iframe class="viewFrame" frameborder="0" marginheight="0" marginwidth="0" id="viewFrame" name="viewFrame" scrolling="no" src="'+this.page+'"/>';
    return frameCode;
}

function generateIpix360Applet() {
    var appletCode = '<applet archive="'+jarLocation+'/new_viewer/IpixViewer.jar" code="IpixViewer.class" name="myApplet" width="'+this.width+'" height="'+this.height+'">';
    appletCode += '<param name="url" value="'+this.filename+'"/>';
    if(this.toolbar != null) {
        appletCode += '<param name="toolbar" value="'+this.toolbar+'"/>';
    }
    if(this.spinspeed != null) {
        appletCode += '<param name="spinspeed" value="'+this.spinspeed+'"/>';
    }
    if(this.spinstyle != null) {
        appletCode += '<param name="spinstyle" value="'+this.spinstyle+'"/>';
    }
    if(this.splashfile != null) {
        appletCode += '<param name="splash" value="'+this.splashfile+'"/>';
    }
    //appletCode += noJavaMessage;
    appletCode += '</applet>';
    return appletCode;
}

function generatePanoApplet() {
    var appletCode = '<applet name="myApplet" codebase="." archive="'+jarLocation+'/studioviewer/studioviewer-csa.jar" code="StudioViewer.class" width="'+this.width+'" height="'+this.height+'" MAYSCRIPT>';
    appletCode += '<param name="Skin File URL" value="'+myLocation+this.panoSkin+'"/>';
    appletCode += '<param name="Media URL" value="'+myLocation+this.assetFolder+'"/>';
    appletCode += '<param name="Media File URL" value="'+myLocation+this.assetFolder+'/'+this.configFile+'"/>';
    appletCode += '<param name="MarkCode" value="740027"/>';
    appletCode += '<param name="POPUP Help" value="NO"/>';
    appletCode += '<param name="Audio Streaming Buffer Size" value="5"/>';
    appletCode += '<param name="Mute" value="NO"/>';
    appletCode += '<param name="Initial Panorama" value="1"/>';
    appletCode += '<param name="Panorama Memory Manager" value="1"/>';
    appletCode += '<param name="Flat Frame Rate" value="30"/>';
    appletCode += '<param name="Flat Play X Shift" value="1"/>';
    appletCode += '<param name="Flat Play Y Shift" value="1"/>';
    appletCode += '<param name="Flat Drag X Max Shift" value="10"/>';
    appletCode += '<param name="Flat Drag Y Max Shift" value="10"/>';
    appletCode += '<param name="Imm High Quality" value="YES"/>';
    appletCode += '<param name="Imm Frame Rate" value="30"/>';
    appletCode += '<param name="Imm Play X Shift" value="0.2"/>';
    appletCode += '<param name="Imm Play Y Shift" value="0.2"/>';
    appletCode += '<param name="Imm Drag X Max Shift" value="2.0"/>';
    appletCode += '<param name="Imm Play Y Max Shift" value="2.0"/>';
    appletCode += '<param name="Imm Zoom Step" value="1.0"/>';
    //appletCode += noJavaMessage;
    appletCode += '</applet>';
    return appletCode;
}

function generateStillImage() {
    var imageCode = '<img src="'+this.filename+'" height="'+this.height+'" width="'+this.width+'"/>';
    return imageCode;
}

function generateSlideshowApplet() {


    var appletCode = '<applet name="myApplet" archive="'+jarLocation+'/bookflip.jar" code="bookflip.class" width="'+this.width+'" height="'+this.height+'">';
    appletCode += '<param name="credits" value="Applet by Fabio Ciucci (www.anfyteam.com)">';
    appletCode += '<param name=regcode value="qkt7t5bjjtgqnfd6qeiihlwuhqk7gsg77j">';
    appletCode += '<param name=reglink value="NO">';
    appletCode += '<param name="res" value="1">';
    appletCode += '<param name="speed" value="7">';
    appletCode += '<param name="pause" value="1000">';
    appletCode += '<param name="extrah" value="0">';
    appletCode += '<param name="flipcurve" value="2">';
    appletCode += '<param name="shading" value="0">';
    appletCode += '<param name="backR" value="255">';
    appletCode += '<param name="backG" value="255">';
    appletCode += '<param name="backB" value="255">';
    appletCode += '<param name="overimg" value="NO">';
    appletCode += '<param name="overimgX" value="0">';
    appletCode += '<param name="overimgY" value="0">';
    appletCode += '<param name="regcode" value="NO">';
    appletCode += '<param name="regnewframe" value="YES">';
    appletCode += '<param name="regframename" value="_blank">';
    appletCode += '<param name="memdelay" value="1000">';
    appletCode += '<param name="priority" value="3">';
    appletCode += '<param name="MinSYNC" value="10">';
    for(var i = 0; i < this.imageArray.length; i++) {
        appletCode += '<param name="image'+(i+1)+'" value="'+this.imageArray[i].filename+'">';
        appletCode += '<param name="link'+(i+1)+'" value="NO">';
        appletCode += '<param name="statusmsg'+(i+1)+'" value="">';
        appletCode += '<param name="flip'+(i+1)+'" value="0">';
    }
    //appletCode += noJavaMessage;
    appletCode += '</applet>';
    return appletCode;
  
}

function generateVideoApplet() {
    var so = new SWFObject(this.videoFile, "tonopah", this.width, this.height, "8", "#FFF");
    so.addParam("movie", this.videoFile);
    so.addParam("quality", "high");
    so.addParam("play", "true");
    so.addParam("loop", "false");
    so.addParam("allowscriptaccess", "sameDomain");
    so.addParam("allowFullScreen", "true");
    so.addParam("scale","exactfit");
    so.write("image");
}

function generateNewVideoApplet() {
    var so = new SWFObject("../Viewers/mediaplayer/player.swf", "player", this.width, this.height, "9", "#FFF");
    so.addParam("quality", "high");
    so.addParam("play", "true");
    so.addParam("loop", "false");
    so.addParam("allowscriptaccess", "sameDomain");
    so.addParam("allowfullscreen", "true");
    so.addVariable("file","../"+this.videoFile);
    so.addVariable("autostart","true");
    so.write("image");
}

function generateStandardAddressBar(tour) {
    var addressBar = '';

    if(tour.type == "COM") {
        if(tour.showAddress) {
            addressBar += delimit(tour.address, ", ") + delimit(tour.city, " ") + delimit(tour.state, ' ') + delimit(tour.zip, ", ") + delimit(tour.country, "") + '<br/>';
        }
        if(tour.showId) {
            addressBar += tour.mlsid + '<br/>';
        }
    } else {
        addressBar += delimit(tour.city, ", ") + delimit(tour.state, " ") + delimit(tour.zip, ", ") + delimit(tour.country, "") + '<br/>';
        addressBar += 'MLS ID#: ' + tour.mlsid + '<br/>';
    }
    addressBar += '<br/>';
    addressBar += '<a class="underlined" href="/emailer.asp?link=/'+tour.id+(isMLS() ? '/?mls' : '')+'" target="_blank">Send to a Friend</a>';
    return addressBar;
}

function delimit(string, character) {
    if(string != null && string != "") {
        return string + character;
    } else {
        return "";
    }
}

function generateAgentBranding(agent, tour) {
    if(isMLS()) {
        return generateMLSAgentBranding(agent, tour);
    } else {
        return generateStandardAgentBranding(agent, tour);
    }
}

function generateStandardAgentBranding(agent, tour) {
    var agentBranding = '';
    if(tour != null && tour.type == "COM") {
        agentBranding += '<div class="heading">Presented By</div><br/>';
    } else {
        agentBranding += '<div class="heading">Listed By</div><br/>';
    }
        if(agent.agentPhoto != null) {
        agentBranding += '<img src="' + agent.agentPhoto + '"/><br/>';
        }
	if(agent.name2 != null) {
        agentBranding += '<strong>' + agent.name2 + '</strong><br/>';
	}
	if(agent.company != null) {
        agentBranding += agent.company + '<br/><br/>';
	}
	if(agent.directPhone != null) {
        agentBranding += 'Direct: ' + agent.directPhone + '<br/>';
	}
	if(agent.officePhone != null) {
        agentBranding += 'Office: ' + agent.officePhone + '<br/><br/>';
	}
	if(agent.email != null) {
        agentBranding += '<a class="underlined" href="mailto:'+agent.email+'">E-Mail Me</a><br/><br/>';
	}
	if(agent.website != null) {
        agentBranding += '<a class="underlined" href="'+agent.website+'" target="_blank">Visit My Website</a>';
	}
    return agentBranding;
}

function generateMLSAgentBranding(agent) {
    var agentBranding = '&nbsp;';
    return agentBranding;
}

function generateBanner(agent) {
    if(isMLS()) {
        return generateMLSBanner(agent);
    } else {
        return generateStandardBanner(agent);
    }
}

function generateStandardBanner(agent) {
    var agentBanner = '';
    if(agent.banner != null) {
        agentBanner = '<img src="'+agent.banner+'" alt="'+agent.company+'" border="0" height="100%" width="100%"/>';
    }
    return agentBanner;
}

function generateMLSBanner(agent) {
    return '<img src="../Images/BannersAndAgents/azimaging.jpg" alt="Banner" border="0" height="100%" width="100%"/>';
}

function setDiv(id, content) {
    var element = document.getElementById(id);
    element.innerHTML = content;
}

function displayNavigation() {
    var navigation = '<br/>';
    var initialSet = false;
    
    if(video != null && video.getContent() != '') {
        navigation += video.getContent();
        navigation += '<br/>';
        showVideo();
        initialSet = true;
    }
    
    if(tourScenes != null && tourScenes.getContent() != '') {
        navigation += tourScenes.getContent();
        navigation += '<br/>';
        if(!initialSet) {
            setDiv("image", tourScenes.imageArray[0].getApplet());
            //if(!isJavaInstalled()) {
            //    setDiv("image", noJavaMessage);
            //}
            initialSet = true;
        }
    }
    
    if(externalLinks != null) {
        for(var i = 0; i < externalLinks.length; i++) {
            var eLink = externalLinks[i];
            navigation += '<strong><a href="'+eLink.link+'" target="_blank">'+eLink.name+'</a><br/></strong>';
        }
        navigation += '<br/>';
    }
    
    if(slideshow != null && slideshow.getContent() != '') {
        navigation += slideshow.getContent();
        navigation += '<br/>';
        if(!initialSet) {
            showSlideshow();
            initialSet = true;
        }
     }   
        
    if(stillScenes != null && stillScenes.getContent() != '') {
        navigation += stillScenes.getContent();
        if(!initialSet) {
            setDiv("image", stillScenes.imageArray[0].getImage());
            initialSet = true;
        }
    }

    setDiv("navigation", navigation);
}

function showSlideshow() {
    setDiv("image", slideshow.getApplet());
    //if(!isJavaInstalled()) {
    //    setDiv("image", noJavaMessage);
    //}
    setDiv("copyright", standardCopyright);
}

function showVideo() {
    video.getApplet();
    setDiv("copyright", "Please be patient, the video may take a few moments to load.  <br/> Please turn the volume on your computer up to hear the narration. Thank you!<br/><br/>" + standardCopyright); 
}

function openURLShow(name, url, width, height, toolbar, location, directories, status, menubar, scrollbars, resizable, copyhistory){
  cad ="toolbar="+toolbar +",";
  cad+="location="+location+",";
  cad+="directories="+directories+",";
  cad+="status="+status+",";
  cad+="menubar="+menubar+",";
  cad+="scrollbars="+scrollbars+",";
  cad+="scrollbars="+scrollbars+",";
  cad+="resizable="+resizable+",";
  cad+="copyhistory="+copyhistory+",";
  cad+="width="+width+",";
  cad+="height="+height;
  window.open(url,name,cad);
}

function isMLS() {
    return window.location.search.indexOf('mls') == -1 ? false : true;
}

function isJavaInstalled() {
    if(document.myApplet==null) {
        return false;
    } else {
        return true;
    }
}

function playSound() {
	soundManager.play("music");
}

function pauseSound() {
	soundManager.pause("music");
}

function soundButtonOnClick(element) {
	if(!sound.paused) {
		element.value = "Play";
		element.src = "../Images/Icons/play.gif"
		setDiv("playmsg", " Play Sound");
		pauseSound();
	} else {
		element.value = "Pause";
		element.src = "../Images/Icons/pause.gif"
		setDiv("playmsg", " Pause Sound")
		playSound();
	}	
}