/* TODO cancellare -- solo per debug */
var gup = function(name) {
    var results = (new RegExp("[\\?&]" + name + "=([^&#]*)")).exec(window.location.href);
    if (results == null) { return "" }
    else { return results[1] }
};

var timeDiff = {
    setStartTime: function() {
        d = new Date();
        time = d.getTime();
    },

    getDiff: function() {
        d = new Date();
        return (d.getTime() - time);
    }
}

var writeSpeed = function(text) {
    jQuery("#speed").html(text);
}
/* ---- fine -------- */
var debug = function(text) {
    try {
        if (allow_debugging)
            jQuery("#debug").append("<p>" + text + "</p>");
    } catch (e) { }
}

var currentSummaryPopup;
var FoxShowPopup = function() {
    var _cache = new Array();

    var _buildPopup = function(id, html, x, y) {
				if(currentSummaryPopup != id) return;
				popupdiv = jQuery("div#popup");

				if(popupdiv.length > 0) {
					try {
						popupdiv = popupdiv[0];

						popupdiv.html(html)
									.css("left", (x ) + "px")
			            .css("top", (y - 145) + "px");
					} catch (e) {}
				} else {
					popupdiv = jQuery(document.createElement("div"));
					popupdiv.attr('id','popup');
	        popupdiv.addClass("summary")
	            .addClass("temp-summary")
	            .html(html)
	            .css("left", (x ) + "px")
	            .css("top", (y - 145) + "px")
	            .hover(
	                function() { },
	                function() {
	                    jQuery(this).remove();
	                    currentSummaryPopup = null;
	                }
	            );

	        jQuery("body").append(popupdiv);
					
				}
        
    }

    return {
        GetShowDetail: function(id, x, y) {
						currentSummaryPopup = id;
            //if (currentSummaryPopup == id)
            //    return;
            if (_cache[id] != undefined) {
                _buildPopup(id, _cache[id], x, y);
            } else {
                /* TODO inserire la url corretta */
                $.ajax(
                    { "url": "/calendar/program/" + id,
                        "success": function(data) {
                            _cache[id] = data;
                            _buildPopup(id, _cache[id], x, y);
                        },
                        "error": function(data) {
                            /* non metto in cache l'errore */
                            _buildPopup(id, _cache[id], x, y);
                        }
                    });
            }
        }
    }
} ();

var needToCloseTent = true;
var checkTent = function() {
    if (needToCloseTent)
        $(".showtent").fadeOut("fast");
}

function _showLoadingModal() {
    var screen_w = $("body").width();
    var screen_h = $("body").height();

    var $div = jQuery(document.createElement("div"));
    var $spinner = jQuery(document.createElement("div"));
    $div.css("width", $(document).width())
            .css("height", $(document).height()).attr("id", "loading").fadeTo(10, 0.7);

    $spinner.css("top", "300px").attr("id", "loading-spinner");
    $img = jQuery(document.createElement("img")).attr("src", "/images/spinner.gif");
    $spinner.append($img);
    jQuery("body").append($div);
    jQuery("body").append($spinner);
}
function _hideLoadingModal() {
    jQuery("#loading").remove();
    jQuery("#loading-spinner").remove();
    jQuery(this).remove();
}


var _needToScrollTelefilm = false;
function scrollTelefilm(dir) {
    if (_needToScrollTelefilm) {
        var $cont = $(".telefilm-list .cont-telefilm ul");
        var top = parseInt($cont.css("top"));
        if (dir == 1 && top >= 0) {
            return;
        }
        if (dir == -1) {
            var deltay = 0 - ($cont.height() - $(".telefilm-list .cont-telefilm").height());
            if (top <= deltay)
                return;
        }
        $cont.css("top", top + (3 * dir));
        setTimeout(function() {
            scrollTelefilm(dir);
        }, 40);
    }
}
function startScrollTelefilm(dir) {
    _needToScrollTelefilm = true;
    scrollTelefilm(dir);
}
function stopScrollTelefilm() {
    _needToScrollTelefilm = false;
}
function calculateWeek(curr, type) {
    var y = curr.substr(0, 4);
    var m = parseInt(curr.substr(4, 2).substr(0, 1) == "0" ? curr.substr(5, 1) : curr.substr(4, 2)) - 1;
    var d = curr.substr(6, 2).substr(0, 1) == "0" ? curr.substr(7, 1) : curr.substr(6, 2);

    var currDate = new Date();
    currDate.setFullYear(y);
    currDate.setMonth(m);
    currDate.setDate(d);

    var t = currDate.getTime();

    var deltaMS = t + ((1000 * 60 * 60 * 24 * 7) * type);
    var newDate = new Date();
    newDate.setTime(deltaMS);

    var newY = newDate.getFullYear();
    var newM = (newDate.getMonth() + 1) + "";
    var newD = (newDate.getDate() + "");

    return newY + (newM.length == 1 ? "0" + newM : newM) + (newD.length == 1 ? "0" + newD : newD);
}
function calculateNextWeek(curr) {
    return calculateWeek(curr, 1);
}
function calculatePreviousWeek(curr) {
    return calculateWeek(curr, -1);
}
function shiftWeekStrip(html, dir) {
    var delta = 959 * (0 - dir);
    if (dir == 1) {
        jQuery(".week-strip").append(html);
    } else {
        jQuery(".week-strip").prepend(html).css("left", "-959px");
        delta = 0;
    }
    
    jQuery(".week-strip").animate({ "left": delta }, function() {
        var selector = dir == 1 ? "first" : "last";
        jQuery(".week-strip").find(".week:" + selector).remove();
        jQuery(".week-strip").css("left", "0px");
        initTelefilmStrip();
        bindPopup(currentTimeSpan);Cufon.refresh();
    });
}

function bindThumbClick (){
	jQuery(".cont-thumb").find("img").unbind("click").click(function(){
		var source = jQuery(this);
		var img = new Image();
		img.src = source.attr("src").replace(/SM_01/, "BIG_01");
		var dest = jQuery(".cont-gallery img");
		dest.attr("src", img.src);
		return false;
	});
}

function shiftStripPagerGallery(selector, html, time) {
    var elem = jQuery(selector);	
    var delta = elem.outerWidth(true);
    elem.animate({ 
	"left": -delta
    }, time, function(){
	elem.html( jQuery(html).html() );
	Cufon.refresh();
	bindThumbClick();
	elem.animate({ 
		"left": 0
    	}, time, function(){});
    });
}


