jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

if (typeof playlist_params != 'undefined') {
	var hash = window.location.hash;

	if (hash && hash.indexOf("video") != -1) {
		var videoId = hash.substr(hash.indexOf("video") + 6);
		playlist_params = playlist_params + ((playlist_params != "") ? "&" : "?") + "video=" + videoId;
	}

	playlist_params = encodeURIComponent(playlist_params);

	swfobject.embedSWF(
		'/player/player.swf', 'playerflash', '567', '384', '8.0.0', '/images/expressInstall.swf',
		{ _xmlpath: '/player/playlistXML.jsp' + playlist_params,  _xmlpathplayer: '/player/playlistXML.jsp' + playlist_params },
		{ wmode: 'transparent', menu: 'false', menu: 'false', allowScriptAccess : 'always' }
	)
}
if (typeof mobile_params != 'undefined') {
	swfobject.embedSWF(
		'/player/playeringame.swf', 'playerflash', '211', '374', '8.0.0', '/images/expressInstall.swf',
		{ path: mobile_params, sound: mobile_sound },
		{ wmode: 'transparent', menu: 'false' }
	)
}

if (typeof film_params != 'undefined') {
	swfobject.embedSWF(
		'/player/player.swf', 'playerflash', '567', '384', '8.0.0', '/images/expressInstall.swf',
		{ _xmlpath: '/player/playlistXML.jsp' + film_params,  _xmlpathplayer: '/player/playlistXML.jsp' + film_params },
		{ wmode: 'transparent', menu: 'false' }
	)
}

document.onclick = function(e) {
	var imagePopup = function(url, width, height) {
		
		var w = window.open(url, '_blank', 'width=' + width + ',height=' + height + ',resizable=yes')
		w.document.open()

		w.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'+"\n");
		w.document.write('<html><head>');
		w.document.write('<style>html, body { padding: 0; margin: 0; background: #FFFFFF;  height: 100%;}')
		w.document.write(' img {margin:0; border: none;} </style>')
		w.document.write('</head><body>');
		w.document.write('<img src="' + url + '" width="' + width + '" height="' + height + '" border="0" alt=""></body></html>')

		w.document.close();
		w.focus();

		return false;
	}

	var textPopup = function(url, name, width, height, scrollbars) {
		window.open(url, name, 'width=' + width + ',height=' + height + ',resizable=yes' + (scrollbars ? ',scrollbars=yes' : ''))
		return false;
	}

	e = e || event

	if (e.button != 0) {
		return true
	}

	var t = e.target || e.srcElement
	while (t && t.nodeType == 1 && t.tagName.toLowerCase() != 'a') {
		t = t.parentNode
	}

	if (!t || t.nodeType != 1) {
		return true
	}

	if (/\bglowing\b/.test(t.className)) {
		size = t.getAttribute('popup').split(' ')

		return imagePopup(t.href, (parseInt(size[0]) || '320'), (parseInt(size[1]) || '240'));
	}

	if (/\bdownload\b/.test(t.className)) {
		size = t.getAttribute('popup').split('x')

		return textPopup(t.href, '_blank', (parseInt(size[0]) || '800'), (parseInt(size[1]) || '600'), true);
	}

	if (/\bpreviews\b/.test(t.parentNode.className) && /\bmobile\b/.test(t.parentNode.parentNode.className)) {
		size = t.hash.substr(1).split(',')


		return imagePopup(t.href, (parseInt(size[0]) || '320'), (parseInt(size[1]) || '240'));
	}
	if (/\bpreviews\b/.test(t.parentNode.className) && /\bmovies\b/.test(t.parentNode.parentNode.className)) {
		size = t.getAttribute('popup').split(' ')

		return imagePopup(t.href, (parseInt(size[0]) || '800'), (parseInt(size[1]) || '422'));
	}
	if (/\bpreviews\b/.test(t.parentNode.className) && /\pc\b/.test(t.parentNode.parentNode.className)) {
		size = t.getAttribute('popup').split(' ')

		return imagePopup(t.href, (parseInt(size[0]) || '800'), (parseInt(size[1]) || '600'));
	}
	if (/\bpreviews\b/.test(t.parentNode.className) && /\mobile\b/.test(t.parentNode.parentNode.className)) {
		size = t.getAttribute('popup').split(' ')

		return imagePopup(t.href, (parseInt(size[0]) || '240'), (parseInt(size[1]) || '320'));
	}

	if (/\bpreviews\b/.test(t.parentNode.className)) {
		return textPopup('/player/screenshot.jsp?' + t.href, 'previews', 820, 620, false)
	}

	if (/\bprint\b/.test(t.target)) {
		return textPopup(t.href, '_blank', 820, 620, true)
	}

	if (/\btrailers\b/.test(t.parentNode.className)) {
		return textPopup(t.href, 'trailers', 588, 397, false)
	}

	if(/\bbanner\b/.test(t.target)) {
		size = t.name.split(',')
		return textPopup(t.href, '_blank', (parseInt(size[0]) || '600'), (parseInt(size[1]) || '480'), false)
	}

	if (/\bdvdcover\b/.test(t.target)) {
		size = t.hash.substr(1).split(',')
		return imagePopup(t.href, (parseInt(size[0]) || '425'), (parseInt(size[1]) || '600'));
	}

	if (/\bgamecover\b/.test(t.target)) {
		size = t.getAttribute('popup').split(' ')

		return imagePopup(t.href, (parseInt(size[0]) || '320'), (parseInt(size[1]) || '240'));
	}

	if(/\bveil\b/.test(t.className)) {
		document.body.className += ' veil';

		var veilIframe = document.getElementById('popupframe');

		var hash = (t.href.indexOf('#') > -1) ? t.href.substr(t.href.indexOf('#') + 1) : '';

		if(hash.length > 0 && (hashNode = document.getElementById(hash))) {
			veilIframe.innerHTML = '<div>' + hashNode.innerHTML + '</div>';

		} else {
			veilIframe.innerHTML = '<iframe frameborder="0" src="' + t.href + '"></iframe';
		}

		window.scrollTo(0,0);

		return false;
	}

	if(t.id == 'closeVeil') {
		document.body.className = document.body.className.replace(/ veil/g, '');
		return false;
	}

	return true
}


