 
	function Collection (id) {
	this.id = id; 
	this.images = []; 
	}
	function CollectionImage(url,thumbnail,comp_id,date,tags,title,desc,view_count,video_id) {
		this.url = url; this.desc = desc; this.title=title;
	        this.thumbnail=thumbnail;
	        this.comp_id=comp_id;
			this.date=date;
	        this.tags=tags;
	        this.view_count=view_count;
			this.video_id=video_id;
	        
		}


function getFromSource(source){
var compId=getCookie('CompId');
if(compId==null){
/*
Just to as a work around for preventing an error page
*/
        try{
		 dojo.xhrGet(  { url:'service?action=notification&notificationType=initSession&NextPage=download', load: function(type, data, evt){  },    handleAs: 'xml'} );
        }catch(e){}
        
        ///////////////////////////////////////////////// End of work around ////////////////////
        
	signin();
	return;
}
dojo.xhrGet(  { url:'service?action=getMediaFromSource&source='+source, 
	load: function(data, evt){
		  	var collections=data.getElementsByTagName("collection");
			for(var col=0;col<collections.length;col++ ){
				var imageCol=	collections[col];
				var colId=imageCol.getAttribute("id");
				collection=new  Collection(colId);
	
				for(var childIndex=0;childIndex<collections[col].childNodes.length;childIndex++){
					var nodeVal=collections[col].childNodes[childIndex];
					for(var childIndex2=0;childIndex2<nodeVal.childNodes.length;childIndex2++){
						var nodeVal2=nodeVal.childNodes[childIndex2];
						var title=	nodeVal2.getElementsByTagName("title")[0].firstChild.nodeValue;
						var url=	nodeVal2.getElementsByTagName("url")[0].firstChild.nodeValue;
						var desc=nodeVal2.getElementsByTagName("desc")[0].firstChild.nodeValue;
	
						var date=nodeVal2.getElementsByTagName("date")[0].firstChild.nodeValue;
						var thumbnail=nodeVal2.getElementsByTagName("thumbnail")[0].firstChild.nodeValue;
						var comp_id=nodeVal2.getElementsByTagName("comp_id")[0].firstChild.nodeValue;
						var video_id=nodeVal2.getElementsByTagName("video_id")[0].firstChild.nodeValue;
						var tags=nodeVal2.getElementsByTagName("tags")[0].firstChild.nodeValue;
						var view_count=nodeVal2.getElementsByTagName("view_count")[0].firstChild.nodeValue;
	
						collection.images.push(new CollectionImage(url,thumbnail,comp_id,date,tags,title,desc,view_count,video_id));
	
				}
	
			 
	         renderSourcedMedia(collection);
			}
		}	
	   },    
	handleAs: 'xml'} );
         

}
var deviceFormats=[];
function formatParam( name,ab, f, vcodec, acodec, s, b ,r,fileExt){
		this.name=name;
		this.ab=ab;	
		this.f=f;
		this.vcodec=vcodec;
		this.acodec=acodec;
		this.s=s;
		this.b=b;
		this.r=r;
        this.fileExt=fileExt;
}
var devices=[];
function selectDevice(device){
	
	selectedDevice=device;
	
	var d=[];
	d.push("cell");
	d.push("ipod");
	d.push("psp");
	d.push("pc");
	 
	for(var dev=0;dev<d.length;dev++){
		var devDiv=document.getElementById("device" + d[dev]); 
		if(device==d[dev]){
			devDiv.className="deviceSelectedDiv";
		}else{
			devDiv.className="deviceUnselectedDiv";
		}
		
	}
	
	 initMediaFormat(device);
	 

	
	
}
var selectedDevice=null;
function initMediaFormat(device){
			dojo.xhrGet(  { url:'service?action=getMediaFormat&deviceType='+device, 
			handleAs:"xml",
			load: function(  data, evt){ 
			 
			try{
				var deviceType=data.getElementsByTagName("deviceFormat");
				
				 var myobj = new Array();
                    for(var col=0;col< deviceType.length;col++ ){
                    var cInfo=	deviceType[col];
					 
					devices.push(new formatParam(   cInfo.getAttribute("name")  ,
													cInfo.getAttribute("ab"), 
													cInfo.getAttribute("f") ,
													cInfo.getAttribute("vcodec") ,  
													cInfo.getAttribute("acodec"), 
													cInfo.getAttribute("s"), 
													cInfo.getAttribute("b") ,
													cInfo.getAttribute("r"),
													cInfo.getAttribute("fileExt")	));
					
					myobj[col]= new Array(cInfo.getAttribute("name"), col); 
					
				
					}
					
					//init comboBox
					//dijit.byId('formatOpt').store.setValue(myobj);
					for(j=0;j<myobj.length;j++){
						var oOption = document.createElement("OPTION");
						oOption.text = myobj[j][0];
						oOption.value = myobj[j][1];		
						dojo.byId('formatOpt').options.add(oOption);
				
					}	
					formatValueChanged(myobj[0][0]);
				
			}catch(e){ }
			 } } );
}
function selectMedia(videoId,index){
//the index relates to the id of the selected div
dojo.xhrGet(  { url:'service?action=notification&notificationType=initSession&downloadMediaId='+videoId, load: function( data, evt){ },    handleAs: 'xml'} );

for(var c=0;c<collection.images.length;c++){
	
	var mediaDiv=document.getElementById(divPrefix+c);
	if(index==c){
		mediaDiv.className="videoSelectedDiv";
	}else{
		mediaDiv.className="videoUnselectedDiv";
	}
}
         	
}
var divPrefix="MediaListDiv";
 
