// czyszczenie defaultowego tekstu w formularzach

function clearDefault(el) {
	if (!el.notDefault) {
		el.notDefault = 1;
		el.value = '';
	}
}

/**
 * jQuery 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') {
        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();
        }
        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 {
        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]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


$(function() {
	// menu glowne
	
	$("#mainMenu").each(function() {
		var mainMenu = this;
		var active = parseInt($.cookie('tv4menuActiveTab'));
		if (active > 0) {
			$("li.submenu", mainMenu).each(function() {
				if ($("ul", this).get(0).id == "mainMenu" + active) {
					$(this).addClass("active");
				} else {
					$(this).removeClass("active");
				}
			});
		} else {
			$("li.submenu", mainMenu).eq(0).addClass("active");
		}
		$("li.submenu > a", mainMenu).mouseover(function(event) {
			$("li.submenu", mainMenu).removeClass("active");
			$(event.target).parent().addClass("active");
			return false;
		});
		$("li.submenu a", mainMenu).click(function(event) {
			var ul = $("ul", $(this).parents("li.submenu")).get(0);
			var n = 0;
			if (ul.id) {
				n = ul.id.replace("mainMenu", "");
			}
			$.cookie('tv4menuActiveTab', n, { path : "/"});
			return true;
		});
	});

	// scrollowanie `z ostatniej chwili`
	
	$("#content div.homePage div.latestInfo div.text span.t").each(function() {
		var step = 2;
		var w = 0;
		var scroller = $(this);

		$("> span", scroller).each(function(){
			w += $(this).width() + parseInt($(this).css("padding-right"));
		})
		
		var text = this.innerHTML;
		var times = Math.ceil(615 / (w+1)) + 1;
		for (var i = 0; i < times; i++) {
			this.innerHTML += text;
		}

		var m = 0;
		setInterval(function() {
			m += step;
			if (m >= w)
				m = m - w;
			scroller.css("left", -m);
		}, 50);

		scroller
			.width(times * w + 2000)
			.mouseover(function () {
				step = 0;
			})
			.mouseout(function () {
				step = 2;
			})
	});

	// scrollowanie w galerii

	$("#content div.main div.gallery").each(function() {
		$("div.photos", this).css("overflow", "hidden");
		$("a.next", this).show().mouseover(function() {
			var a = this;
			var parent = $(this).parent();
			var ul = $("ul.photos", parent);
			$("a.prev", parent).show();

			a.interval = window.setInterval(
				function() {
					var m = parseInt(ul.css("margin-left"));
					if ( (-m ) < (ul.width() - 360)) {
						ul.css("margin-left", m - 5)
					} else {
						window.clearInterval(a.interval);
						$(a).hide();
					}
					return false;
				}, 40);

		}).mouseout(function() {
			if (this.interval)
				window.clearInterval(this.interval);
		}).click(function() {
			return false;
		});
		
		$("a.prev", this).mouseover(function() {
			var a = this;
			var parent = $(this).parent();
			var ul = $("ul.photos", parent);
			$("a.next", parent).show();

			a.interval = window.setInterval(
				function() {
					var m = parseInt(ul.css("margin-left"));
					if ( m < 0) {
						ul.css("margin-left", m + 5)
					} else {
						window.clearInterval(a.interval);
						$(a).hide();
					}
					return false;
				}, 40);
		}).mouseout(function() {
			if (this.interval)
				window.clearInterval(this.interval);
		}).click(function() {
			return false;
		});
	});
	
	// fix dla ie6: czasem zle pozycjonuje ten element, dlatego wymuszamy przerenderowanie
	// i przy okazji obnizamy o 1px
	
	if ($.browser.msie) {
		var elm = $("#content div.main div.bottomLnk");
		elm.css("bottom", 0);
		elm.css("bottom", 68);
	}
	
});