window.theme_load = function( avatar_option, avatar_box ){
	
	/* -------------------------------------------------------------------------------------------------------
	 * -------------------------------------------------------------------------------------------------------
	 * -------------------------------------------------VIEW--------------------------------------------------
	 * -------------------------------------------------------------------------------------------------------
	 * -----------------------------------------------------------------------------------------------------*/
	
	//-------------------------load avatar big + add mask_avatar----------------------------------------------	
	if($("#identity_avatar_img").length){
		var urlAvatar = $('#identity_avatar_img').attr('src').replace(/avatar_cp\.jpg/,'avatar_cp_big.jpg');
		replaceAvatar = function(){
			$('#identity_avatar_img').attr('src',urlAvatar);	
			$('<div id="mask_avatar" class="unload">').insertAfter('#identity_avatar_img');
		}
		replaceAvatar();
	};
	//-------------------------add div class "widget-bottom"--------------------------------------------------	
	addBottomWidget = function(){
		$('#cv').find('.widget').each(function(){		
			if($(this).find('.widget-bottom').length == 0){
				$('<div class="widget-bottom unload"></div>').appendTo($(this));
			}
		});
	}
	addBottomWidget();	
	
	//-------------------------add div class "clear" ul skills ---------------------------------------	
	addClearSKills = function(){
		$('#cv').find('.widget_skills ul').each(function(){
			$('<span class="clear unload"></span>').appendTo($(this))
		});
	}
	addClearSKills();
	
	//-------------------------add div class "clear" ul hobbies ---------------------------------------	
	addClearHobbies = function(){
		$('#cv').find('.widget_hobbies ul').each(function(){
			$('<span class="clear unload"></span>').appendTo($(this))
		});
	}
	addClearHobbies();
	
	//--------------------------add title widget contact-------------------------------------------------------
	addContactTitle = function(){
		$('<h3 class="unload">Contact</h3>').insertBefore('#col_1 .widget_contact ul.info2');
	};
	addContactTitle();
	
	//--------------------------add title widget identity-------------------------------------------------------	
	addInfosTitle = function(){
		$('<h3 class="unload">Infos</h3>').insertBefore('#col_1 .widget_identity ul.info1');
	}
	addInfosTitle();
	
	//--------------------------widget userlinks--------------------------------------------------	
	
	userlink = function(){
		$('#col_1 #userlinks .userlink').find('a').each(function(){
			var linkString = $(this).text();
			$(this).append('<span class="unload"><em>' + linkString + '</em></span>');
		});		
		if($('#col_1 #userlinks .userlink').length > 3){
			$('<span class="viewOtherLinks unload">More</span>').appendTo('.widget_userlink');
			$('<div id="otherLinks">').insertBefore('#col_1 #userlinks .userlink:nth-child(4)');		
			$('#col_1 #userlinks .userlink:gt(2)').each(function(){
				$(this).appendTo('#otherLinks');
			});
			$('.viewOtherLinks').bind('click',function(){
				$('#otherLinks').slideToggle('100');
				
				if($(this).hasClass('open')){
					$(this).removeClass('open');
				}else{
					$(this).addClass('open');
				}
			});
			$('body').click(function() {
				if($('.viewOtherLinks').hasClass('open')){
					$('.viewOtherLinks').click();
				}
			});
			$('.widget_userlink .viewOtherLinks').click(function(event){
				event.stopPropagation();
			});
			$('.widget_userlink #otherLinks').click(function(event){
				event.stopPropagation();
			});
			
		};
		$('#col_1 .widget_userlink .options a.edit').bind('click',function(){
			setTimeout(function(){ refresh_userlinks(); } , 1000 );
		});
		$('#col_1 .widget_userlink').css('opacity','1');
	};
	
	userlink();
	
	//-----------------------------fontsize userName autofit-------------------------------------

	$.fn.textfill = function(options) {
		var fontSize = options.maxFontPixels;
		var ourText = $('span', this);
		var maxHeight = $(this).height();
		var maxWidth = $(this).width();
		var textHeight;
		var textWidth;
		do {
			ourText.css('font-size', fontSize);
			textHeight = ourText.height();
			textWidth = ourText.width();
			fontSize = fontSize - 1;

		} while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize > 3);
		return this;
	};
	
	var noiseUserName =  function noiseUserName(){
		$('#userCvTitle').textfill({ maxFontPixels: 40 });
		$('#userFirstName').textfill({ maxFontPixels: 65 });
		$('#userLastName').textfill({ maxFontPixels: 65 });
	}
	noiseUserName();
	
	$('#userCvTitle').bind('cvTitleChange',noiseUserName);
	
	/* -------------------------------------------------------------------------------------------------------
	 * -------------------------------------------------------------------------------------------------------
	 * -------------------------------------------------Edit--------------------------------------------------
	 * -------------------------------------------------------------------------------------------------------
	 * -----------------------------------------------------------------------------------------------------*/
	
	if($("#configbar").length){
		
		//--------------------------userlinks (edit)-------------------------------------------
		refresh_userlinks = function(){
			$('#col_1 .widget_userlink .save').bind('click',function(){
				setTimeout(function(){ userlink(); } , 1000 );
			});
			$('#col_1 .widget_userlink .cancel').bind('click',function(){
				setTimeout(function(){ userlink(); } , 1000 );
			});
		};
		
		//--------------------------add title widget contact (edit)-------------------------------------------
		$('#col_1 .widget_contact a.edit').bind('click' , function(){
			setTimeout(function(){
				$('<h3 class="unload">Contact</h3>').insertBefore('#col_1 .widget_contact form');
				$('#col_1 .widget_contact.widget_edit .save').bind('click',function(){
					setTimeout(function(){ addContactTitle(); } , 1000 );
				});
				$('#col_1 .widget_contact.widget_edit .cancel').bind('click',function(){
					setTimeout(function(){ addContactTitle(); } , 1000 );
				});
			} , 1000 );		
		});
		
		//-------------------------load avatar big + add mask_avatar + infos title (edit)---------------------------------------
		$('.widget_identity a.edit').bind('click',function(){
			setTimeout(function(){
				replaceAvatar();
				$('.widget_identity .save').bind('click',function(){
					setTimeout(function(){
						replaceAvatar();
						addInfosTitle();
					} , 1000 );
				});
				$('.widget_identity .cancel').bind('click',function(){
					setTimeout(function(){
						replaceAvatar();
						addInfosTitle();
					} , 1000 );
				});
			} , 1000 );
		});

		//-------------------------Widget_add / edit load widget-bottom---------------------------------------
		$(".widget_add form .save").live("mouseup", function(){
			setTimeout(function(){
				addBottomWidget();
			} , 1500 );
		});
		$(".widget_edit form .save").live("mouseup", function(){
			setTimeout(function(){
				addBottomWidget();
			} , 1500 );
		});
		$(".widget_edit form .cancel").live("mouseup", function(){
			setTimeout(function(){
				addBottomWidget();
			} , 1500 );
		});
		$(".widget .options a.edit").live("mouseup", function(){
			setTimeout(function(){
				addBottomWidget();
			} , 1500 );
		});
		//-----------------------Widget skills addClear edit-----------------------------------------------
		refreshAddClearSKills = function(){
			$('.widget_edit .save').bind('click',function(){
				setTimeout(function(){
					addClearSKills();
				} , 1000 );
			});
			$('.widget_edit .cancel').bind('click',function(){
				setTimeout(function(){
					addClearSKills();
				} , 1000 );
			});
		};
		$('.widget_skills .options a.edit').bind('click',function(){
			setTimeout(function(){
				refreshAddClearSKills();
			} , 1000 );
		});
		
		//-----------------------Widget hobbies addClear edit-----------------------------------------------
		refreshAddClearHobbies = function(){
			$('.widget_edit .save').bind('click',function(){
				setTimeout(function(){
					addClearHobbies();
				} , 1000 );
			});
			$('.widget_edit .cancel').bind('click',function(){
				setTimeout(function(){
					addClearHobbies();
				} , 1000 );
			});
		};
		$('.widget_hobbies .options a.edit').bind('click',function(){
			setTimeout(function(){
				refreshAddClearHobbies();
			} , 1000 );
		});
	};
}





window.theme_unload = function(){
	$('#userCvTitle').unbind('cvTitleChange');
	//-------------------------remove all "unload" elements--------------------------------------------------------
	$('.unload').remove();
	$('#userName span').attr('style','');
	//-------------------------unloadload avatar big--------------------------------------------------------
	if($("#identity_avatar_img").length){
		var urlAvatar = $('#identity_avatar_img').attr('src').replace(/avatar_cp_big\.jpg/,'avatar_cp.jpg');
		$('#identity_avatar_img').attr('src',urlAvatar);
	};
};
