function addPhotoCarouselItem(photoId){
    var np = jQuery('#noPhotos')[0];
    if(np){
        photocarousel_idRef.unshift({
            'id':photoId,
            'galleryUrl':'/showImage/'+photoId+'/gallery_photo',
            'thumbUrl':'/showImage/'+photoId+'/thumb125_92'
        });

        jQuery(np).replaceWith("<ul id='photoCarousel' class='jcarousel-skin-name'></ul>");
        jQuery('#photoCarousel').jcarousel({
            initCallback: photocarousel_initCallback,
            itemLoadCallback: photocarousel_itemLoadCallback,
            size:photocarousel_idRef.length+1
        });
    }
}

function photocarousel_initCallback(c){
    window.addPhotoCarouselItem = function(photoId){
        photocarousel_idRef.unshift({
            'id':photoId,
            'galleryUrl':'/showImage/'+photoId+'/gallery_photo',
            'thumbUrl':'/showImage/'+photoId+'/thumb125_92'
        });

        c.size(c.size() + 1);
        c.add(0,photocarousel_getItemHTML(0));
        c.reset();

        tb_init(c.container.find('.thickbox'),c);
    }
}

function addVideoCarouselItem(videoId,videoTypeId,code,flashVars,title,description){
    var nv = jQuery('#noVideos')[0];
    if(nv){
        videocarousel_idRef.unshift({
            'id':videoId,
            'videoType':videoTypeId,
            'url':code,
            'flashVars':flashVars,
            'title':title,
            'description':description
        });
        jQuery(nv).replaceWith("<ul id='videoCarousel' class='jcarousel-skin-name'></ul>");
        jQuery('#videoCarousel').jcarousel({
            initCallback: videocarousel_initCallback,
            itemLoadCallback: videocarousel_itemLoadCallback,
            size:videocarousel_idRef.length+1
        });
    }
}

function videocarousel_initCallback(c){
    window.addVideoCarouselItem = function(videoId,videoTypeId,code,flashVars,title,description){
        videocarousel_idRef.unshift({
            'id':videoId,
            'videoType':videoTypeId,
            'url':code,
            'flashVars':flashVars,
            'title':title,
            'description':description
        });

        c.size(c.size() +1);
        c.add(0,videocarousel_getItemHTML(0));
        c.reset();

        tb_init(c.container.find('.thickbox'),c);
    }
}

function photocarouselHome_itemLoadCallback(c,s){
    if (c.options.size > 4 && document.getElementById('photoCounter')) {
        if (c.last == c.options.size) {
            document.getElementById('photoCounter').innerHTML = c.first + "-" + (c.last) + " of " + (c.options.size);
        } else {
            document.getElementById('photoCounter').innerHTML = c.first + "-" + c.last + " of " + (c.options.size);
        }
    }
    if(c.has(c.first,c.last)){
        return;
    }

    for(var i=c.first-1; i < c.last; i++){
        if(s == "init" || !c.has(i+1)){
            c.add(i+1,photocarouselHome_getItemHTML(i));
        }
    }

    tb_init(c.container.find('.thickbox'),c);
}

function photocarouselHome_getItemHTML(idx){
    var id = photocarousel_idRef[idx];
    var venue = photocarousel_venueRef[idx];
    var link = photocarousel_linkRef[idx];
    var linkName = photocarousel_linkNameRef[idx];
    if (!!id == false) {
        return "<a href=''><img src='http://s3.amazonaws.com/wimgo/images/addPhoto.gif' class='addPhoto'/></a>";
    } else {
        var galleryUrl = id.galleryUrl;
        var thumbUrl = id.thumbUrl+"?report=true&moduleId="+id.id+"&moduleTypeId=30";

        return "<a href='"+galleryUrl+"' class='thickbox thumb' rel='user-gallery' onclick='return false;'><img src='"+thumbUrl+"' border='0'/></a>"+reportLink(id.id,30)+"<div class='venueName'><a href='"+linkName+"/business/"+link+"'>"+venue+"</a></div>";
    }
}

function photocarousel_itemLoadCallback(c,s){
    if (c.options.size > 3 && document.getElementById('photoCounter')) {
        if (c.last == c.options.size) {
            document.getElementById('photoCounter').innerHTML = c.first + "-" + (c.last - 1) + " of " + (c.options.size - 1);
        } else {
            document.getElementById('photoCounter').innerHTML = c.first + "-" + c.last + " of " + (c.options.size - 1);
        }
    }
    if(c.has(c.first,c.last)){
        return;
    }

    for(var i=c.first-1; i < c.last; i++){
        if(s == "init" || !c.has(i+1)){
            c.add(i+1,photocarousel_getItemHTML(i));
        }
    }

    tb_init(c.container.find('.thickbox'),c);
}

function photocarousel_getItemHTML(idx){
    var id = photocarousel_idRef[idx];

    if (!!id == false) {
        return "<a href='/photoupload?moduleId="+venueID+"&moduleTypeId=22&TB_iframe=true&height=400&width=400' class='thickbox' title='Upload a photo'><img src='http://s3.amazonaws.com/wimgo/images/addPhoto.gif' class='addPhoto'/></a>";
    } else {
        var galleryUrl = id.galleryUrl;
        var thumbUrl = id.thumbUrl;
        var title = id.title;
        var description = id.description;
        
        return "<a href='"+galleryUrl+"' class='thickbox thumb' title='"+htmlquotes(title)+"' tb_description='"+htmlquotes(description)+"' rel='user-gallery' onclick='return false;'><img src='"+thumbUrl+"' border='0'/></a>"+( id.actionUrl ? removePhotoLink(id.id,30,id.photoId) : reportLink(id.id,30) )+"<div class='venueName'>"+title+"</div>";
    }
}