$(function() {
	$(".phone .allmodels span").click(function() {
		$(".phone .full").not($(this).parents("dd")).removeClass("full");
		$(this).parents("dd").toggleClass("full");
	});
});
$(function() {
	$("body.archive #content ul.othernews").slice(1).hide();
	$("body.archive #content h3").slice(0,1).addClass("openlist");
	$("body.archive #content h3").click(function() {
		$(this).next("ul").toggle();
		$(this).toggleClass("openlist");
		$(this).parents().children("h3").not($(this)).removeClass("openlist");
		$(this).nextAll("ul").not($(this).next("ul")).hide();
		$(this).prevAll("ul").hide();
	});
});

$(document).ready(function() {
	var dvdPromoItem = document.getElementById("dvd-promo-item");

	if (dvdPromoItem) {
		var externalScript = document.createElement("script");

		var onLoadScript = function () {
			var buttons = "";
			$.each(dvdSoon, function(i) {
				if (dvdSoon[i] != 0) {
					if (dvdSoon[i].current) {
						$('#slideshow-img').load(function() {
							$(this).show();
						}).attr("src", dvdSoon[i].image);

						$("#slideshow-link").attr({
							"href": dvdSoon[i].clk,
							"dimensions": dvdSoon[i].popup
						});

						if (!window.location.hash) {
							carouselSlideshow.selectThumb(dvdSoon[i].url);
						}

						if (dvdSoon[i].soon == 1) {
							$(".dvd-promo-soon").show();
						} else {
							$(".dvd-promo-soon").hide();
						}

						buttonsAttrs = ' id="slideshow-current" class="slideshow-current" ';
					} else {
						buttonsAttrs = '';
					}

					buttons += '<a href="' + dvdSoon[i].url + '" image="'+dvdSoon[i].image+'" title="'+ dvdSoon[i].title + '" rel="'+dvdSoon[i].url + '" rev="' + dvdSoon[i].soon + '" dimensions="'+ dvdSoon[i].popup + '" ' + buttonsAttrs + '>';
					buttons += '<img src="' + dvdSoon[i].button + '" alt="' + dvdSoon[i].title + '"/>';
					buttons += '</a>';
				}
			});

			$("#slideshow-buttons").html(buttons);
			initButtons();

			carouselSlideshow.init();
		};

		externalScript.type="text/javascript";
		externalScript.onreadystatechange = function () {
			if (this.readyState == 'loaded' || this.readyState == 'complete') {
				onLoadScript.apply(this);
			}
		}

		externalScript.onload = onLoadScript;
		externalScript.src = dvdPromoItem.getAttribute('path');

		var nodeBefore = document.getElementById("slideshow-link");
		dvdPromoItem.insertBefore(externalScript, nodeBefore);
	}
});

function initButtons(){
	var img = $('#slideshow-img');

	if (img) {
		var link = $('#slideshow-link');
		var currentClass = 'slideshow-current';

		$('#slideshow-buttons a').click(function() {
			if (!$(this).hasClass(currentClass)) {
				$('.' + currentClass).removeClass(currentClass);
				$(this).addClass(currentClass);

				img.attr('src', $(this).attr('image')); // previews are now loaded to #image-preloadings in changeflash.js

				link.attr({
					'href': $(this).attr('rel'),
					'dimensions': $(this).attr('dimensions')
				});

				if ($(this).attr('rev') != '') {
					$('.dvd-promo-soon').show();
				} else {
					$('.dvd-promo-soon').hide();
				}

				with (carouselSlideshow) {
					//selectThumb(this.rel);

					if (!promoTriggered) {
						restartPromoCycle();
					}
				}
			}

			return false;
		});
	}
}