function renderSourcedMedia(collection){

	if(collection==null ) return;
	  
	try{
		 
		var htmlStr="<div>";
		 
  
		for(var index=0;index<collection.images.length;index++){
			 
		 
				if(collection!=null && collection.images[index]!=null){
	        	htmlStr+='<div id='+ divPrefix+index+ '> <table style=" margin: 0;padding: 0;">';
				
				//selector
				htmlStr+='<tr ><td><input dojoType="Checkbox" type="radio" name="cb" value="1" onclick="selectMedia(\''+collection.images[index].video_id +'\','+index+' );return false;"/> </td>';
				
				//image section
				htmlStr+= '<td ><a href=# OnClick=\"selectMedia(\''+collection.images[index].video_id +'\','+index+' );return false;\"><img  id=ThumbnailImg src=\"'+ decodeURIComponent(collection.images[index].thumbnail) +'\"  width="120px" height="90px" /></a></td>';
				
				//name and desc section
				htmlStr+='<td >';
				htmlStr+='<div><a class=\'video_title_style\' href=# onclick="selectMedia(\''+collection.images[index].video_id +'\','+index+' );return false;" >' + collection.images[index].title + '</a></div>';
				
				htmlStr+='<div style"width:200px"><span class=\'imageLabel\'>'+collection.images[index].desc+'</span><\div>';
				
				htmlStr+="</tr>";
				
				htmlStr+="</table></div>";
	   			}
	   	
	  
	   }
	   htmlStr+="</div>";
	   document.getElementById("MediaListDiv").innerHTML=htmlStr;
	   }
	   catch(e){
	   	
	   }   	
	
}
function formatValueChanged(newValue){
	try{
		if(devices==null || devices == undefined)return;
		  
		for(var indx=0;indx<devices.length;indx++) {
		if(devices[indx].name==newValue){
		var formatStr=devices[indx].ab!=null?("-ab "+devices[indx].ab):"";
		formatStr+=devices[indx].f!=null?(" -f "+devices[indx].f):"";
		formatStr+=devices[indx].vcodec!=null?(" -vcodec "+devices[indx].vcodec):"";
		formatStr+=devices[indx].acodec!=null?(" -acodec "+devices[indx].acodec):"";
		formatStr+=devices[indx].s!=null?(" -s "+devices[indx].s):"";
		formatStr+=devices[indx].b!=null?(" -b "+devices[indx].b):"";
		formatStr+=devices[indx].r!=null?(" -r "+devices[indx].r):"";
		 
		 //add water mark
		 formatStr+=" -vhook '/usr/share/ffmpeg/src/vhook/watermark.so  -f /usr/local/share/tomcat-6.0.14/webapps/ROOT/images/watermark.gif'"; 
		  
		//store value as a session parameter
		dojo.xhrGet(  { url:'service?action=notification&notificationType=initSession&downloadMediaFormat='+formatStr+"&outputFileExt="+devices[indx].fileExt+"&deviceType="+selectedDevice, load: function( data, evt){ },    handleAs: 'xml'} );
		}
	}
		
	}catch(e){
		 
	}
	
}
function initSource(){
	var myobj=[];
	myobj.push(new Array(getLocaleStr("download_source_selected","Selected"),"SELECTED"));
	myobj.push(new Array(getLocaleStr("favourite","favourite"),"FAVOURITE")); 
	myobj.push(new Array(getLocaleStr("download_source_my_media","My Media"),"MY_MEDIA"));				 
	//init comboBox
	var srcWidget=dojo.byId('sourceOpt');
	//srcWidget.store.setValue(myobj);
	
	
	for(j=0;j<myobj.length;j++){
		var oOption = document.createElement("OPTION");
		oOption.text = myobj[j][0];
		oOption.value = myobj[j][1];		
		srcWidget.options.add(oOption);

	}
	 
}

function initAjax() {
	try{
		
		/* var formatOpt=	new dijit.form.FilteringSelect(  
     							 {	dataUrl: "",  name: "formatOpt",  value: "",  widgetId: "formatOpt"  },
     								 dojo.byId("formatOpt")
  									 ); */
	   var formatOpt = dojo.byId("formatOpt");
        selectDevice( "pc");
		//dojo.connect(formatOpt, "_startSearch", "initMediaFormat");
        dojo.connect(formatOpt, "onValueChanged", "formatValueChanged");
		
		
	/* var srcOpt=	new dijit.form.FilteringSelect(  {  dataUrl: "",  name: "sourceOpt",  value: "", widgetId: "sourceOpt"  },
      dojo.byId("sourceOpt")  );*/ 

		
		var srcOpt=dojo.byId("sourceOpt");
		dojo.connect(srcOpt, "_startSearch", "initSource");
		initSource();
		dojo.connect(srcOpt, "onValueChanged", "getFromSource");
		getFromSource("SELECTED");
		}catch(e){
			console.debug(e.message);
		}
    }

dojo.addOnLoad(initAjax);