// JavaScript Document
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            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*1000));
                //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
        }
        // NOTE 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;
    }
};

$(document).ready(function(){
	replaceJovers();
	replaceAudio();
	replaceVideo();
});

function replaceJovers(){
	var jovers = $('.jover');
	jovers.each(function(index){
		var jover = $(this);		
		var over = jover.attr("data-jover");
		var out = jover.attr("data-joff");
		var href = jover.find("a").attr("href");
		jover.hover(function(){
			jover.css('backgroundImage',"url('"+over+"')");
			jover.css('cursor',"pointer");
		},function(){
			jover.css('backgroundImage',"url('"+out+"')");
			jover.css('cursor',"auto");
		});
		jover.click(function(){
			window.location.href = href;
		})
		//Preload
		var img = new Image();
		img.src = over;
	});
	
}


function replaceAudio(){
	var wordpress = $('.wordpress');
	var links = wordpress.find("a");
	links.each(function(index){
		var href = $(this).attr("href");
		var p = href.split(".");
		var ext = p[p.length-1];
		if(ext == "mp3"){
			$.fn.media.defaults.width = 250;
			$.fn.media.defaults.height = 24;
			$(this).media();
		}
	})
}

function replaceVideo(){
	var wordpress = $('.wordpress');
	var links = wordpress.find("a");
	links.each(function(index){
		var href = $(this).attr("href");
		var p = href.split(".");
		var ext = p[p.length-1];
		if(ext == "flv"){
			$.fn.media.defaults.width = 430;
			$.fn.media.defaults.height = 300;			
			$(this).media();
		}
	})
}


function submitNicEdit(d){
	try{
		for(var e in nicEditors.editors){
			for(var i in nicEditors.editors[e].nicInstances) nicEditors.editors[e].nicInstances[i].saveContent();	
		}
	}catch(e){
		
	}
}

function sum_px(a,b,op){
	var a = parseFloat(a);
	var b = parseFloat(b);
	if(!op) op = "+";
	
	if(op == "+") return (a + b) + "px";
	if(op == "-") return (a - b) + "px";
	
}

function grow(object,s,complete){
	if(!s) s = 350;

	object.wrap("<div class='j-outer'/>").wrap("<div class='j-inner'/>");
	var inner = object.parent();
	var outer = object.parent().parent();
	//outer.css("background-color","#ff00ff");
//	inner.css("background-color","#ffff00");

	object.append("<div class='clear'>&nbsp;</div>");
	var h = outer.height();
	object.children().last().remove();

	var mt = object.css("margin-top");
	var mr = object.css("margin-right");
	var mb = object.css("margin-bottom");
	var ml = object.css("margin-left");
	
	outer.css("margin-left",ml);
	outer.css("margin-top",mt);
	outer.css("margin-right",mr);

	var m = object.css("margin");
	
	object.css("margin",0);
	
	//Check for no bottom padding and an element inside with bottom padding!
	var pb = object.css("padding-bottom");
	if(!pb || pb == "0px"){
		var cmb = object.children().last().css("margin-bottom");
		if(cmb && cmb != "0px") mb = sum_px(mb,cmb);
	}
	
	inner.css("opacity",0);
	inner.animate({
		opacity: 1
	},s);
	
	outer.height(0);
	outer.animate({
			height:h,
			marginBottom:mb
		},
		s,
		function(){
			object.unwrap().unwrap();
			object.css("margin",m);
			if(complete) complete(object);
		});
}

