
var emptyStr ="&nbsp;";

function CosmosItems(isMultiView,renderTarget,nextbutton,prevbutton,pagers)
{
    /*** Properties ***/
    this.renderTarget = renderTarget;
   

    this.itemArrayIndex = 0;
    this.itemArray = new Array();
    this.out = "";
    this.left = "";
    this.right = "";
    this.startPageIndex = 1;
    this.oldIndex = 1;
    this.itemsPerPage = 9;
    this.maxPagers = 4;
    this.nextbutton=nextbutton;
    this.prevbutton=prevbutton;
    this.pagers=pagers;
    this.mp=0;
    this.isMultiView=isMultiView;
    if(!this.isMultiView)
        this.itemsPerPage = 1;
    
	/*** Methods ***/
	this.render=render;
	this.addItem = addItem;
	//this.next = next;
	//this.prev = prev;
	this.renderPart=renderPart;
	this.renderFirst=renderFirst;
	this.renderRightSide=renderRightSide;
	this.maxPage=maxPage
	this.fixButtons=fixButtons;
	this.fixPagers=fixPagers;
	this.clearOld=clearOld;
	this.getIndexOfId=getIndexOfId;
	this.getItem=getItem;
}



function addItem(id,title,shortdesc,longdesc,date,movie,edrawing,industry,analyse)
{
    this.itemArray[this.itemArrayIndex++] = new Item(id,title,shortdesc,longdesc,date,movie,edrawing,industry,analyse);
}

function maxPage()
{
    if(this.mp == 0)
        this.mp = Math.ceil((this.itemArray.length / this.itemsPerPage));
    return this.mp;
}


function fixButtons()
{
    //Button calc
    if (this.startPageIndex == this.maxPage())
    {
        this.nextbutton.className = "browse next disabled";
    }
    else
    {
        this.nextbutton.className = "browse next";
    }
    if(this.startPageIndex == 1)
    {
        this.prevbutton.className =  "browse prev disabled";
    }
    else
    {
         this.prevbutton.className =  "browse prev";
    }
}

function fixPagers()
{
    var output = "";
    
    var count = 0;
    var max  = this.maxPage();
    var mod = 0;
    mod = max - this.startPageIndex < this.maxPagers-1 ? this.maxPagers + this.startPageIndex - max -1 : 0;
    while(this.startPageIndex - mod < 1)
        mod--;

    //Add the min page
    if (this.startPageIndex-mod > 1)
    {
        //output += "<a href=\"#\" onclick=\"index=1;cItems.render(index);scroll.toElement(scrolls[index].id);return false;\">(1)...</a>\n";
        output += "<a href=\"#\" onclick=\"index=1;cItems.render(index);setTimeout('scroll.toElement(scrolls[index].id);', delay);return false;\">(1)...</a>\n";
    }
      
    while (count < this.maxPagers && this.startPageIndex+count - mod  <= max)
    {
        var pageindex = this.startPageIndex + count - mod;
        //Make sure we get enugh
        if (pageindex == this.startPageIndex)
        {
            //output += "<a href=\"#\" class=\"choosen\" onclick=\"index="+pageindex+";cItems.render(index);scroll.toElement(scrolls[index].id);return false;\">" +pageindex+"</a>\n";
            output += "<a href=\"#\" class=\"choosen\" onclick=\"index="+pageindex+";cItems.render(index);setTimeout('scroll.toElement(scrolls[index].id);', delay);return false;\">" +pageindex+"</a>\n";
            
        }
        else
        {
            //output += "<a href=\"#\" onclick=\"index="+pageindex+";cItems.render(index);scroll.toElement(scrolls[index].id);return false;\">" +pageindex+"</a>\n";
            output += "<a href=\"#\" onclick=\"index="+pageindex+";cItems.render(index);setTimeout('scroll.toElement(scrolls[index].id);', delay);return false;\">" +pageindex+"</a>\n";
            
        }
            
        count++;
    }

    //Add the max page
    if (max - this.startPageIndex - count + mod >= 0)
    {
        output +="...";
        //output += "<a href=\"#\" onclick=\"index="+this.maxPage()+";cItems.render(index);scroll.toElement(scrolls[index].id);return false;\">("+this.maxPage()+")</a>\n";
        output += "<a href=\"#\" onclick=\"index="+this.maxPage()+";cItems.render(index);setTimeout('scroll.toElement(scrolls[index].id);', delay);return false;\">("+this.maxPage()+")</a>\n";
        
    }
    
    return output;
}

