function Media(count) {
    this.items = undefined;
    this.start = 0;
    this.display_count = count;
    this.selected = -1;
    this.last_selected = -1;
    
}

Media.prototype.select = function(index) {
    this.last_selected = this.selected;
    this.selected = index;
};

Media.prototype.prev = function() {
    if (this.start > 0) {
        var new_start = this.start - this.display_count;
        if (new_start >= 0) {
            this.start = new_start;
        } else {
            this.start = 0;
        }
        return true;
    }
    return false;
};

Media.prototype.next = function() {
    var last_start = this.items.length - this.display_count;
    var new_start = this.start + this.display_count;

    if (new_start < last_start) {
        this.start = new_start;
    } else {
        this.start = last_start;
    }
};

Media.prototype.has_next = function() {
    return (this.start < this.items.length-this.display_count);
};

var videos = new Media(6);
videos.items = [
    { img: "pi_latimes.png", 
      title:"PiCycle electric bike LA Times review", 
      desc: "PiCycle electric bike LA Times review by Susan Carpenter, Throttle Jockey.", 
      code: "&nbsp;<embed type='application/x-shockwave-flash' salign='l' flashvars='&amp;titleAvailable=true&amp;playerAvailable=true&amp;searchAvailable=false&amp;shareFlag=N&amp;singleURL=http://latimes.vidcms.trb.com/alfresco/service/edge/content/72ddaf0f-2ab8-4c7e-ae06-b2e8deb36b6f&amp;propName=latimes.com&amp;hostURL=http://www.latimes.com&amp;swfPath=http://latimes.vid.trb.com/player/&amp;omAccount=tribglobal&amp;omnitureServer=latimes.com' allowscriptaccess='always' allowfullscreen='true' menu='true' name='PaperVideoTest' bgcolor='#ffffff' devicefont='false' wmode='transparent' scale='showall' loop='true' play='true' pluginspage='http://www.macromedia.com/go/getflashplayer' quality='high' src='http://latimes.vid.trb.com/player/PaperVideoTest.swf' align='middle' height='340' width='560'></embed>" },
    { img: "http://i2.ytimg.com/vi/eKZoqsp24lU/default.jpg", 
      title:"Welcome: Pi Mobility Electric Bikes", 
      desc: "Welcome: Pi Mobility Electric Bikes", 
      src: 'http://www.youtube.com/v/eKZoqsp24lU&hl=en&fs=1&' },
    { img: "http://i1.ytimg.com/vi/HNDX8pUFyMU/default.jpg", 
      title:"Pi Mobility is an Electric Transportation Company", 
      desc: "Pi Mobility is an Electric Transportation Company ", 
      src: 'http://www.youtube.com/v/HNDX8pUFyMU&hl=en&fs=1&' },
    { img: "http://i4.ytimg.com/vi/wUSbAiPa110/default.jpg", 
      title:"History: From bikes to Electric Bikes", 
      desc: "History: From bikes to Electric Bikes", 
      src: 'http://www.youtube.com/v/wUSbAiPa110&hl=en&fs=1&' },
    { img: "http://i3.ytimg.com/vi/fGzH0XnUOiE/default.jpg", 
      title:"Origin of idea: The Perfect Electric Bike Design", 
      desc: "Origin of idea: The Perfect Electric Bike Design", 
      src: 'http://www.youtube.com/v/fGzH0XnUOiE&hl=en&fs=1&' },
    { img: "http://i4.ytimg.com/vi/GWtx7XF497A/default.jpg", 
      title:"This is Pi - A Lightweight Electric Bike", 
      desc: "This is Pi - A Lightweight Electric Bike", 
      src: 'http://www.youtube.com/v/GWtx7XF497A&hl=en&fs=1&' },
    { img: "http://i3.ytimg.com/vi/JEDeUf1OeMA/default.jpg", 
      title:"This is Picycle - A Lightweight Hybrid Electric Bike", 
      desc: "This is Picycle - A Lightweight Hybrid Electric Bike", 
      src: 'http://www.youtube.com/v/JEDeUf1OeMA&hl=en&fs=1&' },
    { img: "http://i2.ytimg.com/vi/EielG6pzNpg/default.jpg", 
      title:"This is Picycle a New Animal - A Lightweight Hybrid Electric Bike ", 
      desc: "This is Picycle a New Animal - A Lightweight Hybrid Electric Bike ", 
      src: 'http://www.youtube.com/v/EielG6pzNpg&hl=en&fs=1&' },
    { img: "http://i2.ytimg.com/vi/qVPNRU58BQ0/default.jpg", 
      title:"Why the Arch for an Electric Bike? ", 
      desc: "Why the Arch for an Electric Bike? ", 
      src: 'http://www.youtube.com/v/qVPNRU58BQ0&hl=en&fs=1&' },
    { img: "http://i1.ytimg.com/vi/tDCTS35OD2w/default.jpg", 
      title:"Drop off roof - Will the Electric Bike Frame Bend?", 
      desc: "Drop off roof - Will the Electric Bike Frame Bend?", 
      src: 'http://www.youtube.com/v/tDCTS35OD2w&hl=en&fs=1&' },
    { img: "http://i2.ytimg.com/vi/1zinwTg5z_0/default.jpg", 
      title:"Electric Bike Engineering for Dogs", 
      desc: "Electric Bike Engineering for Dogs", 
      src: 'http://www.youtube.com/v/1zinwTg5z_0&hl=en&fs=1&' },
    { img: "http://i3.ytimg.com/vi/Rmou8PnNEro/default.jpg", 
      title:"Drag Racing Electric Bikes", 
      desc: "Drag Racing Electric Bikes", 
      src: 'http://www.youtube.com/v/Rmou8PnNEro&hl=en&fs=1&' },
    { img: "http://i2.ytimg.com/vi/AFrMAfmvdSs/default.jpg", 
      title:"Picycle Undercarriage of the Electric Bike", 
      desc: "Picycle Undercarriage of the Electric Bike", 
      src: 'http://www.youtube.com/v/AFrMAfmvdSs&hl=en&fs=1&' },
    { img: "http://i3.ytimg.com/vi/v1BJKasOW6A/default.jpg", 
      title:"Philosophy of Pi's Electric Bike Design", 
      desc: "Philosophy of Pi's Electric Bike Design", 
      src: 'http://www.youtube.com/v/v1BJKasOW6A&hl=en&fs=1&' },
    { img: "http://i1.ytimg.com/vi/Xi2C2KTX-aU/default.jpg", 
      title:"Accessories for Pi's Electric Bike", 
      desc: "Accessories for Pi's Electric Bike", 
      src: 'http://www.youtube.com/v/Xi2C2KTX-aU&hl=en&fs=1&' },
    { img: "http://i1.ytimg.com/vi/0vjBox02FVM/default.jpg", 
      title:"Brushless Electric Motor for Pi's Electric Bike", 
      desc: "Brushless Electric Motor for Pi's Electric Bike", 
      src: 'http://www.youtube.com/v/0vjBox02FVM&hl=en&fs=1&' },
    { img: "http://i3.ytimg.com/vi/RIYwewQ8iFw/default.jpg", 
      title:"Arch construction for Pi's Electric Bike ", 
      desc: "Arch construction for Pi's Electric Bike ", 
      src: 'http://www.youtube.com/v/RIYwewQ8iFw&hl=en&fs=1&' },
    { img: "http://i1.ytimg.com/vi/T4mohs-MV_s/default.jpg", 
      title:"Compare Pi and Picycle Electric Bikes", 
      desc: "Compare Pi and Picycle Electric Bikes", 
      src: 'http://www.youtube.com/v/T4mohs-MV_s&hl=en&fs=1&' }];

