isIE=document.all?true:false;
isNS4=document.layers?true:false;
isNS6=document.getElementById?true:false;
isOpera=navigator.appName.indexOf('opera')!=-1?true:false;


function Theme( userId,deliCat,count, destId   ) { 
    this.userId = userId; 
	this.deliCat = deliCat; 
    this.count = count; 
    this.destId = destId; 
    this.MenuT = []; 
    this.childtagsT = [];
}
function MenuItem( linkLabel, url,note,tags ) { 
    this.linkLabel = linkLabel; 
    this.url = url;  
    this.note = note;
    this.tags = tags;
}
myThemes = {};

    function showImage(img){ return (function(){ img.style.display='inline'; }); }
	//@theme is a delicious category
	//@destId is div container
	//buildParam is object build is boolean ,buildStyle is str,filterTag is str, limitView is num
	function getAndBuildDeliciousList( destId , theme , build ,buildStyle,filterTag,limitView){
		//console.log("getAndBuildDeliciousList ::"+destId);
		if(myThemes[theme]==null)
			myThemes[theme] = new Theme('oceatoon',theme,'40',destId);
		MenuT = myThemes[theme].MenuT;
		destId = (destId!=null)? destId : "delicious";
		count = (myThemes[theme].count!=null)? myThemes[theme].count : "20";
		deliCatUrl = (myThemes[theme].deliCat!=null)? "/"+myThemes[theme].deliCat+"?count="+count : "?count="+count;
    	if(destId!=null && destId!="" && destId!="rightDeco")
    		$(destId).update('<img src="style/images/loader3.gif" width="50px"/>');
    	MenuTlength = MenuT.length;
    	//console.log("getAndBuildDeliciousList :: get "+deliCatUrl+" update ::"+destId);
    	if(MenuTlength==0)
    		LoadScript('http://del.icio.us/feeds/json/'+myThemes[theme].userId+deliCatUrl+'?date=dodp',function () {buildDeliciousList(destId,theme,build,buildStyle,filterTag,limitView);},destId);
    	else
    		buildDeliciousList(destId,theme,build,buildStyle,filterTag,limitView);
    }	
	function getDeliciousAndBuildQuicksand( destId , theme , build ,buildStyle,filterTag,limitView){
		alert(JQuery('#container'));
	}
    var countTags=0;
    var alltagsStr = "";//liste de tous les tags disponible
    function buildDeliciousList(destId,theme,build,buildStyle,filterTag,limitView){
    	menuT = myThemes[theme].MenuT;
    	//getting and caching 
    	if(menuT.length==0 || menuT.length==1){
    		for (var i=0, post; post = Delicious.posts[i]; i++) {
    			tagsT = post.t;
    			tagsStr = "";
    			//concatenate all tags
    			for (var ct=0; ct < tagsT.length; ct++) {
    				tagsStr += " "+tagsT[ct];
    				if(alltagsStr.indexOf(tagsT[ct])<0){
    					alltagsStr += " "+tagsT[ct];
    					(myThemes[theme].childtagsT).push(tagsT[ct]);
    					countTags++;
    				}
    			}
		       menuT[i] = new MenuItem(post.d,post.u,post.n,tagsStr);
		    }
	    }
    	//console.log(myThemes[theme].childtagsT);
		MenuTlength = menuT.length;
    	if(limitView!=null && limitView < MenuTlength)
    		MenuTlength = limitView;
	   //console.log("buildDeliciousList "+buildStyle+" building menu ? "+build+" : menuT.length : "+menuT.length+" theme:"+theme);
	    if(build){
	    	switch (buildStyle) {
				case "thumbnailList" : buildThumbnailList(destId,MenuT,MenuTlength,filterTag); break;
	    		case "boxList" : buildBoxList(destId,MenuT,MenuTlength,filterTag); break;
	    		case "floatingBoxMenu" : buildDynamicDecoration(destId,theme); break;
	    		default :break;
	    	}
	    }
    }	
    
	function buildBoxList(destId,MenuT,MenuTlength,filterTag){
		$(destId).innerHTML = "<div style='margin:10px;'>";
			
		for(ix=MenuTlength-1 ; ix >=0 ; ix--){
		 	var obj = MenuT[ix];
		 	var tags = obj.tags;
	    	var dontDraw = false;
	    	if( filterTag!=null && filterTag!="" && tags.indexOf(filterTag)<0 )
	    		dontDraw = true; 
	    	
	    	if(!dontDraw){
				var imgHTML = (obj.note=="" || (obj.note.indexOf('http://')<0)) ? "" : "<img src='"+obj.note+"' height='60' width='125' style='border:none;padding-right:10px;vertical-align:middle;display:inline;'/>";
				var modulo = ix%6;
				if(modulo==0)bgColor = "#361A28";
				else if(modulo==1)bgColor = "#5C2D44";
				else if(modulo==2)bgColor = "#824061";
				else if(modulo==3)bgColor = "#827480";
				else if(modulo==4)bgColor = "#5C525A";
				else if(modulo==5)bgColor = "#5C525A";
				else bgColor = "#352F34";
				width=((obj.linkLabel).length>28) ? "300px" : "140px";
				
				HTMLstr = "<div class='item roundborder' style='width:"+width+"'>"+
				"<div class='itembox roundborder' style='background: "+bgColor+" none repeat scroll 0% 0%; ' >"+
				"<a id='"+destId+ix+"' class='roundborder itembox'  href='"+obj.url+"' target='_blank'>"+obj.linkLabel+"</a>"+
				imgHTML+
				"</div></div>";
				new Insertion.After(destId, HTMLstr);
				
	    	}
	    }
	    new Insertion.After(destId, "</div>");
	     buildStyle="";
	}
	