function clearOld()
{
    
    
    /*
    if( this.oldIndex != this.startPageIndex)
    {
       
        getElement('stage-in_'+this.oldIndex).innerHTML = "&nbsp";

    }*/
}

function render(index)
{


    
    if( this.oldIndex != this.startPageIndex)
    {
        getElement('stage-in_'+this.oldIndex).innerHTML = emptyStr;
    }
    
    this.oldIndex = this.startPageIndex;
    this.out = "";
    this.startPageIndex = index;
    
    this.fixButtons();
    this.pagers.innerHTML = this.fixPagers();
    
    
   
   
    if( getElement('stage-in_'+index) != null)
        getElement('stage-in_'+index).innerHTML = this.renderPart(index);
    else
    {
        //TODO styla detta
        //no hits
        getElement('stage-in_0').innerHTML = "No Hits!";
    }
    
    
    if(!this.isMultiView)
    {
    
        var item = this.itemArray[index-1];
        var so = new SWFObject("http://www.streamio.se/players/projectViewerEmbedded.swf?movieId=" + item.movie, "mymovie"+item.id, "640", "494", "8", "#FFFFFF");
        so.addParam("scaleMode","noBorder");
        so.addParam("wmode","opaque");
        so.addParam("scale", "noBorder");
        so.addParam("quality", "high");
          
        so.write("flashmovie"+item.id);
        
   
        /*
      
        var fo = new SWFObject( "/includes/flash/mini.swf", "fo", "640", "480", "7", "#FFFFFF" );
	    fo.addVariable( "movieId", item.movie );
	    fo.addParam("wmode","opaque");
	    so.write("flashmovie"+item.id);
      */
        getElement('column_right').innerHTML = this.renderRightSide(index);
    }

}


//Writes the 4 items on the right panel
function renderRightSide(index)
{
   var output=  "";
   for(var i = index; i < this.itemArrayIndex && i < index+4; i++)
    {
            var item = this.itemArray[i];
            output += item.createOutput(i,true);
    } 
    return output
}

function renderFirst()
{
    //we need to do pages amount of divs
    var output = "";
    var pageId = 0;
    var maxPage = this.maxPage()+1;
    for(var i = 0; i < maxPage; i++)
    {
            //Create a empty div
            output += "<div class=\"stage-in\" id=\"stage-in_" + pageId + "\">"+emptyStr+"</div>";
            pageId ++;
    }
    
    return output;
}

function renderPart(index)
{
    var output = emptyStr;
    var output = "";
    if(index == 0)
    {
        
        return emptyStr;
    }
    if (index > Math.ceil((this.itemArray.length / this.itemsPerPage)))
        return "";
    
    for(var i = 0; i < this.itemArrayIndex; i++)
    {
        if (i < (index * this.itemsPerPage) && i >= ((index - 1) * this.itemsPerPage))
        {
            var item = this.itemArray[i];
            output += item.createOutput(i,this.isMultiView);
            if (i == index * this.itemsPerPage)
               break;
        }
    }
    return output;
    
}

function getItem(id)
{
    for(var i = 0; i < this.itemArrayIndex; i++)
    { 
        var item = this.itemArray[i];
        if(item.id==id)
        {
            //Calculate it's index
            return item;
        }
    }
}

function getIndexOfId(id)
{
    for(var i = 0; i < this.itemArrayIndex; i++)
    { 
        var item = this.itemArray[i];
        if(item.id==id)
        {
            //Calculate it's index
            return Math.floor(i/this.itemsPerPage)+1;
        }
    }
    return 1;
}


//Object Item
function Item(id,title,shortdesc,longdesc,date,movie,edrawing,industry,analyse)
{
    /*** Properties ***/
    this.id=id;
	this.title=title;
	this.shortdesc=shortdesc;
	this.longdesc=longdesc;
	this.date=date;
	this.movie=movie;
	this.edrawing=edrawing;
	this.industry=industry;
	this.analyse=analyse;
	
    /*** Methods ***/
    this.createOutput=createOutput;
    this.createMultiView=createMultiView;
    this.createSingelView=createSingelView;
}


function createOutput(index, isMultiView)
{
    if(isMultiView)
        return this.createMultiView(index);
    else
        return this.createSingelView(index);
}