var videocarousel_typeRef = {youtube:1,google:2,vimeo:3,brightcove:4};
function videocarousel_itemLoadCallback(c,s){
    //c.size(videocarousel_idRef.length);
    if(c.has(c.first,c.last)){
        return;
    }

    for(var i=c.first-1; i<c.last; i++){
        var jqItem = jQuery(videocarousel_getItemHTML(i));
        tb_init(jqItem.find('.thickbox'));
        c.add(i+1,jqItem);
    }
    if (document.getElementById('videoCounter')) {
        document.getElementById('videoCounter').innerHTML = c.first + " of " + c.options.size;
    }
}

function videocarousel_getItemHTML(i){
    switch(videocarousel_idRef[i].videoType){
        case videocarousel_typeRef.youtube:
            return "<div class='embededVideo'>" + youtubeHTML(videocarousel_idRef[i].id,videocarousel_idRef[i].url) + "</div><div class='videoContent'><p class='video-title'>" + videocarousel_idRef[i].title + "</p><p class='video-description'>" + videocarousel_idRef[i].description + "</p></div>";
        case videocarousel_typeRef.google:
            return "<div class='embededVideo'>" + googleHTML(videocarousel_idRef[i].id,videocarousel_idRef[i].url) + "</div><div class='videoContent'><p class='video-title'>" + videocarousel_idRef[i].title + "</p><p class='video-description'>" + videocarousel_idRef[i].description + "</p></div>";
        case videocarousel_typeRef.vimeo:
            return "<div class='embededVideo'>" + vimeoHTML(videocarousel_idRef[i].id,videocarousel_idRef[i].url) + "</div><div class='videoContent'><p class='video-title'>" + videocarousel_idRef[i].title + "</p><p class='video-description'>" + videocarousel_idRef[i].description + "</p></div>";
        case videocarousel_typeRef.brightcove:
            return "<div class='embededVideo'>" + brightcoveHTML(videocarousel_idRef[i].id,videocarousel_idRef[i].url,videocarousel_idRef[i].flashVars) + "</div><div class='videoContent'><p class='video-title'>" + videocarousel_idRef[i].title + "</p><p class='video-description'>" + videocarousel_idRef[i].description + "</p></div>";
        default:
            return false;
    }
}

var vidheight = 199, vidwidth = 266;

function youtubeHTML(modid,vidid){
    return reportLink(modid,53)+"<object width=\""+vidwidth+"\" height=\""+vidheight+"\"><param name=\"movie\" value=\"http://www.youtube.com/v/"+vidid+"&hl=en&fs=1\"></param><param name=\"wmode\" value=\"transparent\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/"+vidid+"&hl=en&fs=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\""+vidwidth+"\" height=\""+vidheight+"\"></embed></object>";
}
function googleHTML(modid,vidid){
    return reportLink(modid,53)+"<embed id=\"VideoPlayback_"+vidid+"\" src=\"http://video.google.com/googleplayer.swf?docid="+vidid+"&hl=en&fs=true\" style=\"width:"+vidwidth+"px;height:"+vidheight+"px\" allowFullScreen=\"true\" wmode=\"transparent\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\"></embed>";
}
function vimeoHTML(modid,vidid){
    return reportLink(modid,53)+"<object width=\""+vidwidth+"\" height=\""+vidheight+"\"><param name=\"allowfullscreen\" value=\"true\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id="+vidid+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id="+vidid+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" wmode=\"transparent\" allowscriptaccess=\"always\" width=\""+vidwidth+"\" height=\""+vidheight+"\"></embed></object>";
}
function brightcoveHTML(modid,url,flashVars){
    return reportLink(modid,53)+"<embed src=\"http://services.brightcove.com/"+url+"\" bgcolor=\"#FFFFFF\" flashVars=\""+flashVars+"\" wmode=\"transparent\" base=\"http://admin.brightcove.com\" name=\"flashObj\" width=\""+vidwidth+"\" height=\""+vidheight+"\" allowfullscreen=\"true\" seamlesstabbing=\"false\" type=\"application/x-shockwave-flash\" swLiveConnect=\"true\" pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\"></embed>";
}

function reportLink(moduleId,moduleTypeId){
    return "<div class='relative'><a class='reportIcon thickbox' href='/report?moduleId="+moduleId+"&moduleTypeId="+moduleTypeId+"&TB_iframe=true&height=300&width=400'>Report</a></div>";
}

function removePhotoLink(moduleId,moduleTypeId,photoId){
    //return "<div class='relative'><a class='reportIcon thickbox' href='/report?moduleId="+moduleId+"&moduleTypeId="+moduleTypeId+"&TB_iframe=true&height=300&width=400'>Remove Image</a></div>";
	return "<div class='relative'><a class='reportIcon thickbox' href='/removephoto/?photoId="+photoId+"&moduleId="+moduleId+"&moduleTypeId="+moduleTypeId+"&TB_iframe=true&height=300&width=400'>Remove Image</a></div>";
	
}
