// Visual Studio intelisense references :
///<reference path="../../../../../../misc/jquery-vsdoc.js" />
///<reference path="../../../../../../misc/drupal.js" />

$(document).ready(function() {

	if ($('#tabs-video-top-last').length > 0) {
		$('#tabs-video-top-last').tabs();
	}

	if ($('#sliderContent').length > 0) {

		function pagerFactory(idx, slide) {
			var s = idx % 2 ? ' style="display:none"' : '';
			s = '';
			return '<a class="pngfix" href="#slide-' + (idx + 1) + '" ' + s + '>' + (idx + 1) + '</a>';
		};

		function onAfter(curr, next, opts) {
			var index = opts.currSlide;
			$('#prev')[index == 0 ? 'hide' : 'show']();
			$('#next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
		};

		$('#sliderContent').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 5000,
			pauseOnPagerHover: 1,
			pager: '#controls',
			pause: 1,
			next: '#next',
			prev: '#prev',
			delay: -500,
			cleartype: 1,
			pagerAnchorBuilder: pagerFactory,
			after: onAfter,
			slideExpr: 'div.slide'
		});

		$('#pause').click(function() { $('#sliderContent').cycle('pause'); return false; });
		$('#play').click(function() { $('#sliderContent').cycle('resume'); return false; });
		$('#sliderContainer').hover(
	        function() { $('#sliderControls').fadeIn(); $('#controls').fadeIn(); },
	        function() { $('#sliderControls').fadeOut(); $('#controls').fadeOut(); }
	    );
	}

	if ($('.prima-kino-container ul').length) $('.prima-kino-container ul').kwicks({ max: 280, duration: 300, easing: 'easeOutQuad', complete: 'callback' });

	if ($("#topSliderTab").length) {
		var activeClass = 'active';
		var marginTopDefault = 0;
		$("#topSliderTab").click(function() {
			var topSliderContainer = $("#topSliderContainer");
			if (!topSliderContainer.hasClass(activeClass)) {
				marginTopDefault = topSliderContainer.css('margin-top');
				topSliderContainer.animate({ marginTop: 0 }, 500);
				topSliderContainer.addClass(activeClass);
			} else {
				topSliderContainer.removeClass(activeClass);
				topSliderContainer.animate({ marginTop: marginTopDefault }, 500);
			}
		});
	}


	/* BUTTONS */
	if ($('.fg-button').length) {
		$('.fg-button').hover(
                    function() { $(this).removeClass('ui-state-default').addClass('ui-state-focus'); },
                    function() { $(this).removeClass('ui-state-focus').addClass('ui-state-default'); }
        );
	}

	/* MENUS */
	if ($('.page-videoarchive .videoarchive-choose-program .select-box-button').length) {
		$('.videoarchive-choose-program .select-box-button').each(function() {
			$(this).menu({
				content: $(this).parent().find(".select-box-items-container .item-list").html(),
				//content: $(this).parent().find(".select-box-items-container").html(),
				maxHeight: 250,
				width: 260,
				positionOpts: {
					posX: 'left',
					posY: 'bottom',
					offsetX: -7,
					offsetY: -1,
					directionH: 'right',
					directionV: 'down',
					detectH: false, // do horizontal collision detection
					detectV: false, // do vertical collision detection
					linkToFront: false
				},
				showSpeed: 300

			});

		});
	} else if ($('.select-box-button').length) {
		$('.select-box-button').each(function() {
			$(this).menu({
				content: $(this).parent().find(".select-box-items-container .item-list").html(),
				//content: $(this).parent().find(".select-box-items-container").html(),
				maxHeight: 250,
				width: 260,
				positionOpts: {
					posX: 'left',
					posY: 'bottom',
					offsetX: -3,
					offsetY: -5,
					directionH: 'right',
					directionV: 'down',
					detectH: false, // do horizontal collision detection
					detectV: false, // do vertical collision detection
					linkToFront: false
				},
				showSpeed: 300
			});

		});
	}

	//default styles
	if ($.fn.qtip && $.fn.qtip.styles) {
		$.fn.qtip.styles.iprima = { // Last part is the name of the style
			border: {
				width: 2,
				radius: 5,
				color: '#000000'
			},
			width: 289,
			padding: 0,
			background: '#000000',
			textAlign: 'left',
			tip: {
				corner: 'bottomLeft',
				size: {
					x: 5,
					y: 5
				}
			}
		}
	}

	if ($('#leftContainer .tvprogram-options-link').length) {
		$('#leftContainer .tvprogram-options-link').each(function() {
			var id = $(this).attr('rel');
			var content = $('#tvprogram-details-container-' + id).html();
			$(this).qtip({
				content: {
					text: content
				},
				position: {
					corner: {
						tooltip: 'bottomLeft',
						target: 'topLeft'
					},
					adjust: { x: 30, y: -3, screen: true, mouse: false }
				},
				show: {
					when: 'click',
					solo: true // hide all other tooltips
				},
				hide: {
					when: 'click'
				},
				style: 'iprima'
			});
		});

		$('#leftContainer .tvprogram-options-link').click(function(event) {
			if (event.stopPropagation) { event.stopPropagation(); } //For 'Good' browsers
			else { event.cancelBubble = true; } //For IE

			return false;
		});
	}

	/* Login/Registration from content - show panel */
	var activeLoginRegistration = false;
	$(".btn-prihlaseni-registrace").click(function(event) {
		if (activeLoginRegistration == false) {
			$("div#panel").slideDown("slow");
			$("#toggle a").toggle();
			activeLoginRegistration = true;
		} else {

		}

		$.scrollTo('div#panel', 800);

		return false;
	});

	/* Expand Panel */
	$(".open").click(function() {
		$("div#panel").slideDown("slow");
		return false;
	});
	$(".top-title").click(function() {
		$("div#panel").slideDown("slow");
		$("#toggle a").toggle();
		return false;
	});


	/* Collapse Panel */
	$(".close").click(function() {
		$("div#panel").slideUp("slow");
	});

	/* Switch buttons from "Log In | Register" to "Close Panel" on click */
	$("#toggle a").click(function() {
		$("#toggle a").toggle();
	});


	/* Pasted from jsuchy.js */
	// Add to any link, Print
	$('.node-links .addtoany .a2a_dd').html('<span class="share"><span class="icon icon-share-17x24"></span>sdílet</span>');
	$('#a2apage_EMAIL').css('display', 'none');
	$('#a2apage_amazon_wish_list').css('display', 'none');
	$('.prima_print a').click(function() {
		window.print();
		return false;
	});


	// Form errors
	var shown = 0;
	$('.login-box #user-login .error').each(function() {
		if (shown == 0) { shown = 1; $("div#panel").slideDown("slow"); $("#toggle a").toggle(); }
	});
	$('.register-box #user-register .error').each(function() {
		if (shown == 0) { shown = 1; $("div#panel").slideDown("slow"); $("#toggle a").toggle(); }
	});

	$(".select-box div").css("display", "none");
	$(".select-box span").click(function() {
		$(this).next().slideToggle("fast");
	});
	/* end temporary */

	/* Tonight */
	$(".view-b02-tonight .views-row-2 .views-field-title").css("display", "none");
	$(".view-b02-tonight .views-row-3 .views-field-title").css("display", "none");
	$(".view-b02-tonight .views-row-4 .views-field-title").css("display", "none");
	/* Make first active */
	$(".view-b02-tonight .views-row-1 .views-field-field-time-start-value").addClass('tonight-active');

	$(".view-b02-tonight .views-row .views-field-field-time-start-value").mouseover(function() {
		$(".view-b02-tonight .views-row .views-field-title").each(function() {
			$(this).css('display', 'none');
		});
		$(".view-b02-tonight .views-row .views-field-field-time-start-value").each(function() {
			$(this).removeClass('tonight-active');
		});
		$(this).addClass('tonight-active');
		$(this).next('.views-field-title').css('display', 'block');
	});

	/* Tonight end */

	/* Porady TV prima - zmena odpalu od spodni hrany pri dvouradkovem textu */
	if ($('#primaPoradyContainer .views-field-title a').height() > 16) {
		$('#primaPoradyContainer .views-field-title').css('bottom', '9px');
	}

  if ($('div').hasClass('tabs')) {
    $(".video-reference-container .fivestar-widget .fivestar-processed .form-item .description .user-rating").css("display", "none");
		$(".video-reference-container .fivestar-widget .fivestar-processed .form-item .description").css("display", "block");
  }
  
  if($("input[name='edit-submitted-stranka-s-problemem'][type='hidden']")){
    var ur = document.referrer ? document.referrer : "nepodařilo se zjistit";
    $('#edit-submitted-stranka-s-problemem').val(
     "funkční javascript: ano, \n" +
     "adresa s problémem: " + ur + ", \n" +
     "název prohlížeče: " + navigator.appCodeName + ", \n" +
     "označení prohlížeče: " + navigator.appName + ", \n" +
     "verze prohlížeče: " + navigator.appVersion + ", \n" +
     "system: " + navigator.platform + ", \n" +
     "hlavička: " + navigator.userAgent);
	}

	if($('#slider .views-row').length > 6){
		var p = new primaSlider();
		p.init();
  	$('#sliderLeft').css('display', 'block');
		$('#sliderRight').css('display', 'block');
	}
});



