/**
 * Contains generic javascript functions
 * 
 * All scripts are using jQuery framework
 * 
 */

/**
 * Handler for disabled functions 
 */
$(document).ready(function(){
	$(".funcDisabled").each(function(){
		$(this).click(function(){alert('Tato funkce ještě není k dispozici.'); return false;});
	});
});


/**
* Left menu handler - opens and closes second level of menu
**/
$(document).ready( function(){
	// script changing class for graphic view of opened second level
	function changeView(obj) {
		var mySection = $(obj).parent().get(0);
		if($(mySection).hasClass("emProfMinimize")) {
			$(mySection).removeClass("emProfMinimize");
		} else {
			$(mySection).addClass("emProfMinimize");
		}
	}

	$('#leftMenu li ul').slideUp("fast");
	$('#leftMenu .opened ul').slideToggle('normal');
	$('#leftMenu .opened').addClass("emProfMinimize");
	$('#leftMenu li a').each(function() {
		var parent = $(this).parent().get(0);
		if($(parent).children("ul")) {
			$(this).click(function() {
				$('ul', parent).slideToggle('normal', function() {changeView(this);});
				//return false;
			});
		}
	});
});

/**
*  Upload files form in dialog box
**/
$(document).ready( function(){
	var checkboxes = $(':radio.group');
	checkboxes.click(function(){
	  var self = this;
	  checkboxes.each(function(){
	    if(this!=self) this.checked = ''
	  })
	})

});

function reset_fileinput() {
    $('.fileinput').each(function(){
    $(this).html($(this).html());
    });
    
    $(':radio.group').each(function(){
    	this.checked = '';
    });
    $.fn.colorbox.close();
}
function close_dialog() {
	$.fn.colorbox.close();
}

/**
*  attached fiiles
**/
$(document).ready(function(){
 
    $(".attachements a[href$='.pdf']").addClass("PDF");
    $(".attachements a[href$='.doc']").addClass("DOC");
    $(".attachements a[href$='.xls']").addClass("XLS");
    $(".attachements a[href$='.png']").addClass("PIC colorbox");
    $(".attachements a[href$='.jpg']").addClass("PIC colorbox");
    $(".attachements a[href$='.gif']").addClass("PIC colorbox");
    $(".attachements a[href$='.zip']").addClass("ZIP");
    $(".attachements a[href$='.rar']").addClass("ZIP");
    
});

$(document).ready(function(){
	 
    $(".filesListing a[href$='.pdf']").addClass("PDF");
    $(".filesListing a[href$='.doc']").addClass("DOC");
    $(".filesListing a[href$='.xls']").addClass("XLS");
    $(".filesListing a[href$='.png']").addClass("PIC colorbox");
    $(".filesListing a[href$='.jpg']").addClass("PIC colorbox");
    $(".filesListing a[href$='.gif']").addClass("PIC colorbox");
    $(".filesListing a[href$='.zip']").addClass("ZIP");
    $(".filesListing a[href$='.rar']").addClass("ZIP");
    
});
/**
 * Lightbox inicialization
 */
$(document).ready(function(){
	$('.lightbox').colorbox();
	$('.colorbox').colorbox();
	$('.wallAddFile').colorbox({href: "#dialog", inline: true, overlayClose: false});
	$('.emailAddFile').colorbox({href: "#dialog", inline: true, overlayClose: false});
	
	$(".flash").corner("5px");
	$(".results ul li").corner("10px");
});

/**
 * Preparsovani odkazu z textu
 */
//vytvoreni odkazu
$(document).ready(function() {
	$('.postContent div, .schoolInfo').each(function(i){
		var theText = this.innerHTML;
		//cokoli co zacina na http, ftp, nebo www | cokoli co nema v sobe zadny oddelovac a ma v sobe aspon dve tecky
		theText = theText.replace(/(((http|ftp|www)[^ \n\r\t<\(\)\[\]]*)|(([a-zA-Z0-9]{1,}\.){2,}([a-zA-Z0-9]{1,})))/gi, replaceLinks);
		this.innerHTML = theText; 
	});
});

function replaceLinks(txt) {
	var link = txt;
	//doplneni protokolu, pokud neni
	if (link.search('http') > -1 || link.search('ftp') > -1) {
	} else {
		link = 'http://'+link;
	}
	
	var out = "";
	// pokud obsahuje domenu youtube.com, vytvorime standartni object+embed pro zobrazeni videa
	if(link.search('youtube.com') > -1) {
		link = link.replace('/watch?v=', '/v/');
		out = '<object width="240" height="148"><param name="movie" value="'+ link + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+ link +'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="240" height="148"></embed></object>';
	} else { // jinak udelame jenom odkaz
		out = "<a href='"+link+"' target='_blank'>"+txt+"</a>";
	}
	
	//zkraceni textu v odkazu
	/*if (txt.length > 80) {
		var le = Math.min(txt.indexOf('?'), txt.lastIndexOf('/'));
		if (le > 80) {
			le = txt.indexOf('/', 7);
		}
		txt = txt.substr(0, le);
	}*/
	
	
	return out;
}

$(document).ready(function() {
	$('.state select').attr('onclick','return false');
});
