/**
 * @author PATRICK STORHAYE
 */

// functions

function redirect(el) {
	window.location=el;
}

function Doublons(TabInit){
	NvTab = new Array();
	var q=0;
	var LnChaine= TabInit.length;
	 for(x=0;x<LnChaine;x++)
	    {
	    for(i=0;i<LnChaine;i++)
	        {
	        if(TabInit[x]==  TabInit[i] && x!=i) TabInit[i]='faux';
	        }
	    if(TabInit[x]!='faux'){  NvTab[q] = TabInit[x]; q++}
	    }
	return NvTab;
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function PSload(el) {
	this.location='index.php?Entry='+el;
}


function PSrubrique(el){
	//$('div[Rubrique]').hide();
	$('div[MenuRubrique]').css({"background-color": "transparent","color": "white"});
	//$('div[Rubrique|=Rubrique' + el + ']').fadeIn('slow');
	$('div[MenuRubrique|=Rubrique' + el + ']').css({"background-color": "white","color": "black"});
	Selected_Entry=el;
	}
	
function PShighlight(elOut,elInName,elIn){
	$("div[name='"+elOut+"']["+elInName+"!='"+elIn+"']").css({"opacity":"0.7","filter":"alpha(opacity=70)","-moz-opacity":"0.7","-khtml-opacity":"0.7"}); 
	$("["+elInName+"='"+elIn+"']").css({"border-color":Thumbnail_border_color_over});
}
function PSunhighlight(elOut,elInName,elIn){
	$("div[name='"+elOut+"']["+elInName+"!='"+elIn+"']").css({"opacity":"1","filter":"alpha(opacity=100)","-moz-opacity":"1","-khtml-opacity":"1"}); 
	$("["+elInName+"='"+elIn+"']").css({"border-color":Thumbnail_border_color});
}
function PSshowCache(el) {
	$("#Cache").fadeIn('fast');
	//$("#Cache2").html("");
	//$("#Cache2").append(el);
	$("#Cache2").positionCenter();
	$("#Cache2").fadeIn('slow');
}

function PScheckCooliris(what,where) {
	if (ModeCoolirisRubrique[what]==1) {
		if(typeof(LabelMosaique) != "undefined") {
			HideMoreInfo="false";
			DoubleVisuMode='<span class="SelectedVisu">'+LabelMosaique+'</span>&nbsp;<a href="#" onclick="PSloadCooliris(\''+what+'\',\''+where+'\');">'+LabelCooliris+'</a>';
			$("#MoreInfo").append(DoubleVisuMode);
		} else {
			HideMoreInfo="true";
			}
	} else {
			HideMoreInfo="true";
	}
}

function PScheckSearchIsOn(what,where) {

	$("#MoreSearch").html("");
	if (SearchIsOn[what]==1) {
		HideSearch="false";
		$("#MoreSearch").append(LabelToPutInSearchDiv);
	} else {
		HideSearch="true";
	}
}

function PSloadCooliris(what,where) {
	CoolirisRSSurl=CoolirisRSSbase+Language_Directory_Label+"/"+what+".html";
	CoolirisHeight=getDocHeight()-HeaderHeight-FooterHeight+"px";
	CoolirisEmbed='<object id="ci_84516_o" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + CoolirisWidth + '" height="' + CoolirisHeight + '"><param name="movie" value="http://apps.cooliris.com/embed/cooliris.swf"/><param name="allowFullScreen" value="' + CoolirisallowFullScreen + '"/><param name="allowScriptAccess" value="always"/><param name="bgColor" value="' + CoolirisBgcolor + '" /><param name="flashvars" value="feed=' + CoolirisRSSurl + '" /><param name="wmode" value="opaque" /><embed id="ci_84516_e" type="application/x-shockwave-flash" src="http://apps.cooliris.com/embed/cooliris.swf" width="' + CoolirisWidth + '" height="' + CoolirisHeight + '" allowFullScreen="' + CoolirisallowFullScreen + '" allowScriptAccess="always" bgColor="' + CoolirisBgcolor + '" flashvars="feed=' + CoolirisRSSurl + '" wmode="opaque"></embed></object><br>';	
	$("#More").hide();
	$(where).html("");
	$("#MoreInfo").html("");
	$(where).append(CoolirisEmbed);
	DoubleVisuMode='<a href="#" onclick="PSloadData(0,'+what+',\''+where+'\');">'+LabelMosaique+'</a>&nbsp;<span class="SelectedVisu">'+LabelCooliris+'</span>';	
	$("#MoreInfo").hide();
	$("#MoreInfo").append(DoubleVisuMode);
	$("#MoreInfo").show();	
}

function PSloadSingleContent (ContentId,where) {
	$.getJSON("SingleEncode.php?Content="+ContentId, function(data){
		if (data.recordsReturned == 1) {
			$.each(data.items, function(i, item){
				switch (MyLangue) {
					case "1":
						Title = item.Content_Comments_Language1;
						break;
					case "2":
						Title = item.Content_Comments_Language2;
						break;
				}
				
				if (item.Id_Content_Type==1) {
					ContentToAppend='<center><div class="SingleImage"><img src="' + ContentImage_baseURL + Language_Directory_Label + '/' + item.Content_FileName + '"><br><br>' + Title + '</div></center>';
				}
				if (item.Id_Content_Type == 2) {
					ContentToAppend='<center>' + item.Content_FileName + '</center>';
				}	
				if (item.Id_Content_Type == 4) {
					ContentToAppend='<center><iframe scrolling=auto frameborder=0 src="' + ContentHtml_baseURL + Language_Directory_Label + '/' + item.Content_FileName + '" width="' + HTMLfancyWidth + 'px" height="' + HTMLfancyHeight + 'px"></center>';
				}	
				
				
				$(where).append(ContentToAppend);
			})
		} else {
			$(where).append('<center><br><br>'+LabelNoRecordAvailable+'</center>');
		}
	});
	
}

function PSloadData(start,what,where,texttosearch) {


$("#Cache2").html("");
	
	// DIV creation for data & pagination
		// Hide while doing the job
		$("#More").hide();
		$(where).html("");
		$("#MoreInfo").html("");
		$("#MorePage").html("");
		$(where).append('<div class="AffichePage" id="PageData"></div>');

	// Load Json
	if (typeof(MySearch) != "undefined" && MySearch != "" && MySearch !== null) {
		MyStringSearch="&MySearch="+MySearch;
		LabelToPutInSearchDiv=LabelSearchOn + '&laquo;<i>' + MySearch + '</i>&raquo;&nbsp;<a href="#" ><img onclick="PSload(' + what + ');" src="css/CancelSearch.png"  align="absmiddle" border="0"></a><br>';
	} else {
		MyStringSearch="";
		LabelToPutInSearchDiv='<form class="MySearch" name="MySearchform"><input type="hidden" name="Entry" value=' + what + '><div ><label for="tags"><span class="Search">' + LabelSearch + ' </span></label><input name="MySearch" id="tags" />&nbsp;</div></form>';
		}
	
	$.getJSON("Jsonencode.php?start=" + start + "&PS=" + MyPageSize + "&SQL="+what + MyStringSearch + "&Langue="+MyLangue, function(data){
	
	if (start==0 && start<MyPageSize) {PageActive=1;} else { PageActive = (Math.floor(start/(MyPageSize))+1); }
	
	
	if (data.recordsAvailable % MyPageSize != 0) {
		NbPage = Math.floor(data.recordsAvailable / MyPageSize) + 1;
	} else {
		NbPage = Math.floor(data.recordsAvailable / MyPageSize);
	}

	PScheckCooliris(what,where);
	PScheckSearchIsOn(what,where);

	$("#MorePage").append(data.recordsAvailable + " " + LabelOfTheEntryTopic[what] + "&nbsp;|&nbsp;" + LabelPage + "&nbsp;");
	$("#MorePage").append(PageActive + " " + LabelOn + " " + NbPage + " | ");
	
	if (PageActive>1) { $("#MorePage").append('<a href="#" onclick="PSloadData(0,' + what + ',\'' + where + '\');">' + LabelFirst + '</a>&nbsp;'); } else { $("#MorePage").append('<span class="NoClick">' + LabelFirst) + '</span>'; }
	if (PageActive>1) {$("#MorePage").append('<a href="#" onclick="PSloadData(' + ((PageActive-2)*MyPageSize) + ',' + what + ',\'' + where + '\');">' + LabelPrev + '</a>&nbsp;');} else { $("#MorePage").append('<span class="NoClick">' + LabelPrev) + '</span>'; }
	if (PageActive<NbPage) {$("#MorePage").append('<a href="#" onclick="PSloadData(' + (PageActive*MyPageSize) + ',' + what + ',\'' + where + '\');">' + LabelNext + '</a>&nbsp;');} else { $("#MorePage").append('<span class="NoClick">' + LabelNext) + '</span>'; }
	if (PageActive<NbPage) {$("#MorePage").append('<a href="#" onclick="PSloadData(' + ((NbPage-1)*MyPageSize) + ',' + what + ',\'' + where + '\');">' + LabelLast + '</a>&nbsp;');} else { $("#MorePage").append('<span class="NoClick">' + LabelLast) + '</span>'; }
	
	var availableTags = new Array;
	$.each(data.items, function(i, item){
	
		availableTags[i]=item.Tags;
	
		// Display mode as thumbnails

		if (Entry_Display_Mode[what]==1) {		
				
				Img_Class_To_Use="ImageGallery";
				
				
				switch (MyLangue) {
				case "1":
					ContentComments = item.Content_Comments_Language1;
					item.Title=item.Title_Language1;
					break;
				case "2":
					ContentComments = item.Content_Comments_Language2;
					item.Title=item.Title_Language2;					
					break;
				default:
					ContentComments = item.Content_Comments_Language1;
					item.Title=item.Title_Language1;					
					break;
				}
				
				if (Show_Content_As_A_Collection[what] == 1) {
					MyFancyRel = "rel=\"MultiTab\""; } else { MyFancyRel=""; 
				}
				
				if ((item.Thumb_Image_FileName=="")||(item.Thumb_Image_FileName===null)) {
					Div_Class_To_Use="Thumbnail_With_Transparency";
					Style_To_Add_To_Div='style="border-color:' + Thumbnail_border_color + ';width:' + ThumbnailSquare_Width[what] + 'px;height:' + ThumbnailSquare_Height[what] + 'px;background-color:rgb(' + Math.round(Math.random() * 190) + ',' + Math.round(Math.random() * 190) + ',' + Math.round(Math.random() * 190)+')"';			
					IMG='<table class="innerHTML" ><tr><td align=right valign=bottom>' + item.Title + '<br><span class="smallinnerHTML">' + item.Content_Year + '</span></td></tr></table>';
				} else { 
					ContentThumbnail = Thumbnails_baseURL + item.Thumb_Image_FileName;
					if (Thumbnail_Is_Transparent[what]==1) { 
						Div_Class_To_Use="Thumbnail_With_Transparency";
						Style_To_Add_To_Div='style="border-color:' + Thumbnail_border_color + ';width:' + ThumbnailSquare_Width[what] + 'px;height:' + ThumbnailSquare_Height[what] + 'px;background-color:rgb(' + Math.round(Math.random() * 190) + ',' + Math.round(Math.random() * 190) + ',' + Math.round(Math.random() * 190)+')"';
					} else {
						Div_Class_To_Use="Thumbnail_Without_Transparency";
						Style_To_Add_To_Div='style="border-color:' + Thumbnail_border_color + ';width:' + ThumbnailSquare_Width[what] + 'px;height:' + ThumbnailSquare_Height[what] + 'px"';			
					}

					IMG='<img id="Image' + item.Id + '" src="' + ContentThumbnail + '" class="' + Img_Class_To_Use + '" style="width:' + ThumbnailSquare_Width[what] + 'px;height:' + ThumbnailSquare_Height[what] + 'px">';	
				}
				
				switch (item.Id_Content_Type) {
						case "1":
							ContentToDisplay = ContentImage_baseURL + Language_Directory_Label + '/' + item.Content_FileName;
							A_Content_Type='Ctype="IMG"';
							break;
						case "2":
							$("#Container").append('<div style="display:none;"><div class="InnerModal" name="MyModal" id="Modal' + item.Id + '">' + item.Content_FileName + '</div></div>');
							ContentToDisplay = '#Modal' + item.Id;
							A_Content_Type='Ctype="Modal"';
							break;	
						case "4":
							ContentToDisplay = ContentHtml_baseURL + Language_Directory_Label + '/' + item.Content_FileName;
							A_Content_Type='Ctype="HTML"';
							break;	
						default:
							ContentToDisplay = ContentImage_baseURL + what + '/' + item.Content_FileName;
							A_Content_Type='Ctype="IMG"';
							break;
				}
				
				if (item.Content_IsNot_Clickable == 1) {
					MyNewDiv = '<div  ' + what + 'ID="' + item.Id + '" onmouseover="PShighlight(\'' + what + '\',\'' + what + 'ID\',\'' + item.Id + '\');" onmouseout="PSunhighlight(\'' + what + '\',\'' + what + 'ID\',\'' + item.Id + '\');" name="' + what + '" ContentYear="' + item.Content_Year + '" id="DIV' + item.Id + '" class="' + Div_Class_To_Use + '" ' + Style_To_Add_To_Div + '>' + IMG + '</div>';
				}
				else {
					MyNewDiv = '<div  ' + what + 'ID="' + item.Id + '" onmouseover="PShighlight(\'' + what + '\',\'' + what + 'ID\',\'' + item.Id + '\');" onmouseout="PSunhighlight(\'' + what + '\',\'' + what + 'ID\',\'' + item.Id + '\');" name="' + what + '" ContentYear="' + item.Content_Year + '" id="DIV' + item.Id + '" class="' + Div_Class_To_Use + '" ' + Style_To_Add_To_Div + '><a A_ID="' + item.Id + '" ' + A_Content_Type + ' Content_Year="' + item.Content_Year + '" ' + MyFancyRel + ' name="Image' + what + '" title="' + ContentComments + '" href="' + ContentToDisplay + '">' + IMG + '</a></div>';
				}
				
				if (i < MyPageSize) {
					$(where).append(MyNewDiv);
					
				}
				
		}				
				
			// Display mode as a list				
			if (Entry_Display_Mode[what] == 2) {
			}			

	});
				
		
			$("a[rel=MultiTab]").fancybox({ 'titleShow': true, 'titlePosition': 'inside', 'transitionIn': 'elastic', 'transitionOut': 'elastic', 'overlayOpacity': '0.8', 'overlayColor': '#101010', 'hideOnContentClick': true	});
			$("a[Ctype=IMG]").fancybox({ 'titleShow': true, 'titlePosition': 'inside', 'transitionIn': 'elastic', 'transitionOut': 'elastic', 'overlayOpacity': '0.8', 'overlayColor': '#101010', 'hideOnContentClick': true	});
			$("a[Ctype=HTML]").fancybox({'titleShow': false,'width': HTMLfancyWidth,'height':HTMLfancyHeight,'titlePosition'	: 'inside','transitionIn'	: 'elastic','transitionOut'	: 'elastic','overlayOpacity' : '0.8','overlayColor' : '#101010','type': 'iframe','hideOnContentClick':false});
			$("a[Ctype=Modal]").fancybox({'titleShow':false,'transitionIn'	: 'elastic','transitionOut'	: 'elastic','overlayOpacity' : '0.8','overlayColor' : '#101010','hideOnContentClick':false});
								
			$('.Thumbnail_Without_Transparency').css({"border-width":Entry_Thumbnail_border[what]+"px"});
			$('.Thumbnail_With_Transparency').css({"border-width":Entry_Thumbnail_border[what]+"px"});
			
			$("#More").fadeIn();
			
			if (HideSearch=="true") {
				$("#MoreSearch").hide();
			} else {
				$("#MoreSearch").show();
			}
			
			if (HideMoreInfo=="true") {
				$("#MoreInfo").hide();
			} else {
				$("#MoreInfo").show();
			}
			
			var monTableauSansDoublon = Doublons(MyTags[what]);
			$("#tags").autocomplete({source: monTableauSansDoublon});
			

	});

}

// init
 
$(document).ready(function() {

	// Set the site's title
	$(this).attr("title", SiteTitle);
	
	// Set the main interface's structure of the site
	
		// main div creation & position
		$("#MyBody").append('<div id="Header"></div>');
		$("#MyBody").append('<div id="MinimizeHeader"></div>');
		
		$("#MyBody").append('<div id="Cache"></div>');
		$("#MyBody").append('<div id="Cache2"></div>');
		$("#Header").append('<div id="More"></div>');
		$("#Header").hide();
		$("#Header").css({'height':HeaderHeight+"px"});
		$("#More").append('<div id="MoreSearch"></div>');
		$("#Header").append('<div id="MoreInfo"></div>');
		$("#Header").append('<div id="ContactInfo"></div>');
		$("#More").append('<div id="MorePage"></div>');
		$("#MyBody").append('<div id="Container"></div>');
		Today = new Date;
		
		if (FooterIsActive==1) {
		$("#MyBody").append('<div id="Footer">© Patrick Storhaye,&nbsp;'+Today.getFullYear()+'&nbsp;|&nbsp;<a href="Mailto:'+EmailContact+'">'+LabelContact+'</a>&nbsp;|&nbsp;<a href="http://twitter.com/storhaye" target="_blank"><img src="css/footer_tw.png" border="0" align="absmiddle">&nbsp;'+LabelTwitter+'</a>&nbsp;<a href="http://www.facebook.com/Storhaye" target="_blank"><img src="css/fb_button.png" border="0" align="absmiddle">&nbsp;'+LabelFacebook+'</a><span class="st_facebook_button" displayText="Share this on Facebook" st_url="http://www.storhaye.com" st_title="Storhaye"></span><span class="st_twitter_button" displayText="Tweet this" st_url="http://www.storhaye.com" st_title="Storhaye"></span></div>');
		}
		
		
		
		
		$("#ContactInfo").append('<iframe align=absmiddle src="http://www.facebook.com/plugins/like.php?locale='+LanguageTagofFB+'&amp;href=http%3A%2F%2Fwww.storhaye.com&amp;layout=button_count&amp;show_faces=false&amp;width=150&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:150px; height:21px;" allowTransparency="true"></iframe>&nbsp;© Patrick Storhaye,&nbsp;'+Today.getFullYear()+'&nbsp;|&nbsp;<a href="Mailto:'+EmailContact+'">'+LabelContact+'</a>&nbsp;|&nbsp;<a href="http://twitter.com/storhaye" target="_blank"><img src="css/footer_tw.png" border="0" align="absmiddle">&nbsp;'+LabelTwitter+'</a>&nbsp;<a href="http://www.facebook.com/Storhaye" target="_blank"><img src="css/fb_button.png" border="0" align="absmiddle">&nbsp;'+LabelFacebook+'</a><span class="st_facebook_button" displayText="Share this on Facebook" st_url="http://www.storhaye.com" st_title="Storhaye"></span><span class="st_twitter_button" displayText="Tweet this" st_url="http://www.storhaye.com" st_title="Storhaye"></span>');
		

		
		$("#Container").css({"left":"0px","width":"100%","top":(HeaderHeight+HeaderVerticalSpacing)+"px"});
		$("#More").css({"padding":DivMorePadding+"px","top":(HeaderHeight-MoreInfoHeight-5)+"px","left":"8px","border-width":DivPaginationBorderWidth+"px"});
		$("#MorePage").css({"padding":DivMorePagePadding+"px","border-width":DivMoreBorderWidth+"px"});
		$("#MoreInfo").css({"height":MoreInfoHeight+"px","top":(HeaderHeight-MoreInfoHeight-DivMoreInfoPadding)+"px","padding":DivMoreInfoPadding+"px","border-width":DivMoreBorderWidth+"px"});
		$("#MoreSearch").css({"padding":DivMoreSearchPadding+"px","border-width":DivMoreBorderWidth+"px"});
		$("#Footer").css({"height":FooterHeight+"px"});
		$("#ContactInfo").css({"right":"10px","top":"3px"});
		// Setting Header content
		
			// Adding a logo
				$("#Header").append('<div id="Logo"><a href="' + HomeURL + '"><img id="LogoImg" src="' + LogoURL + '" border="0" alt="' + LogoAlt + '" width="' + LogoWidth + '" height="' + LogoHeight + '"></a><br>');
		
			// Creating main entries
			$("#Header").append('<div id="Navig"></div>');	
			$("#Navig").css({'left':LogoWidth+"px",'width':EntryMenuWidth+"px"});
						
			for (i=1; i<=NbRubriques; i++){
				if (Entry_Is_Active[i] == 1) {
					MyNewRubrique = '<div class="Rubrique" onclick="' + URLRubrique[i] + 'PSrubrique(' + i + ');" MenuRubrique="Rubrique' + i + '" id="Rubrique' + i + '">' + LabelRubrique[i] + '</div>';
					$("#Navig").append(MyNewRubrique);
				}
			}	
			
			// Adding languages
				$("#Header").append('<div id="Language"></div>');
				$("#Language").css({'left':LogoWidth+EntryMenuWidth+SpaceBeforeLanguageMenu+"px"});
				
				for (i = 1; i <= Nb_Available_Languages; i++) {
					if (MyLangue==i) {MyStyle='style="background-color:white;color:black;display:block"';} else {MyStyle='style="color:white;display:block"';}

					HtmlToAppend='<div onclick="redirect(\'LanguageChange.php?NewLanguage='+i+'\');" class="Rubrique" Langue="Language'+i+'" ' + MyStyle + '>' + LabelLanguage[i] + '</a></div>';				
					$("#Language").append(HtmlToAppend);
					
					
				}
				
			
			// Adding effect on entries
			$('div[MenuRubrique]').mouseover(function(){
				this.style.cursor="pointer";
				this.style.border="dotted 1px #CCC";
				});
			$('div[MenuRubrique]').mouseout(function(){
				this.style.cursor="auto";
				this.style.border="solid 1px transparent";
				});
			$('div[Langue]').mouseover(function(){
				this.style.cursor="pointer";
				this.style.border="dotted 1px #CCC";
				});
			$('div[Langue]').mouseout(function(){
				this.style.cursor="auto";
				this.style.border="solid 1px transparent";
				});
			$("#Cache").click(function(){
				$("#Cache2").hide();
				$("#Cache").fadeOut();
					});
					
		
	
	$("#Header").show();
	
	// Site launching
		if (typeof(Selected_Entry) != "undefined") {
			Entry=Selected_Entry;
		} else {
			Entry=DefaultEntry;
		}
	
	PSrubrique(Entry);
	
	if (typeof(Content) != "undefined" && Content != "" && Content !== null) {
		 PSloadSingleContent(Content,BaseTarget);
	} else {
		PSloadData(0, Entry, BaseTarget, '');
	}
	


}); 
