var Redefine = {
	construct: function() {		
		Redefine.clearInput("query");
		Redefine.bigBoxSlider();
		Redefine.newsSlider();
		

		var titleToReplace = $("title").html();
		titleToReplace = titleToReplace.replace("- Redefine","");
		$("#subHeader h2").html(titleToReplace);
		
	
		/* wlaczanie formatowania daty */
		var datePages = [2148, 2149, 2141, 2142];
		$.each(datePages, function(intIndex, value) {
			var pattern = new RegExp(datePages[intIndex]);
			if (pattern.test(document.location.href)) {
				Redefine.trimDate();
			}
		});
		
		var functionSet = {
			2142: "applyImageSpace",
			2164: "dictionaryPager", 
		//	2152: "videoGallery", 
			2169: "enlargeMap",
			2166: "applyJobForm"
		};
		$.each(functionSet, function(i, n) {
			var pattern = new RegExp(i);
			if (pattern.test(document.location.href)) {
				if(/\w/.test(n)) {
					eval("Redefine."+n+"()");
				}
			}
		});
		
		/* Pager Aktualnosci/Komunikaty prasowe */
		Redefine.entriesPager();
		
		Redefine.applyImageCentering();
		
		$(".getIplaBox").click(function() {
			window.open($(this).attr("href"));
		});
		
	}/* constructor END */,
	clearInput: function(input) {
		input = "input[name="+input+"]";
		var inputValue = $(input).val();
		$(input).focus(function() {
			if($(this).val()==inputValue) {
        		$(input).val("");
			}
		});
		$(input).blur(function() {
			if($(input).val()=="") {	
				$(input).val(inputValue);
			}
		});
	},
	bigBoxSlider: function() {
		var slides = $(".bigNewsBox").size();
		$("#bigBoxContent div").each(function() { $(this).hide(); });
		
		var rand = Math.floor(Math.random()*slides);
		var randBox = $("#bigBoxContent .bigNewsBox").eq(rand);
		randBox.show();
		randBox.find("div").show();
		$("#bigBoxNav a").eq(rand).addClass("bigBoxActive");
		
		function toggleSlides(slideID) {
			$("#bigBoxContent div").each(function() { $(this).hide(); });
			$(".bigNewsBox .wrapperBox").eq(slideID).css("display", "block");
			$(".bigNewsBox").eq(slideID).fadeIn("slow");
			$(".bigBoxActive").attr("class", "");
			$("#bigBoxNav a").eq(slideID).addClass("bigBoxActive");
		}
				
		$("#bigBox").everyTime(10000, function(){
			rand++;
			if(rand==slides) rand = 0;
			toggleSlides(rand);
		});
		
		var bigBoxClick = $("#bigBoxNav a").click(function() {
			$("#bigBox").stopTime();
			toggleSlides(bigBoxClick.index(this));
			return false;
		});
	},
	newsSlider: function() {
		$("#newsContent").cycle({
			fx: 'fade',
			timeout: 5000,
			pager: "#newsNav"
		});
	},
	trimDate: function() {
		$(".entryDate").each(function() {
			var dateToTrim = $(this).html();
			$(this).html(dateToTrim.split(" ")[0]);
		});
	},
	dictionaryPager: function() {
		var glyphs = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "W", "V", "X", "Y", "Z"];
		
		function switchLetter(letter) {
			$(".dictionaryPager li a.activeSlide").removeClass("activeSlide");
			$(".dictionary li").each(function() { 
				$(this).hide(); 
			});
			$(".letter-"+letter).each(function() {
				$(this).show();
			});
			$(".pager-"+letter).addClass("activeSlide");
		}
		
		$.each(glyphs, function(intIndex, value) {
			if($(".letter-"+value).is("li")) {
				$(".dictionaryPager").append('<li><a class="pager-'+value+'" href="#">'+value+"</a></li>");
			}
		});
		
		$(".dictionaryPager li a").click(function() {
			switchLetter($(this).html());
		});
		
		var firstLetter = $(".dictionaryPager li a:nth-child(1)").html();
		
		switchLetter(firstLetter);
	},
	entriesPager: function() {
	
    if($(".entrySlide").length > 0)
    {
      $(".entrySlide").each(function() {
        if(!$(this).children().is("div")) $(this).remove();
      });
		
      var subContentHeight = subContentHeight = $("#subContent").height();
      var entrySlideHeight = entrySlideHeight = $(".entrySlide").eq(0).height();
		
      $("#subContent").height(entrySlideHeight+'px');
		
      $(".entriesContent").cycle({
        speed: 1,
        timeout: 0,
        pager: '.entriesPager'
      });
     }
	},
	handleVods: function(vodID) {
		
		if(/vod\d/.test(vodID)) {
			var json = $.toJSON(eval(vodID));
		}
		
		$("#videoDetails h4").html($.evalJSON(json).Name);
		$("#videoDetails p").html($.evalJSON(json).Description);
		$("#videoContainer").html('<a href="ipla://playvod-1|'+$.evalJSON(json).md5+'"><img src="/templates/redefine/gfx/vod/'+$.evalJSON(json).vodId+'_max.jpg" /></a>');
		$(".videoWatchExternal").attr("href", "http://ipla.tv/vod/h/"+$.evalJSON(json).md5+'/');
		$("input[name=get_video_code]").attr('value', '<script type="text/javascript" src="http://ins.ipla.tv/embed_'+$.evalJSON(json).md5+'.js"></script><noscript><a href="http://ipla.tv">Oglądaj w ipla</a></noscript>');
		$("input[name=get_video_code_big]").attr('value', '<script type="text/javascript" src="http://ins.ipla.tv/embed_big_'+$.evalJSON(json).md5+'.js"></script><noscript><a href="http://ipla.tv">Oglądaj w ipla</a></noscript>');
	},
	videoGallery: function() {

		if ($(".videoContent li").size() > 3) {
			$(".videoContent").jCarouselLite({
				btnPrev: ".videoNavPrev",
				btnNext: ".videoNavNext"
			});
		} else {
			$(".videoNavPrev a, .videoNavNext a").hide();
		
		}
		
		Redefine.handleVods($(".videoContent li a").eq(0).attr("class"));
		
		$(".videoContent li a").click(function() {
			var identifier = $(this).attr("class");
			Redefine.handleVods(identifier);
			
			return false;
		});
	},
	enlargeMap: function(){
		var startContainerHeight = $("#subContentWrapper").height();
		var expandedContainerHeight = "645px";
		
		$(".enlargeMap").click(function() {
			$(this).fadeOut("fast", function() {
				$("#subContentWrapper")
				.animate({height: expandedContainerHeight}, function() {
					if ($(".closeBlock").is("a")) {
						$(".closeBlock").show();
					}
					else {
						var closeBlock = document.createElement("a");
						$(closeBlock).css({
							"background": "url("+$(".enlargeMap").attr("href")+") 0 0 no-repeat"
						}).addClass("closeBlock").prependTo("#subContentWrapper").attr("href", "#");
					}
					$(".closeBlock").click(function() {
						$("#subContentWrapper").animate({height: startContainerHeight+"px"}, function() {
							$(".enlargeMap").show();
							$(".closeBlock").hide();
						});
						
					});
				});
			});
			
			return false;
		});
	},
	applyJobForm: function() {
		$("form[name=jobApply] input[type=text]").each(function() {
			if($(this).attr("value")!="") {
				var name = $(this).attr("name");
				Redefine.clearInput(name);
			}
		});
	},
	// FCKEditor image properties translator
	applyImageSpace: function() {
		if (!$.browser.msie) {
			$(".entry img").each(function(){
				var properties = {
					"hspace": $(this).attr("hspace"),
					"vspace": $(this).attr("vspace"),
					"direction": $(this).attr("align")
				};
				if(properties.direction=="left" || properties.direction=="right") {
					// odwrocenie kierunku l-r
					var paddingDir = (properties.direction=="left") ? "right" : "left";
					$(this).css("margin-"+paddingDir, properties.hspace+"px");
				}
				$(this).css({
					"margin-top": properties.vspace/2 + "px",
					"margin-bottom": (properties.vspace/2 > 0) ? properties.vspace/2 + "px" : "10px"
				});
				//alert(properties.vspace/2);
			});
		}
	},
	applyImageCentering: function() {
		$(".entry p img").each(function() {
			if ($(this).parent().css('text-align') == 'center' && $(this).css('float')=='none') {
				var border = $(this).css("border-width");
				var imgWidth = $(this).attr("width");
				var parWidth = $(this).parent().width();
				var margin = (parWidth - imgWidth) / 2;
				$(this).parent().css('margin-left', margin+'px');
			}
		})
	}
}

$(document).ready(function() { Redefine.construct(); });