function show_video_clips() {

    var html = "<div class='left_nav'>";
    if (videos.start > 0) {
        html += "<a class='video_clip_nav' href='' onclick='return prev_clip()'><</a> ";
    } else {
        html += "&nbsp;";
    }
    html += "</div>";
    for (var i=0; i < videos.display_count; i++) {
        var j = i + videos.start;
        var clip_css = j==videos.selected ? "video_clip_selected" : "video_clip";
        var img_src = videos.items[j].img;
        if (img_src.indexOf("http://") === -1) {
            img_src = "images/" + img_src;
        }
        var title = videos.items[j].title;
        html += "<div id='video_clip"+j+"' class='" + clip_css+ "'>";
        html += "<div class='video_clip_inner'>";
        html += "<a href='' onclick='return show_video("+j+")' title=\""+title+"\"><img src='"+ img_src +"' width='90' border='0'></a>\n";
        html += "</div></div>\n";
    }
    if (videos.has_next()) {
        html += " <a class='video_clip_nav' href='' onclick='return next_clip()' style='padding-left:8px'>></a>";
    }
    html += "<div style='clear:both'></div>";

    document.getElementById("video_clips").innerHTML = html;

    if (videos.selected == -1) {
        show_video(0);
    }
}

function show_video(index) {
    videos.select(index);
    
    var clip = videos.items[index];
    var title = clip.title;
    var desc = clip.desc;
    if (clip.src) {
        var src = clip.src + "autoplay=1";
        //var code = '<object width="560" height="340"><param name="movie" value="' + src +'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="quality" value="high"></param><embed src="' + src+ '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" quality="high" width="560" height="340"></embed></object>';
        var code = '<embed src="' + src+ '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" quality="high" width="560" height="340"></embed>';
    } else {
        var code = clip.code;
    }
    var info = "<div><span class='red'>NOW PLAYING</span> <b>" + title + "</b></div>\n";
    info += "<div>"+desc+"</div>\n";
    document.getElementById("main_video_meta").innerHTML = info;
    document.getElementById("main_video").innerHTML = code;

    var elem = document.getElementById("video_clip"+videos.last_selected);
    if (elem) {
        elem.className = "video_clip";
    }
    elem = document.getElementById("video_clip"+index);
    if (elem) {
        elem.className = "video_clip_selected";
    }
    return false;
}