primaSlider = function(){
	var self = this;
	this.delay;
	this.direction;
	this.sliderWidth;
	this.itemWidth;
	this.sliderPosition;
	this.timeout;
	this.mouseX;
	this.mouseDownX;
	this.buttonDown;
	this.mouseOver;

	this.init = function(){
		// sirka slideru
		self.sliderWidth = 0;
		$('#slider .views-row').each(function(index, elm){
			self.sliderWidth += $(elm).outerWidth(true);
		});
		$('#slider').css({width:self.sliderWidth+'px'});

		// sirka jedne polozky
		self.itemWidth = $('#slider .views-row:first').outerWidth(true);

		// handlery
		$('#sliderLeft').bind('click', self.directionLeft);
		$('#sliderRight').bind('click', self.directionRight);
		$('#slider').hover(self.handleMouseEnter, self.handleMouseLeave);
		$('#slider').bind('mousedown', self.handleMouseDown);
		$('#slider .views-row a').bind('click', self.clickItem);
		$(document).bind('mouseup', self.handleMouseUp);

		// vychozi hodnoty
		self.sliderPosition = 0;
		self.buttonDown = false;
		self.buttonOver = false;

		self.delay = 20;
		self.directionLeft();
		self.animate();
	}

	// kliknuti na element pokud sem posouval tak nic
	this.clickItem = function(event){
	  if(self.mouseDownX != event.pageX)
			return false;
	}
	
	// vstup mysi
	this.handleMouseEnter = function(){
		self.mouseOver = true;
		self.stopAnimation();
	}

	// opusteni mysi
	this.handleMouseLeave = function(){
		self.mouseOver = false;
		if (!self.buttonDown){
			self.startAnimation();
		}
	}

	// stisk tlacitka mysi
	this.handleMouseDown = function(event){
		event.preventDefault();

		self.stopAnimation();
		self.buttonDown = true;
		self.mouseX = event.pageX;
		self.mouseDownX = self.mouseX;
		$(document).bind('mousemove', self.handleMouseMove);
	}

	// uvolneni tlacitka mysi
	this.handleMouseUp = function(event){
		if (self.buttonDown){
			self.buttonDown = false;
			$(document).unbind('mousemove', self.handleMouseMove);
			// spusteni animace
			if (!self.mouseOver){
				self.startAnimation();
			}
		}
	}

	// pohyb mysi pri stisknutem tlacitku
	this.handleMouseMove = function(event){
		event.preventDefault();

		var move = event.pageX - self.mouseX;
		if (move > 0) {
			self.direction = 1;
		} else {
			self.direction = -1;
		}

		self.moveSlider(move);
		self.mouseX = event.pageX;
	}

	// animace
	this.animate = function(){
		self.moveSlider(self.direction);
		self.timeout = setTimeout(function(){self.animate();}, self.delay);
	}

	// spusteni animace
	this.startAnimation = function(){
		clearTimeout(self.timeout);
		self.animate();
	}

	// zastaveni animace
	this.stopAnimation = function(){
		clearTimeout(self.timeout);
	}

	// zmena pozice slideru
	this.moveSlider = function(move){
		self.sliderPosition += move;

		// pohyb slideru doleva
		if (self.sliderPosition <= -self.itemWidth){
			$('#slider .views-row:first').appendTo('#slider');
			self.sliderPosition = self.sliderPosition + self.itemWidth;
		}

		// pohyb slideru doprava
		if (self.sliderPosition > 0){
			$('#slider .views-row:last').prependTo('#slider');
			self.sliderPosition = -self.itemWidth + self.sliderPosition;
		}

		// aktualizace pozice slideru
		$('#slider').css({left: self.sliderPosition+'px'});
	}

	// smer pohybu vlevo
	this.directionLeft = function(){
		self.direction = -1;
		return false;
	}

	// smer pohybu vpravo
	this.directionRight = function(){
		self.direction = 1;
		return false;
	}

}