function buildThumbnailList(destId,MenuT,MenuTlength,filterTag){
		document.getElementById(destId).innerHTML = "<ul style='list-style-type: none; padding:0px; margin:10px;'>";
			
		 for (var ix=0; ix < MenuTlength; ix++) {
		 	var obj = menuT[ix];
		 	var tags = obj.tags;
	    	var dontDraw = false;
	    	if( filterTag!=null && filterTag!="" && tags.indexOf(filterTag)<0 )
	    		dontDraw = true; 
	    	
	    	if(!dontDraw){
				var src = (obj.note=="") ? "style/images/waterQ.PNG" : obj.note;
				if(tags.indexOf("photomat")>=0)author="by Mathieu Demachy";
		        else if(tags.indexOf("photokingdahoo")>=0)author="by David durandeau";
		        else if(tags.indexOf("photowatershot")>=0)author="by WaterShot";
		        else if(tags.indexOf("photooceatoon")>=0)author="by Oceatoon";
				HTMLstr = "<li style='list-style-type:none;width:450px;padding:10px;margin:0px 5px 10px 0px;border:1px solid white;display:inline;float:left;background:#000' onmouseover='this.style.background=\"#442e02\"' onmouseout='this.style.background=\"#000\"'>"+
				"<img src='"+src+"' height='60' width='125' style='border:none;padding-right:10px;vertical-align:middle;display:inline;' onclick='window.open(\""+obj.url+"\",\""+obj.linkLabel+"\")'/>"+
		        "<a class='MenuLink' id='"+destId+ix+"' href='"+obj.url+"' target='_blank'>"+obj.linkLabel+"</a>"+
				"<span style='font-size:x-small;'> "+author+"</span></li>";
				
		        document.getElementById(destId).innerHTML += HTMLstr ;
	    	}
	    }
	    document.getElementById(destId).innerHTML += "</ul>" ;
	     buildStyle="";
	}

function buildDynamicDecoration(destId,theme){
	childtagsT = myThemes[theme].childtagsT;
	top = 0;	right = 0;	width = 0;	height=0;	color = 0;
	for(ix=0; ix < childtagsT.length; ix++){//console.log("buildDynamicDecoration::"+childtagsT[ix]);
		p = getDecoParam();
		top = p.t;		right = p.r;		width = p.w;		height=p.h;		color = p.c;
		$(destId).innerHTML = $(destId).innerHTML + '<a id="deco'+ix+'" class="menu2 roundborder" style="position:absolute; top:'+top+'px; right:'+right+'px;width:'+width+'px;height:'+height+'px;background-color:'+color+';color:#333;" onclick="categoryFilter(\''+theme+'\','+ix+',\''+destId+'\');">'+childtagsT[ix]+'</a>';
	}
	startDeco(0,theme);
}
isCategoryMenu = false;
function setUpCategoryMenu(destId,theme){
	//console.log('setUpCategoryMenu::'+theme);
	childtagsT = myThemes[theme].childtagsT;
	isCategoryMenu = true;
	$(destId).style.cssText = "position:absolute; top:0px; right:0px;z-index:2;";
	for(ix=0; ix < childtagsT.length; ix++){
		new Effect.Parallel([
        new Effect.Move('deco'+ix, { sync: true, x: -170, y: 160+(35*ix), mode: 'absolute' }), 
        new Effect.Morph('deco'+ix, {style: { width: "150px", height: "15px",padding:"0px",backgroundColor:"#FF8A00",color:"#fff",borderColor:"#fff" }, sync: true})
     ], {duration: 2});
	}
}
currentCategory = "slidemag";
currentTitle = "Choisissez une categorie.";
function categoryFilter(theme,ix,destId){//console.log("categoryFilter"+theme+ix+destId+isCategoryMenu);
	if(!isCategoryMenu)
		setUpCategoryMenu(destId,theme);
	currentCategory = "slidemag+"+myThemes[theme].childtagsT[ix];
	openPage(pagesT.length-1);
	$(destId).style.cssText = "position:absolute; top:0px; right:0px;z-index:-1;";
}
function startDeco(ix,theme){//console.log("startDeco"+ix+theme+isCategoryMenu);
	if(!isCategoryMenu){
		ix = (ix > (myThemes[theme].childtagsT).length-1)?0:ix;
		p = getDecoParam();
		//log("startDeco :"+ix+", top :"+p.t+", right:"+p.r);
		new Effect.Parallel([
	        new Effect.Move('deco'+ix, { sync: true, x: -120-p.r, y: p.t, mode: 'absolute' }), 
	        new Effect.Morph('deco'+ix, {style: { width: p.w+"px", height: p.h+"px",backgroundColor:p.c }, sync: true})
	     ], {duration: 2});
		ix++;
	}
	setTimeout("startDeco("+ix+",'"+theme+"')",2000);
}
function getDecoParam(){
	var p = new Object();
	p.t = randomXToY(0,600);
	p.r = randomXToY(0,1000);
	p.w = randomXToY(20,190);
	p.h=randomXToY(20,300);
	p.c = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
	return p;
}
//function to get random number upto m
function randomXToY(minVal,maxVal,floatVal)
{
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

