$(document).ready(function() {
		//preloading images
		$.preLoadImages("img/logos/logo_disney.png","img/logos/logo_ea.png", "img/logos/logo_sony.png", "img/logos/logo_tmobile.png", "img/logos/logo_hp.png", "img/logos/logo_intel.png", "img/logos/logo_nokia.png", "img/logos/logo_bbc.png", "img/logos/logo_good.png", "img/logos/logo_nsn.png", "img/logos/logo_play.png" );

    //old nokia browser
    if ( navigator.userAgent.toLowerCase().search("browserng") > -1 )
    {
      $("body").addClass("oldNokia");
    }
    if ( (navigator.userAgent.toLowerCase().search("iphone") > -1) || (navigator.userAgent.toLowerCase().search("ipad") > -1) || (navigator.userAgent.toLowerCase().search("ipod") > -1) )
    {
      $("body").addClass("iOS");
    }

		var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad|android|nokia)/);
    var agentIDNokia = deviceAgent.match(/(nokia)/);
    var agentIDAndroid = deviceAgent.match(/(android)/);

    if ( agentID )
		{
				$(".slider").width('1020'); $(".slide3 .grid-1of2").width(420); $(".slide3 .textContainer").width(480); //iOS fix
		}
    if ( agentIDNokia || agentIDAndroid )
		{
        $("body").width('1100');
        $(".slider").width('100%');
        $(".slider").css("margin","0 auto");
    }

		sliderCopy = $('.slider').clone();
		if ( $(".slider").length != 0 ) {
        $('.slider').css("visibility","visible");
        initSlider();
    }

		if ( !agentID && $(".slider").length != 0 ) {
				$(window).resize(function(){
						if(TO !== false)
								clearTimeout(TO);
						TO = setTimeout(resizePage, 50);
				});
		}

		//init sliding logos on fist slide
		if ( $("#logosList").length != 0 ) {
				slideLogos();
				setInterval(slideLogos, 3500);
		}

		//initializing video
		//if ( $(".video-js-box").length != 0 ) {
		//		VideoJS.setupAllWhenReady();
		//}

		//localScroll
		$.localScroll();
    
    //Newsletter Iframe
    $(".iframe").colorbox({iframe:true, width:"700px", height:"360px"});
    $(".iframeForm").colorbox({iframe:true, width:"700px", height:"500px"});
    
});


//slider
var api;
var sliderCopy;
var TO = false;

function resizeSlider() {
		destroySlider();
		initSlider();
}

function initSlider() {
		if ( $(".slider").length == 0 ) { sliderCopy.insertAfter($('#nav')); }
		api = $(".slider").peKenburnsSlider({api:true});
}

function destroySlider() {
		if ( $(".peKenBurns").length != 0 ) { api.destroy(); console.log("api destroy"); }
		if ( $(".slider").length != 0 ) { $(".slider").remove(); console.log("removed .slider"); }
}

function resizePage() {
		window.location.reload();
}

var isVisible = false;
var thisVisible;

//Slider in slider (on first homepage slide)
function slideLogos() {
		//is slide active
		if ( $("#logosList").parents().hasClass("peKb_active") ) {

				//stoping all animations
				$("#logosList li").stop(true, true);

				$("#logosList li:visible").each(function(index) {
						isVisible = true;
				});

				//showing first element if nothing is visible
				if ( !isVisible ) {
						//$("#logosList li:first-child").fadeIn(1000);
						$("#logosList li:first-child").animate({
								opacity: 'toggle'
						});
				}
				else {
						//checking index of visible element
						thisVisible = $("#logosList li:visible").index();

						//$("#logosList li:visible").fadeOut(500);

						//there's no next elements
						if ( $("#logosList li:gt("+thisVisible+")").length == 0 ) {
								thisVisible = null;
								//showing next slide on end
								//api.next();
						}

						//hiding currently visible elements
						$("#logosList li:visible").animate({
								opacity: 'toggle'
						}, 1000, function() {
								//showing next element on animation end
								//checking if it's first element
								if ( thisVisible == null ) {
										thisVisible = 0;
										$("#logosList li:eq("+thisVisible+")").fadeIn(1000);
								}
								//if not first
								else {
										$("#logosList li:eq("+thisVisible+")").next().fadeIn(1000);
								}
						});

				}

				isVisible = false;
		}
}

//preloading images
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

