/**
 * JavaScript code to detect available availability of a 
 * particular font in a browser using JavaScript and CSS. 
 * 
 * Author : Lalit Patel
 * Website: http://www.lalit.org/lab/jsoncookies
 * License: Creative Commons Attribution-ShareAlike 2.5
 *          http://creativecommons.org/licenses/by-sa/2.5/
 * Version: 0.15 
 *          changed comparision font to serif from sans-serif, 
 *          as in FF3.0 font of child element didn't fallback 
 *          to parent element if the font is missing.
 * Updated: 09 July 2009 10:52pm
 * 
 */

/**
 * Actual function that does all the work. Returns an array with all the info.
 * This test will fail for the font set as the default serif font.
 * 
 * Usage: d = new Detector();
 *        d.test('font_name');
 */

var Detector = function(){
	var h = document.getElementsByTagName("BODY")[0];
	var d = document.createElement("DIV");
	var s = document.createElement("SPAN");
	d.appendChild(s);
	d.style.fontFamily = "sans-serif";			//font for the parent element DIV.
	s.style.fontFamily = "sans-serif";			//serif font used as a comparator.
	s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
	s.innerHTML        = "mmmmmmmmmmlil";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
	h.appendChild(d);
	var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
	var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
	h.removeChild(d);
	/* test
	 * params:
	 * font - name of the font you wish to detect
	 * return: 
	 * f[0] - Input font name.
	 * f[1] - Computed width.
	 * f[2] - Computed height.
	 * f[3] - Detected? (true/false).
	 */
	function debug(font) {
		h.appendChild(d);
		var f = [];
		f[0] = s.style.fontFamily = font;	// Name of the font
		f[1] = s.offsetWidth;				// Width
		f[2] = s.offsetHeight;				// Height
		h.removeChild(d);
		font = font.toLowerCase();
		if (font == "serif") {
			f[3] = true;	// to set arial and sans-serif true
		} else {
			f[3] = (f[1] != defaultWidth || f[2] != defaultHeight);	// Detected?
		}
		return f;
	}
	function test(font){
		f = debug(font);
		return f[3];
	}
	this.detailedTest = debug;
	this.test = test;	
}


var fontdet = new Detector();
if (!fontdet.test('Calibri, sans-serif')) {
    var linkel = document.createElement('link');
    linkel.href = '/wp-content/themes/optimics/css/optimics-arial.css';
    linkel.media = 'screen';
    linkel.rel = 'stylesheet';
    linkel.type = 'text/css';
    document.getElementsByTagName('head')[0].appendChild(linkel);
}


function showContactForm () {
    $('#cf').slideToggle('normal');
    _gaq.push(['_trackEvent', 'Kontakt form', 'show', location.pathname]);
}

// posílá kontaktní formulář
function contactSend () {
    if ($('#cf input[name=jmeno]').val()=='' && $('#cf input[name=email]').val()=='' && $('#cf input[name=telefon]').val()=='' && $('#cf textarea[name=zprava]').val()=='') return false;
    
    $('#cf #ajaxLoader').css('display', 'inline');
    $('#cf div:first-child').append('<input type="hidden" name="ajax" value="1" />');
    
    _gaq.push(['_trackEvent', 'Kontakt form', 'sending', location.pathname]);
    if ($('#cf input[name=email]').val() != '') createCookie('opmail', $('#cf input[name=email]').val(), 730);
    
    $.ajax({
    url: '/wp-content/themes/optimics/_mailform.php',
    data: $('#cf').serialize(),
    success: function(data) {
        $('#cf .contactHlaska').html(data + ' &nbsp; <span style="font-weight: normal; font-size: 0.9em;">(<a href="#" onclick="showContactForm(); return false;" style="color: #fff">zavřít</a>)</span>');
        $('#cf .contactHlaska').css('background-color', '#006600');
        $('#cf').css('height', '349px');
        $('#cf table').hide('fast');
        $('#cf .contactHlaska').slideDown('normal');
        _gaq.push(['_trackEvent', 'Kontakt form', 'wasSent', location.pathname]);
    },
    error: function () {
        $('#cf .contactHlaska').html('Bohužel se nepodařilo zprávu odeslat, buď je špatné připojení k internetu nebo chvilku nefunguje server. Zkuste to prosím znovu, nebo nás kontaktujte prostřednictvím e-mailu <a href="mailto:info@optimics.cz" style="color: #fff">info@optimics.cz</a>.');
        $('#cf').css('height', 'auto');
        $('#cf #ajaxLoader').css('display', 'none');
        $('#cf .contactHlaska').slideDown('normal');
        _gaq.push(['_trackEvent', 'Kontakt form', 'error', location.pathname]);
    },
    dataType: 'text',
    type: 'POST'
    });
    
    return false;
}