function createSingelView(index)
{

    //this.out = "<div id=\"preview_movie\"></div>\n";
    /*
    this.out += "var so = new SWFObject(\"http://www.streamio.se/players/projectViewerEmbedded.swf?movieId=" + this.movie + "\", \"movieflash" + this.id + "\", \"500\", \"375\", \"8\", \"#FFFFFF\");" +
                        "so.addParam(\"wmode\",\"opaque\");\n" +
                        "so.addParam(\"scale\", \"noBorder\");\n" +
                        "so.write(\"flashmovie" + this.movie + "\");";
    */
    this.out = "<div  id=\"flashmovie" + this.id + "\" class=\"flashmovie\"><p>This feature requires Flash-player 8 or later.<br /><br /><a href=\"http://www.adobe.com/go/gntray_dl_getflashplayer\" target=\"_blank\">Download the latest version of Flash-player here.</a></p></div>\n";
    this.out +="<div class=\"model_info\">\n";
    if(this.industry != "")
    {
        if(this.edrawing != "")
            this.out +="<ul class=\"about\"><li>Uploaded:<b>" + this.date + "</b></li><li>Industry:<b>" + this.industry + "</b></li><li>Type:<b>" + this.analyse + "</b></li><li class=\"last\"><a href=\""+ this.edrawing +"\">Download eDrawing</a></li></ul> \n";
        else
            this.out +="<ul class=\"about\"><li>Uploaded:<b>" + this.date + "</b></li><li>Industry:<b>" + this.industry + "</b></li><li>Type:<b>" + this.analyse + "</b></li><li class=\"last\">&nbsp;</li></ul> \n";
    }
    this.out +="<h2>"+ this.title + "\n";
    this.out +="</h2><p>"+this.longdesc+"</p>\n";
    this.out += "</div>\n";
    this.out +="<div class=\"clearfloat\"></div>";
    
    return this.out;
}


function createMultiView(index)
{
    
    if(index%3 == 0)
    {
        this.out =  "<div class=\"thumb_holder first\">";
    }
    else
    {
        this.out = "<div class=\"thumb_holder\">";
    }
    this.out +="<div class=\"shaddow\">\n";
    //this.out += "<a href=\"#\" onclick=\"$('ctl00_Content_hiddenid').value=" + this.id + ";__doPostBack('ctl00$Content$LinkButton1','');return false;\"><img src=\"http://user1206.streamio.se/content/images/1206_" + this.movie + ".jpg\" alt=\"" + this.title + "\" height=\"114\" width=\"149\" /></a>\n";
    this.out += "<a href=\"#"+ this.id +"\" onclick=\"$('ctl00_Content_hiddenid').value=" + this.id + ";__doPostBack('ctl00$Content$LinkButton1','');return false;\"><img src=\"../../fileuploads/images/" + this.movie + ".jpg\" alt=\"" + this.title + "\" /></a>\n";
    this.out +="<img src=\"../../gfx/backgrounds/shaddows/RC_thumb_btm.png\" alt=\"\" class=\"bg\" /></div>\n";
    //this.out +="<p><a href=\"StartPage.aspx?id=" + this.id + "\"><strong>" + this.title + "</strong>\n";
    //this.out +="<p><a href=\"javascript:__doPostBack('ctl00$Content$LinkButton1','');\" onclick=\"ctl00$Content$hiddenid.value=" + this.id + ";return true;\"><strong>" + this.title + "</strong>\n";
    this.out +="<p><a href=\"#"+ this.id +"\" onclick=\"$('ctl00_Content_hiddenid').value=" + this.id + ";__doPostBack('ctl00$Content$LinkButton1','');return false;\"><strong>" + this.title + "</strong>\n";
    this.out += this.shortdesc + "&nbsp;<span class=\"date\">" + this.date + "</span></a></p></div>\n";
    
    if(index%3 == 2)
        this.out +="<div class=\"clearfloat\"></div>";
    
    return this.out;
}

function cutText(textCrop, num, trail)
{
    var iPos;
    var iMax;
    var sOut = "";


    iMax = num - trail.Length;

    if (iMax <= 0)
    {
        return "";
    }
    else if (textCrop.Length <= num)
    {
        return textCrop;
    }
    else
    {
        sOut = textCrop.substring(0, num);
        iPos = sOut.lastIndexOf(" ");
        return sOut.substring(0, iPos) + trail;
    }
}

function ItemInit(newItems,ismultiview) 
{ 
    if(ismultiview)
        var width = (newItems.maxPage()+1) * 575;
    else
        var width = (newItems.maxPage()+1) * 644;
    $(newItems.renderTarget).style.width=width+"px";
    getElement(newItems.renderTarget).innerHTML = newItems.renderFirst();
   // newItems.render(1);
}


