$(document).ready(function()
{   // videobox  prima cool
		$("#newvideo p span:not(.active)").click(function(){
                $("div#newvideo").css("display", "none");
                $("div#videoarchiv").css("display", "block");
                return false;
                });
    $("#videoarchiv p span:not(.active)").click(function(){
    						$("div#videoarchiv").css("display", "none");
                $("div#newvideo").css("display", "block");
                return false;
                });

		/* On homepage start animation of items */
		var haInst = new HeaderAnimation();
		haInst.backgroundInit();
		if($("#sliderContainer").get(0)){
			haInst.init();
			haInst.run();
		}

		/* changing color theme */
    $("#toppanel #toggle .bg.pngfix").before('<div class="background"><span class="st1 bc" id="cool0"><span></span></span><span class="st2 bc" id="cool1"><span></span></span><span class="st3 bc" id="cool2"><span></span></span></div>');
    $("#toppanel .background span.bc").click(function(){
                if($.cookie('theme_cool'))
    							$('#body').removeClass($.cookie('theme_cool'));
								if(this.id)
    							$.cookie('theme_cool', this.id);
								else
                  $.cookie('theme_cool', null);
                $('#body').addClass($.cookie('theme_cool'));
                return false;
    });

    $("#toppanel .background span.bc").hover(function(){
									$(this).animate({paddingLeft:33,fontSize:14},"slow");
									$('#'+this.id+' span').animate({width:22,height:22,top:-5},"slow");
								},
								function(){
  								$(this).animate({paddingLeft:20, fontSize:9},"slow");
  								$('#'+this.id+' span').animate({width:12,height:12,top:0},"slow");
								}
    );

    $("#toppanel .background span.bc").hover(function(){
									$(this).animate({paddingLeft:33,fontSize:14},"slow");
									$('#'+this.id+' span').animate({width:22,height:22,top:-5},"slow");
								},
								function(){
  								$(this).animate({paddingLeft:20, fontSize:9},"slow");
  								$('#'+this.id+' span').animate({width:12,height:12,top:0},"slow");
								}
		);

    /* Expand Panel */
    $(".open").click(function(){
                $("div#panel").slideDown("slow");
                if(!$.browser.mozilla) {$("#scream4cool").css({marginTop:230});}
                return false;
                });
    $(".top-title").click(function(){
                $("div#panel").slideDown("slow");
                $("#toggle a").toggle();
                return false;
                });

    /* Collapse Panel */
    $(".close").click(function(){
                $("div#panel").slideUp("slow");
                if(!$.browser.mozilla){ $("#scream4cool").css({marginTop:0});}
                });

    /* 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(); if(!$.browser.mozilla) $("#scream4cool").css({marginTop:230});}
      });
    $('.register-box #user-register .error').each(function() {
      if (shown == 0) { shown = 1; $("div#panel").slideDown("slow"); $("#toggle a").toggle(); if(!$.browser.mozilla) $("#scream4cool").css({marginTop:230});}
      });

    // Set widthxheight of videoarchive.
    $('.greybox').each(function() {
        $(this).attr('rel', '978x565');
      });
    if (jQuery.browser.msie) {
      var version = parseInt(jQuery.browser.version);
      if (version == 6) {
        $('.greybox').each(function() {
            $(this).unbind('click');
            $(this).removeClass('greybox');
          });
      }
    }

    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");
  	}
});

function HeaderAnimation(){

	var self = this;

	this.list = new Array();
	this.index = 0;
	this.backgroundLeft = 0;
	this.backgroundRight = 0;
	this.backgroundTopRight = 228;
	this.backgroundTopLeft = 298;
	this.backgroundTop = 220;
	this.viewportWidth = $('#sliderContainer').outerWidth();

	this.init = function(){
		var aText = $('#sliderContainer .viewport-text');
		var aImage = $('#sliderContainer .viewport-image');
		var aInfo = $('#sliderContainer .viewport-info');

		// počet animací
		var count = $('#sliderContainer .slide').length;

		for(var i=0;i<count;i++){
			self.list.push({
				text: aText[i],
				image: aImage[i],
				info: aInfo[i]
			});
		}
	}

	this.run = function(){
		// animace začíná zobrazením textu
		$(self.list[self.index].text).css({left:0});
		setTimeout(function(){self.animTextDelay();}, 3000);
	}

	this.animText = function(){
		$(self.list[self.index].text).css({left:self.viewportWidth+'px'});

		$(self.list[self.index].text).animate({'left':'-='+self.viewportWidth+'px'}, self.cas(self.viewportWidth, 18), 'linear', function(){
			self.animTextDelay();
		});
	}

	this.animTextDelay = function(){
		var sirka = $(self.list[self.index].text).outerWidth();

		if (sirka == self.viewportWidth){
			setTimeout(function(){self.animImage();}, 3000);
		} else {
			$(self.list[self.index].text).animate({'left':'-='+(sirka - self.viewportWidth)+'px'}, self.cas(sirka - self.viewportWidth, 10), 'linear', function(){
				self.animImage();
			});
		}
	}

	this.animImage = function(){
		$('#sliderContainer .viewport-info').hide();
		$('#sliderContainer .viewport-image').css({left:self.viewportWidth+'px'});

		self.backgroundAnim();

		$(self.list[self.index].text).animate({'left':'-='+self.viewportWidth+'px'}, self.cas(self.viewportWidth, 18), 'linear');
		$(self.list[self.index].image).animate({'left':'-='+self.viewportWidth+'px'}, self.cas(self.viewportWidth, 18), 'linear', function(){
			self.animInfo();
		});

	}

	this.animInfo = function(){
		$(self.list[self.index].info).animate({width:'show'}, 1000, 'linear', function(){
			self.animNext();
		});
	}

	this.animNext = function(){
		// posun ukazatele
		self.index++;
		if (self.index == self.list.length){
			self.index = 0;
		}

		setTimeout(function(){self.animText();}, 3000);
	}

	this.backgroundInit = function(){
		self.backgroundLeft = $('#headerContainer').position().left - 220;
		self.backgroundRight = 	$('#headerContainer').position().left + $('#headerContainer').outerWidth();

		// umístení obrázků na pozadí
		$('#body').css({'background-image':'url(/sites/all/themes/prima/primacool/images/background-left.png)','background-position':self.backgroundLeft+'px '+self.backgroundTop+'px','background-repeat':'no-repeat','background-attachment':'fixed'});
		$('#mainContainer').css({'background-image':'url(/sites/all/themes/prima/primacool/images/background-right.png)','background-position':self.backgroundRight+'px '+self.backgroundTop+'px','background-repeat':'no-repeat','background-attachment':'fixed'});
		$('#body.front').css({'background-attachment':'scroll','background-position':self.backgroundLeft+'px '+self.backgroundTopLeft+'px'});
		$('.front #mainContainer').css({'background-attachment':'scroll','background-position':self.backgroundLeft+'px '+self.backgroundTopRight+'px'});
	}

	this.backgroundAnim = function(){
	  if($('#body').hasClass('front')){
			// levá strana
			$('#body').animate({'background-position':(self.backgroundLeft-150)+'px '+self.backgroundTopLeft+'px'}, self.cas(780,18), 'linear', function(){
					$('#body').animate({'background-position':self.backgroundLeft+'px '+self.backgroundTopLeft+'px'}, self.cas(160,18), 'linear');
			});

			// pravá strana
			$('#mainContainer').animate({'background-position':(self.backgroundRight+150)+'px '+self.backgroundTopRight+'px'}, self.cas(780,18), 'linear', function(){
					$('#mainContainer').animate({'background-position':self.backgroundRight+'px '+self.backgroundTopRight+'px'}, self.cas(160,18), 'linear');
			});
		} else {
      // levá strana
			$('#body').animate({'background-position':(self.backgroundLeft-150)+'px '+self.backgroundTop+'px'}, self.cas(780,18), 'linear', function(){
					$('#body').animate({'background-position':self.backgroundLeft+'px '+self.backgroundTop+'px'}, self.cas(160,18), 'linear');
			});

			// pravá strana
			$('#mainContainer').animate({'background-position':(self.backgroundRight+150)+'px '+self.backgroundTop+'px'}, self.cas(780,18), 'linear', function(){
					$('#mainContainer').animate({'background-position':self.backgroundRight+'px '+self.backgroundTop+'px'}, self.cas(160,18), 'linear');
			});
		}
	}

	this.cas = function(delka, rychlost){
		return 30*delka/rychlost;
	}
}

/**
 * Returns the value of the location hash.
 * @return {string} Hash value with '#' prefix discarded.
 */
function getLocationHash () {
	return window.location.hash.substring(1);
}

/**
 * Updates the location hash with the specified string.
 * @param {string} str
 */
function setLocationHash(str) {
	window.location.hash = str;
}

/**
 * Flash helper function to track events to Google Analytics
 */
function gaFlashTrackEvent(action) {
	pageTrackerIPrima.push(['_trackEvent', 'Scream for Cool', action]);
}

/**
 * Flash helper function to track page views to Google Analytics
 */
function gaFlashTrackPageview(pageURL) {
	pageTrackerIPrima.push(['_trackPageview', pageURL]);
}