// tvoří žluté pozadí za nadpisem a perexem
function createYellowBackground () {
    var ye = $('<div style="background-color:#FBFAE5; position:absolute; width:100%; z-index:-1;" class="yellowDiv">&nbsp;</div>');
    var top = $('#pageIntro').offset().top;
    var height = $('#pageIntro').height();
    ye.css('top', top-25);
    ye.css('height', height+38);
    $('#obal3').after(ye);
}
$(document).ready(createYellowBackground);

// kontroluje, jestli má být separátor mezi "předchozí články" a "další články"; dále jestli vůbec je stránkování
function removeSeparator() {
    var html1 = $('.pages .prev').html();
    var html2 = $('.pages .next').html();
    if (html1 == '' | html2 == '') {
        $('.pages .separator').hide();
    }
    if (html1 == '' && html2 == '') {
        $('.pages').hide();
    }
}

$(document).ready( function () {
    $('#textKomentare').elastic();
    $('a.lightbox').fancybox( {
        overlayShow: false,
        hideOnContentClick: true
    } );

} );

function validateCommentForm () {
    $('.commentForm .errorHlaska').hide();

    var jmeno = $('.commentForm #jmeno').val();
    var email = $('.commentForm #email').val();
    var text = $('.commentForm #textKomentare').val();
    var logged = $('.commentForm #loggedIn').val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; // pro ověření e-mailu
    
    var chyba = false;
    var hlaska = '';
    if (text == '') {
        chyba = true;
        hlaska = 'Zadejte prosím text komentáře.'
    }
    else if (jmeno == '') {
        chyba = true;
        hlaska = 'Zadejte prosím své jméno.'
    }
    else if (email == '') {
        chyba = true;
        hlaska = 'Zadejte prosím svou e-mailovou adresu. Nemusíte se bát, na webu nebude zveřejněna.'
    }
    else if (!reg.test(email)) {
        chyba = true;
        hlaska = 'Zadejte prosím platnou e-mailovou adresu. Nemusíte se bát, na webu nebude zveřejněna.'
    }
    
    if (logged == '1') chyba = false;
    
    if (chyba) {
        $('.commentForm .errorHlaska').text(hlaska);
        $('.commentForm .errorHlaska').slideDown();
        _gaq.push(['_trackEvent', 'Komentar form', 'error', location.pathname, hlaska]);
        return false;
    }
    else {
        _gaq.push(['_trackEvent', 'Komentar form', 'sending', location.pathname]);
        createCookie('opmail', email, 730);
        return true;
    }
    
}

function toggleTable() {
    var t = $('#formatovani-komentare-tabulka')
    var o = $('#addComment').offset();
    var w = $('#addComment').width();
    t.css('left', o.left + w - 75 + 'px');
    t.css('top', o.top + 'px');
    t.slideToggle("normal");
    _gaq.push(['_trackEvent', 'Komentar form', 'showHelp', location.pathname]);
}

function mailForm (form) {
    var f = $(form);
    $('.errorHlaska', f).hide();
    var em = $('input[name=EMAIL]', f).val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; // pro ověření e-mailu
    if (!reg.test(em)) {
        $('.errorHlaska', f).text('Zadejte prosím platnou e-mailovou adresu.');
        $('.errorHlaska', f).css('background-color', '#FFFF00');
        $('.errorHlaska', f).css('color', '#000');
        $('.errorHlaska', f).slideDown();
        _gaq.push(['_trackEvent', 'Newsletter form', 'error', location.pathname]);
        return false;
    }
    else {
        createCookie('opmail', em, 730);
        _gaq.push(['_trackEvent', 'Newsletter form', 'sending', location.pathname]);
        return true;
    }
}

// cteni cookies
function readCookie(name) {
	var nameEQ = 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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// psani cookies
function createCookie(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=/";
}
