/* mindaugas */
function log(mssg){
    if(typeof console != undefined){
        console.log(mssg);
    }
    return true;
}
function getCookie(c_name){
    if (document.cookie.length>0){
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return null;
}
function setCookie(c_name,value,expireseconds){
    var exdate=new Date();
    exdate.setTime(exdate.getTime()+expireseconds * 1000);
    document.cookie=c_name+ "=" +escape(value)+((expireseconds==null) ? "" : ";expires="+exdate.toUTCString())+ ';path=/';
}

$(function(){
	var b_height = $('body').height();
	var w_height = $(window).height();
	var content = $('#content');
	var c_height = content.height();
	
	if(w_height > b_height){
		var difference = parseInt(w_height - b_height);
		content.height(parseInt(c_height + difference));
	}	
});

/**
 * 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";			//font for the parent element DIV.
    s.style.fontFamily = "sans";			//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 myListener = new Object();
var player_obj = null;
var mp3_file = template_url + '/music/bobbismith.mp3';
var title = 'Come Back To Me';
var pl_sec =  0;

function display_clock(time){   
    var Total_seconds = Math.round(time/1000);
    var Minutes = parseInt(Total_seconds / 60);
    var Seconds = parseInt(Total_seconds % 60);
    $('#mp_duration').html( ((Minutes < 1) ? '0' : Minutes) +':'+ ((Seconds < 1) ? '00' : ((Seconds < 10) ? '0' + Seconds : Seconds)) );
}
function _getFlashObject(){
    if(player_obj == null){
        player_obj = document.getElementById("music_player");
    }
    return player_obj;
}
function get_seconds(){
    pl_sec = getCookie('pl_sec');
    pl_sec = (pl_sec == null) ? 0 : pl_sec;
    return pl_sec;
}
function play(){
    player_obj.SetVariable("method:setPosition", get_seconds());
    player_obj.SetVariable("method:play", '');
}
function pause() {
    _getFlashObject().SetVariable("method:pause", "");
}
function stop() {
    _getFlashObject().SetVariable("method:stop", "");
}
var afterInit = false;
var afterAfter = false
myListener.onInit = function(){
    _getFlashObject();
    player_obj.SetVariable("method:setPosition", get_seconds());
    afterInit = true;
};
myListener.onUpdate = function(){
    pl_sec = myListener.position;
    display_clock(pl_sec);
    if(afterInit && pl_sec != undefined){
        if(!afterAfter && pl_sec != 0){
            player_obj.SetVariable("method:setPosition", get_seconds());
            player_obj.SetVariable("method:setVolume", 100);
            afterAfter = true;

        }
        setCookie('pl_sec', pl_sec, 15);
    }
}

$(function(){
	
	var music_player = $('#music_player');
	
	if(music_player.length > 0){
		var flashvars = {
			mp3: mp3_file,
			autoplay: 1,
			loop: 1,
			id: 'music_player',
			listener: 'myListener',
			interval: 1000,
			enabled: true,
			volume: 0
		};
		var params = {
			AllowScriptAccess: 'always',
			menu: "false",
			base: "."
		};
		swfobject.embedSWF(template_url + "/player_mp3_js.swf", "music_player", "0", "0", "9.0.0", template_url + "/expressInstall.swf", flashvars, params, {}); 
		$('#mp_now_playing').html(title);
		
		$('#mp_action').click(function(){
			if($(this).is('.mp_play_button')){
				pause();
				$(this).removeClass('mp_play_button');
				$(this).addClass('mp_pause_button');
			}else if($(this).is('.mp_pause_button')){
				play();
				$(this).removeClass('mp_pause_button');
				$(this).addClass('mp_play_button');
			}
		});
	}
});


/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// jQuery
$(document).ready(function(){
    // Change menu font if it exists in the users' machine
    Font_detector = new Detector();
    if (Font_detector.test('Trajan Pro')) {
        $('#navigation a').css('font',"11pt 'Trajan Pro', Georgia, Serif");
    }
    // Music Player controller
    /*
    $('#mp_now_playing').live("click",function(){
        //musicPlayer.sendEvent("NEXT");
    });
    */
   
   var newsletter = $('#newsletter');
    // Email Signup form
    $('#newsletter_email').live("focus",function(){
        if ($(this).val() == 'bobbi-smith@gmail.com')
            $(this).val('');
    });
    $('#newsletter_email').live("blur",function(){
        if ($(this).val() == '')
            $(this).val('bobbi-smith@gmail.com');
    });
    $('#newsletter form').live("submit",function(){
        Email_address = $('#newsletter_email').val();
        if (Email_address != '') {
            newsletter.html('<img src="' + template_url + '/img/loading.gif" alt="" />');
            $.ajax({
                url: template_url + "/ajax/email_signup.php",
                data: "email=" + Email_address,
                success: function(result) {
                    if (result == 'OK') {
                        newsletter.html('You have succesfully signed up for a newsletter');
                        $.cookie('newsletter_subscription', 1, {
                            expires: 1
                        });
                    }
                    else {
                        newsletter.html(result+'<form method="post"><input id="newsletter_email" type="text" value="bobbi-smith@gmail.com" /><div class="separator"></div><input id="newsletter_submit" type="image" src="wp-content/themes/BobbiSmith/img/newsletter_submit.png" /></form>');
                    }
                }
            });
        }
        return false;
    });
});

$(function(){
	var content = $('#content');
	if(content.length > 0){
		var content_height = content.height();
		if(content_height < 480){
			content.height(480);
		}
	}
});