function prev_clip() {
    if (videos.prev()) {
        show_video_clips();
    }
    return false;
}

function next_clip() {
    videos.next();

    show_video_clips();

    return false;
}

//------------------------------------------------------------

var photos = new Media(12);
photos.items = [
    { img: "../images/gallery/thumb/_MG_3094.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle Limited ready for paint and assembly", 
      src: '../images/gallery/_MG_3094.jpg' },
    { img: "../images/gallery/thumb/_MG_3097.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle Limited fender detail", 
      src: '../images/gallery/_MG_3097.jpg' },
    { img: "../images/gallery/thumb/_MG_3101.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Steering Head assembly detail", 
      src: '../images/gallery/_MG_3101.jpg' },
    { img: "../images/gallery/thumb/_MG_3102.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle Limitied in the sun", 
      src: '../images/gallery/_MG_3102.jpg' },
    { img: "../images/gallery/thumb/_MG_3104.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle Limited two speed motor ready to roll", 
      src: '../images/gallery/_MG_3104.jpg' },
    { img: "../images/gallery/thumb/_MG_3108.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle Limited two speed motor ready to roll", 
      src: '../images/gallery/_MG_3108.jpg' },
    { img: "../images/gallery/thumb/_MG_3112.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle innovative front end suspension detail", 
      src: '../images/gallery/_MG_3112.jpg' },
    { img: "../images/gallery/thumb/_MG_3115.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Pi dashboard detail and recharge port", 
      src: '../images/gallery/_MG_3115.jpg' },
    { img: "../images/gallery/thumb/_MG_3118.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle Limited adjustable seat", 
      src: '../images/gallery/_MG_3118.jpg' },
    { img: "../images/gallery/thumb/_MG_3125.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Pi factory show room", 
      src: '../images/gallery/_MG_3125.jpg' },
    { img: "../images/gallery/thumb/_MG_3131.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Pi wheel lacing in the work shop", 
      src: '../images/gallery/_MG_3131.jpg' },
    { img: "../images/gallery/thumb/_MG_3132.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Hub motor wheel lacing", 
      src: '../images/gallery/_MG_3132.jpg' },
    { img: "../images/gallery/thumb/_MG_3136.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Brushless motor tech inspection", 
      src: '../images/gallery/_MG_3136.jpg' },
    { img: "../images/gallery/thumb/_MG_3140.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Pi shop chain rings ready for installation", 
      src: '../images/gallery/_MG_3140.jpg' },
    { img: "../images/gallery/thumb/_MG_3142.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Pi hub motor and wheel inspection", 
      src: '../images/gallery/_MG_3142.jpg' },
    { img: "../images/gallery/thumb/_MG_3147.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Tireless tires in the Pi shop", 
      src: '../images/gallery/_MG_3147.jpg' },
    { img: "../images/gallery/thumb/_MG_3149.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Brushless motor interior for Pi electric bike hub motor", 
      src: '../images/gallery/_MG_3149.jpg' },
    { img: "../images/gallery/thumb/_MG_3154.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Pi arched forks, suspension (shocks) and front brake rotor for preformance", 
      src: '../images/gallery/_MG_3154.jpg' },
    { img: "../images/gallery/thumb/_MG_3168.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Front suspension detail, rotor brake and Pi fork", 
      src: '../images/gallery/_MG_3168.jpg' },
    { img: "../images/gallery/thumb/_MG_3187.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Two speed motor for Pi electric bikes and motorcycles for speed and hill preformance", 
      src: '../images/gallery/_MG_3187.jpg' },
    { img: "../images/gallery/thumb/_MG_3191.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Close up of the PiCycle Limited electric bike frame", 
      src: '../images/gallery/_MG_3191.jpg' },
    { img: "../images/gallery/thumb/_MG_3198.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Front view of PiCycle Limited electric bike frame", 
      src: '../images/gallery/_MG_3198.jpg' },
    { img: "../images/gallery/thumb/_MG_3211.jpg", 
      title:"Pi Electric Bikes", 
      desc: "PiCycle Limited electric bike ready to ride", 
      src: '../images/gallery/_MG_3211.jpg' },
    { img: "../images/gallery/thumb/_MG_3247.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Testing the PiCycle electric bike", 
      src: '../images/gallery/_MG_3247.jpg' },
    { img: "../images/gallery/thumb/_MG_3260.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Me (Marcus Hays) happy in the studio preparing a bike for one more happy customer", 
      src: '../images/gallery/_MG_3260.jpg' },
    { img: "../images/gallery/thumb/_MG_3270.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Constantly working for precision and design simplicity", 
      src: '../images/gallery/_MG_3270.jpg' },
    { img: "../images/gallery/thumb/_MG_3279.jpg", 
      title:"Pi Electric Bikes", 
      desc: "After years of development and engineering we continue to strive to perfect the arch for our bike frames", 
      src: '../images/gallery/_MG_3279.jpg' },
    { img: "../images/gallery/thumb/_MG_3284.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Comfort, performance and safely are key to our philosophy", 
      src: '../images/gallery/_MG_3284.jpg' },
    { img: "../images/gallery/thumb/_MG_3294.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Each Pi is designed for bike racks and other accessories", 
      src: '../images/gallery/_MG_3294.jpg' },
    { img: "../images/gallery/thumb/_MG_3297.jpg", 
      title:"Pi Electric Bikes", 
      desc: "From the studio to the road, each pi is hand crafted and professionally engineered to perfection", 
      src: '../images/gallery/_MG_3297.jpg' },
    { img: "../images/gallery/thumb/_MG_3305.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Every bike we build gives me the same excitement I had when I built my first electric bike 15 years ago", 
      src: '../images/gallery/_MG_3305.jpg' },
    { img: "../images/gallery/thumb/_MG_3322.jpg", 
      title:"Pi Electric Bikes", 
      desc: "Design, engineering and performance is our foundation - helping to reduce carbon emissions is mission", 
      src: '../images/gallery/_MG_3322.jpg' },
    { img: "../images/gallery/thumb/_MG_3334.jpg", 
      title:"Pi Electric Bikes", 
      desc: "We feel a strong obligation to inspire other designers and engineers to produce Zero Carbon light electric bikes and motorcycles", 
      src: '../images/gallery/_MG_3334.jpg' },
    { img: "../images/gallery/thumb/_MG_3357.jpg", 
      title:"Pi Electric Bikes", 
      desc: "The best way to get a real taste of Pi is to ride it", 
      src: '../images/gallery/_MG_3357.jpg' }];