$(function() {
	$("body.video #content #playlists h4").click(function() {
		$(this).next("ul").andSelf().toggleClass("opened");
	})

	$("body.video #content #playlists h4 a").click(function(e) {
		$(this).parent("h4").trigger("click");
		return false;
	})
});

$(function() {
	$('#main #video-menu form input.text'
	).each(function() {
		this.defVal = this.value;
	}).focus(function() {
		if (this.value == this.defVal) {
			this.value = '';
		}
	}).blur(function() {
		if (this.value == '') {
			this.value = this.defVal;
		}
	});
});

$(function() {
	$('#search-keyword'
	).each(function() {
		this.defVal = this.value;

	}).focus(function() {
		if (this.value == this.defVal) this.value = '';
		$(this).parents().eq(0).addClass('search-keyword-active');

	}).blur(function() {
		if (this.value == '') this.value = this.defVal;
		$(this).parents().eq(0).removeClass('search-keyword-active');
	});
});

function onSearchComplete(sc, searcher) {
	window.searchTerm = sc.input.value;
	location.hash = searchTerm;

	if (window.cto) {
		cto.ajaxStart();
		cto.pageName = 'search';
		cto.internalSearchPhrase = searchTerm;
		cto.eventType = 'internalSearch';
		cto.ajaxSubmit();
	}

	if (searcher.results && searcher.results.length > 0) {
		var banners = $('.jsbanner').hide(),
			gs = $('#searchcontrol');

		$('div.gs-visibleUrl-long', gs).each(function() {
			$(this).wrapInner('<a href="http://' + $(this).text() + '"></a>');
		});

		$('div.gs-result', gs).each(function(i) {
			$(this).prepend(
				'<div class="gs-counter">'
				+ (i + 1 + searcher.cursor.currentPageIndex * 10)
				+ '</div>'
			);
		});

		$('a.gs-title, div.gs-snippet', gs).each(function() {
			$('b:contains("...")', $(this)).addClass('hellip');
		});

		if (searcher.cursor.pages.length > 1) {
			$(
				'div.gsc-cursor-page', gs
			).show().filter(
				':gt(' + searcher.cursor.pages.length + ')'
			).hide();
			$('div.gsc-cursor', gs).prepend('Страницы: ').show();
		}
		else {
			$('div.gsc-cursor', gs).hide();
		}

		banners.show();

		if (searcher.cursor.moreResultsUrl) {
			$('#gs-moreresults').show().find('a').attr({
				href: searcher.cursor.moreResultsUrl + '&filter=0',
				target : '_blank'
			});
		}
		else {
			$('#gs-moreresults').hide();
		}
	}
}

$(function() {
	if (location.hash) {
		var cleanTitle = document.title;
		setTimeout(function() {
			document.title = cleanTitle;
		}, 0);
	}

	$('#searchform').submit(function(e) {
		window.searchTerm = $('#search-keyword').val();

		if (window.searchControl) {
			location.hash = searchTerm;
			searchControl.execute(searchTerm);
		}
		else {
			location = '/search/#' + searchTerm;
		}

		e.preventDefault();
	});
});

$(function(){
	$("body.channel-program2, body.channel, body.program").each(function(){
		$("#channel-program-list ul li").hover(function(){
			$(this).addClass("hovered");
			if($(this).hasClass("current")) $(this).addClass("current-hovered");
			$(this).parents("ul").eq(0).addClass("mousehovered");
		}, function(){
			$(this).removeClass("hovered current-hovered");
			$(this).parents("ul").eq(0).removeClass("mousehovered");
		});
	});
});

$(function(){
	$("body.games").each(function(){
		$.getJSON('/games/play/action/play-now.jsp' , function(data, textStatus){
			var $html = [];
			if (data && data.games) {
				$.each(data.games, function(_, game){
					$html.push('<li><a href="/games/play/?id='+game.id+'"><img src="'+game.icon+'" width="62" height="53" alt="'+game.title+'" title="'+game.title+'" /><i></i></a></li>');
				});
			} 
			$($html.join("\n")).appendTo($('.play-now ul'));
		});
	});
});

if (typeof String.prototype.trim === 'undefined') {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, '');
	}
}
$(function() {
	$(".yota").hover(function() {
		$(this).find("div.note").show()
	}, function() {
		$(this).find("div.note").hide();
	})
	
	$("#yota-block div a").hover(function() {
		$("#yota-prewrapper").find("div.note").show()
	}, function() {
		$("#yota-prewrapper").find("div.note").hide();
	})
})
