/**
   Alle selbstgechriebenen JavaScript Snippets sollten in dieser Datei sein

   Daegerlen.ch
   PA10 HS ZEMA1

   Thomas Mendler (ZHAW 2010)
   29.09.2010  created
   
   
**/


//this function is called when the dom tree is ready
//---------------------------------------------------------------------------------------------------------------------------
$(document).ready(function(){
	
	
	$('.tblZebra').css('background-color', '#ffffff');
	
	
	//start the slide show
	rotateHeader();
	
	//hide standard (no js) solution
		//GoogleMaps api -> hide error message if js is enabled
		//GoogleSearch -> hide standard solution js-Solution is shown
	$('#no_javascript').hide();
	
	//display gallery images with fancybox
	$('.fbImage').fancybox();
	
	//display frontend login with fancybox
	$("#loginFrontend").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,	
		'onComplete'	: function() {loginFrontend();}
	});
	
	//read textsize from cookie if there is one
	if(get_cookie('dae_font_size') != null){  
		changeTextSize($('#dynamic_content'), get_cookie('dae_font_size') + 'em');
	}

	//show text resize links
	//$('#header_font_resize').show();

	//handles text resize events
	/*
	$('#header_font_resize #font_small').click(function(event){
		event.preventDefault();	
		changeTextSize($('#dynamic_content'),'1.0em');
		set_cookie('dae_font_size', '1.0', 30);
	});
	$('#header_font_resize #font_middle').click(function(event){
		event.preventDefault();	
		changeTextSize($('#dynamic_content'),'1.2em');
		set_cookie('dae_font_size', '1.2', 30);
	});
	$('#header_font_resize #font_big').click(function(event){
		event.preventDefault();	
		changeTextSize($('#dynamic_content'), '1.8em');
		set_cookie('dae_font_size', '1.8', 30);
	});
	*/

	//show that a specific div is editable by changing the style
	$('.editable').hover(function(){$(this).addClass('divHighlight')}, function(){$(this).removeClass('divHighlight')});
	
	//show tiny editor for frontendediting
	$('.editable').click(function(){
		//get id of the div and its original content	  
		var idEditable = $(this).attr('id');
		var contentOrig = $(this).html();
		//insert tinymce
		tinyMCE.execCommand('mceAddControl', false, idEditable);
		//insert save and load buttons
		$(this).parent().append('<div id="frontendFormButtons"><div></div><input class="frontendForm" type="button" value="Speichern"/><input class="frontendForm" type="button" value="Abbrechen"/></div>');
		
		//save button event
		$('input[value="Speichern"].frontendForm').click(function(){
			saveFrontendContent(idEditable);
		});
		//cancel button event
		$('input[value="Abbrechen"].frontendForm').click(function(){
			cancelFrontendEditing(idEditable, contentOrig);
		});
	});
	
	
//end of jquery ready method
});



/*
* Function: cancel edit mode get old contents
----------------------------------------------------------------------------------
*/
function cancelFrontendEditing(idEditable, contentOrig){
	var edTiny = tinyMCE.get(idEditable);
	edTiny.setContent(contentOrig);
	tinyMCE.execCommand('mceRemoveControl', false, idEditable);
	$('#frontendFormButtons').remove();
}


/*
* Function: call frontendSave.php which saves content from tinymce in the database
----------------------------------------------------------------------------------
*/
function saveFrontendContent(idEditable){
	//get the content from the tiny editor
	var edTiny = tinyMCE.get(idEditable);
	var edTinyContent = edTiny.getContent();
	//encode contents from tiny otherwise we have problems storing it in the db
	edTinyContent = encodeURIComponent(edTinyContent);
	//show loadscreen
	edTiny.setProgressState(1);
	//call frontendSave.php via ajax
	$.ajax({
		type	:"POST",
		cache	:false,
		url		:"/include/frontendSave.php",
		data	:'content='+edTinyContent+'&id='+idEditable,
		dataType:'html',
		success :function(msg){
			tinyMCE.execCommand('mceRemoveControl', false, idEditable);
			$('#frontendFormButtons').remove();
		}
	});
}


/*
* Function: call login.inc.php and send parameters from fancybox login form
*-----------------------------------------------------------------------------
*/
function loginFrontend(){
	//call the loginform after button submit event
	$("#login_form").bind("submit", function() {
		$.fancybox.showActivity();
		$.ajax({
			type	:"POST",
			cache	:false,
			url		:"/include/login.inc.php",
			//get form parameters
			data	:$(this).serializeArray(),
			success	: function(data) {
				//display if login was successful in a new fancybox and refresh page after closing the box
				$.fancybox({
					'content'	: data,
					'onClosed'  : function(){location.reload();}
				});
			}
		});
		return false;
	});	
}


/*
* Function: this function is called after tinymce image_upload button was clicked
*-----------------------------------------------------------------------------
*/
function filebrowser(field_name, url, type, win) {
	//url to the filebrowser script
	fileBrowserURL = "/filebrowser/index.php?editor=" + 'tinymce';
	//set the filemanager dialog
	tinyMCE.activeEditor.windowManager.open({
		title: "PDW File Browser",
		url: fileBrowserURL,
		width: 800,
		height: 480,
		inline: 0,
		maximizable: 1,
		close_previous: 0
	},{
		window : win,
		input : field_name
	});		
}


/*
* Function: initializa tinymce editor
*-----------------------------------------------------------------------------
*/
tinyMCE.init({
	mode : "none",
	theme : "advanced",
	plugins : "paste,advlink, style,layer,table,preview",

	//buttons
	theme_advanced_buttons1 : "forecolor,backcolor,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,blockquote,|,undo,redo,|,link,unlink,image,|,help,code,cleanup", 
	theme_advanced_buttons3 :"tablecontrols,insertlayer",
	//call filebrowser method after image button was clicked
	file_browser_callback: "filebrowser",
	theme_advanced_background_color : "#FFFFFF",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,
	content_css : "theme/daegerlen_v4/main.css"
});



/*
* Function: changes textsize to specific size
*-----------------------------------------------------------------------------
*/
function changeTextSize(id, size){
	id.css('font-size',size);
}


/*
* Function:old jumpmenu functions from previous developers
*-----------------------------------------------------------------------------
*/

//jumpMenu from all over the site
function jumpMenu(selObj,restore){
	eval("this.location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

//jumpMenu from Onlineschalter
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/*
* Function: main rotate header function which calls rotate() in intervals
*-----------------------------------------------------------------------------
*/
function rotateHeader() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',10000);	
}


/*
* Function: rotate function which changes the header background image
*-----------------------------------------------------------------------------
*/
function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));
	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
};


/*
* Function: set a cookie with the textsize
*-----------------------------------------------------------------------------
*/
function set_cookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}


/*
* Function: get the textsize from cookie
*-----------------------------------------------------------------------------
*/
function get_cookie(name) {
    var name_eq = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(name_eq) == 0) return c.substring(name_eq.length,c.length);
    }
    return null;
}