function preload_photos() {
    var imgpreload = [];
    for (var i=0; i < photos.items.length; i++) {
        imgpreload[i] = new Image();
        imgpreload[i].src = photos.items[i].src;
    }
}

function show_photo_thumbs() {

    var html = "<div class='left_nav'>";
    if (photos.start > 0) {
        html += "<a class='photo_thumb_nav' href='' onclick='return prev_photo()' style='padding-right:6px'><</a> ";
    } else {
        html += "&nbsp;";
    }
    html += "</div>";
    for (var i=0; i < photos.display_count; i++) {
        var j = i + photos.start;
        var clip_css = j==photos.selected ? "photo_thumb_selected" : "photo_thumb";
        var src = photos.items[j].img;
        html += "<div id='photo_thumb"+j+"' class='" + clip_css+ "'>";
        html += "<div class='photo_thumb_inner'>";
        html += "<a href='' onclick='return show_photo("+j+")' title='" + photos.items[j].desc + "'><img src='"+src+"' height='50' border='0'></a>\n";
        html += "</div></div>\n";
    }
    if (photos.has_next()) {
        html += " <a class='photo_thumb_nav' href='' onclick='return next_photo()' style='padding-left:6px'>></a>";
    }
    html += "<div style='clear:both'></div>";

    document.getElementById("photo_thumbs").innerHTML = html;

    if (photos.selected == -1) {
        show_photo(0);
    }
}

function show_photo(index) {
    photos.select(index);
    
    var thumb = photos.items[index];
    var title = thumb.title;
    var desc = thumb.desc;
    var src = thumb.src;
    var code = '<center><img src="'+src+'"></center>';
    var info = "<div>" + title + "</div>\n";
    info += "<div>"+desc+"</div>\n";
    document.getElementById("main_photo_meta").innerHTML = info;
    document.getElementById("main_photo").innerHTML = code;

    var elem = document.getElementById("photo_thumb"+photos.last_selected);
    if (elem) {
        elem.className = "photo_thumb";
    }
    elem = document.getElementById("photo_thumb"+index);
    if (elem) {
        elem.className = "photo_thumb_selected";
    }
    return false;
}

function prev_photo() {
    if (photos.prev()) {
        show_photo_thumbs();
    }
    return false;
}

function next_photo() {
    photos.next();

    show_photo_thumbs();

    return false;
}

function validate(form) {
    if (form.name.value && form.name.value.length > 0 &&
        form.email.value && form.email.value.length > 0 && 
        form.comment.value && form.comment.value.length > 0) {
        return true;
    }
    document.getElementById("feedback_msg").innerHTML = 
        "All fields are required. Please enter your name, email address, and comments.";
    return false;
}
