/*_*/

document.documentElement.id = "js"; 

var langRus = $('HTML').attr('lang') == "ru-RU",
	haveCodeAlready = $('.b-splash').length,
	isOkToPlay = true,
	isAniPhone = navigator.userAgent.indexOf("iPhone") != -1,
	isAniPod = navigator.userAgent.indexOf("iPod") != -1,
	isAniPad = navigator.userAgent.indexOf("iPad") != -1,
	isAnIOS = isAniPhone || isAniPad || isAniPod,
	resizeForIOS = function() {$('.b-splash_iphone').css('width',window.innerWidth).css('height',window.innerHeight).css('top',window.pageYOffset).css('left',window.pageXOffset)}

var gogoSplashScreen = function() {
	$('.b-splash').fadeIn();
	if (isAniPad) {
		$('.b-splash VIDEO').attr('src', $('.b-splash VIDEO SOURCE[src$=m4v]').attr('src')).height(400);
	}
	if (!haveCodeAlready) {
		$('.b-splash').append($('<div class="b-splash-i"><a class="b-splash-skip">' + (langRus?'Пропустить заставку':'Skip Intro') + '</a><div class="b-splash-progress"></div></div>'));
	}
	if(isAnIOS){
		if (!haveCodeAlready) {
			$('.b-splash').addClass('b-splash_iphone');

			haveCodeAlready = true;
			$('.b-splash-skip').click(function() {
			    $('.b-splash').fadeOut();
				$(window).unbind('scroll resize');	
			});
			$('.b-splash VIDEO').bind('ended', function () {
			    $('.b-splash').fadeOut();
				$(window).unbind('scroll resize');	
			});
		}
		resizeForIOS();
		$(window).bind('scroll resize',function() {
			resizeForIOS();
		});	
	} else {
		if (!haveCodeAlready) {
			haveCodeAlready = true;
			splashVideo = $('.b-splash div.b-video').jmeEmbedControls({removeControls: true});
			$('.b-splash VIDEO').bind('waiting', function(e, data){
			    isOkToPlay = false;
				if (!$('.b-splash VIDEO').isPlaying()) {
					$('.b-splash-progress').show().text('0%');
				}
			});
			$('.b-splash VIDEO').bind('progresschange', function(e, data){
				$('.b-splash-progress').text(parseInt(data.relLoaded,10)+'%');
				if (parseInt(data.relLoaded,10) == 100 && $('.b-splash').css('display') != 'none') {
					$('.b-splash VIDEO').play();
				}
			});
			$('.b-splash VIDEO').bind('playing play', function(e, data){
				$('.b-splash-progress').hide();
			});
			$('.b-splash VIDEO').bind('ended', function(e, data){
				$('.b-splash').fadeOut();
				$('.b-splash-progress').hide();
			});

			$('.b-splash-skip').click(function() {
				$('.b-splash VIDEO').pause();
				$('.b-splash').fadeOut();
			});
			if (isOkToPlay && jQuery.browser.opera) {
				window.setTimeout(function() {$('.b-splash VIDEO').currentTime(0).play();});
			}
		} else {
			$('.b-splash VIDEO').currentTime(0).play();
		}
	}
};

$(function(){
	(function($){
		var playerControls = '<div class="media-controls-wrapper"><div class="media-controls" lang="en"><a class="play-pause button"><span class="ui-icon ui-icon-play"> </span><span class="button-text">play / pause</span></a><div class="timeline-slider"><span class="handle-label">play position</span><div class="progressbar"></div></div><span class="b-video-btn"><span class="current-time player-display b-video-btn-l"></span>/<span class="duration player-display b-video-btn-r"></span></span><a class="mute-unmute button"><span class="ui-icon ui-icon-volume-on"> </span><span class="button-text">mute / unmute</span></a><div class="volume-slider"><span class="handle-label">volume control</span></div><span class="b-video-btn b-video-btn_share"><span class="b-video-btn-l"><span class="b-video-btn-r">share</span></span></span></div></div>';
		var fullscreenBtn = '<a class="fullscreen button"><span class="ui-icon ui-icon-circle-zoomin"> </span><span class="button-text">zoomin / zoomout</span></a>';

		$.fn.jmeEmbedControls = function(o){
			return this
				//append standard controls
				.append(playerControls)
				//add extra video controls
				.each(function(){
					//media-state makes only sense with video not audio
					var hasVideo = $('video', this).after('<div class="media-state" />');
					//include fullscreenn button only if plugin is included and we have a video-element
					if( $.fn.enterFullWindow && hasVideo[0] ) {
						$('div.media-controls', this).append(fullscreenBtn);
					}
				})
				//register controls with jmeConttrol
				.jmeControl(o)
			;
		};

	})(jQuery);
	if(!isAnIOS){
		$('.b-page div.b-video').jmeEmbedControls();
		$('.b-video-btn_share').click(function() {
			var sharedVideo = $(this).closest('.b-video').find('VIDEO');
			var shareBlock = $(this).closest('.b-video').find('.b-video-share');
			if (!shareBlock.length) {
				shareBlock = $('<div class="b-video-share" style="display:none;"><div class="b-video-share-play">' + (langRus?'Играть':'Replay') + '</div><div class="b-video-share-option"><input class="b-video-share-option-url" type="text" readonly="readonly" /></div><div class="b-video-share-option"><a class="b-video-share-option-download">' + (langRus?'Скачать':'Download') + '</a></div></div>');
				$(this).closest('.b-video').append(shareBlock);
				shareBlock.find('.b-video-share-option-url').val(document.URL).click(function() {
					this.select();
				});
				shareBlock.find('.b-video-share-option-download').attr('href','/download.php?file=' + $(this).closest('.b-video').find('source[src$=m4v]').attr('src').replace(/^.+wp-content/,'wp-content'));
			}
			sharedVideo.pause();
			shareBlock.fadeIn();
			shareBlock.click(function(e) {
				if (e.target.className == 'b-video-share') {
					shareBlock.fadeOut();
				}
			});
			shareBlock.find('.b-video-share-play').click(function() {
				shareBlock.fadeOut();
				var sharedVideo = $(this).closest('.b-video').find('VIDEO');
				if (sharedVideo.relCurrentTime() >= 90) {
					sharedVideo.currentTime(0).play();
				} else {
					sharedVideo.play();
				}
			});
		});
	}
	// maybe make some cookies here
	if ($('BODY.home').length) {
		gogoSplashScreen();
	}
});

/*
 * jQuery spritely 0.3 beta
 * http://spritely.net/
 *
 * Documentation:
 * http://spritely.net/documentation/
 *
 * Copyright 2010, Peter Chater, Artlogic Media Ltd, http://www.artlogic.net/
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Removed unused stuff
 */
(function($) {
	//initialize cross-browser HTML-Video-Tag

	$._spritely = {
		// shared methods and variables used by spritely plugin
		animate: function(options) {
			var el = $(options.el);
			var el_id = el.attr('id');
			options = $.extend(options, $._spritely.instances[el_id] || {});
			if (options.play_frames && !$._spritely.instances[el_id]['remaining_frames']) {
				$._spritely.instances[el_id]['remaining_frames'] = options.play_frames + 1;
			}
			if (options.type == 'sprite' && options.fps) {
				var frames;
				var animate = function(el) {
					var w = options.width, h = options.height;
					if (!frames) {
						frames = [];
						total = 0
						for (var i = 0; i < options.no_of_frames; i ++) {
							frames[frames.length] = (0 - total);
							total += w;
						}
					}
					if ($._spritely.instances[el_id]['current_frame'] >= frames.length - 1) {
						$._spritely.instances[el_id]['current_frame'] = 0;
					} else {
						$._spritely.instances[el_id]['current_frame'] = $._spritely.instances[el_id]['current_frame'] + 1;
					}
					var yPos = $._spritely.getBgY(el);
					el.css('background-position', frames[$._spritely.instances[el_id]['current_frame']] + 'px ' + yPos);
					if (options.bounce && options.bounce[0] > 0 && options.bounce[1] > 0) {
						var ud = options.bounce[0]; // up-down
						var lr = options.bounce[1]; // left-right
						var ms = options.bounce[2]; // milliseconds
						el
							.animate({top: '+=' + ud + 'px', left: '-=' + lr + 'px'}, ms)
							.animate({top: '-=' + ud + 'px', left: '+=' + lr + 'px'}, ms);
					}
				}
				if ($._spritely.instances[el_id]['remaining_frames'] && $._spritely.instances[el_id]['remaining_frames'] > 0) {
					$._spritely.instances[el_id]['remaining_frames'] --;
					if ($._spritely.instances[el_id]['remaining_frames'] == 0) {
						$._spritely.instances[el_id]['remaining_frames'] = -1;
						delete $._spritely.instances[el_id]['remaining_frames'];
						return;
					} else {
						animate(el);
					}
				} else if ($._spritely.instances[el_id]['remaining_frames'] != -1) {
					animate(el);
				}
			} else if (options.type == 'pan') {
				if (!$._spritely.instances[el_id]['_stopped']) {
					if (options.dir == 'left') { 
						$._spritely.instances[el_id]['l'] = ($._spritely.instances[el_id]['l'] - (options.speed || 1)) || 0;
					} else {
						$._spritely.instances[el_id]['l'] = ($._spritely.instances[el_id]['l'] + (options.speed || 1)) || 0;
					}
					var bp_top = $._spritely.getBgY(el);
					$(el).css('background-position', $._spritely.instances[el_id]['l'] + 'px ' + bp_top);
				}
			}
			$._spritely.instances[el_id]['options'] = options;
			window.setTimeout(function() {
				$._spritely.animate(options);
			}, parseInt(1000 / options.fps));
		},
		randomIntBetween: function(lower, higher) {
			return parseInt(rand_no = Math.floor((higher - (lower - 1)) * Math.random()) + lower);
		},
		getBgY: function(el) {
			if ($.browser.msie) {
				// fixme - the background-position property does not work
				// correctly in IE so we have to hack it here... Not ideal
				// especially as $.browser is depricated
				var bgY = $(el).css('background-position-y') || '0';
			} else {
				var bgY = ($(el).css('background-position') || ' ').split(' ')[1];
			}
			return bgY;
		},
		getBgX: function(el) {
			if ($.browser.msie) {
				// see note, above
				var bgX = $(el).css('background-position-x') || '0';
			} else {
				var bgX = ($(el).css('background-position') || ' ').split(' ')[0];
			}
			return bgX;
		},
		get_rel_pos: function(pos, w) {
			// return the position of an item relative to a background 
			// image of width given by w
			var r = pos;
			if (pos < 0) {
				while (r < 0) {
					r += w;
				}
			} else {
				while (r > w) {
					r -= w;
				}
			}
			return r;
		}
	};
	$.fn.extend({
		spritely: function(options) {
			var options = $.extend({
				type: 'sprite',
				do_once: false,
				width: null,
				height: null,
				fps: 12,
				no_of_frames: 2,
				stop_after: null
			}, options || {});
			var el_id = $(this).attr('id');
			if (!$._spritely.instances) {
				$._spritely.instances = {};
			}
			if (!$._spritely.instances[el_id]) {
				$._spritely.instances[el_id] = {current_frame: -1};
			}
			$._spritely.instances[el_id]['type'] = options.type;
			$._spritely.instances[el_id]['depth'] = options.depth;
			options.el = this;
			options.width = options.width || $(this).width() || 100;
			options.height = options.height || $(this).height() || 100;
			var get_rate = function() {
                return parseInt(1000 / options.fps);
            }
            if (!options.do_once) {
				window.setTimeout(function() {
					$._spritely.animate(options);
				}, get_rate(options.fps));
			} else {
				$._spritely.animate(options);
			}
			return this; // so we can chain events
		},
		sprite: function(options) {
			var options = $.extend({
				type: 'sprite',
				bounce: [0, 0, 1000] // up-down, left-right, milliseconds
			}, options || {});
			return $(this).spritely(options);
		},
		spSet: function(prop_name, prop_value) {
			var el_id = $(this).attr('id');
			$._spritely.instances[el_id][prop_name] = prop_value;
			return this;
		},
		spGet: function(prop_name, prop_value) {
			var el_id = $(this).attr('id');
			return $._spritely.instances[el_id][prop_name];
		},
		spStop: function(bool) {
			$(this).each(function() {
				var el_id = $(this).attr('id');
				$._spritely.instances[el_id]['_last_fps'] = $(this).spGet('fps');
				$._spritely.instances[el_id]['_stopped'] = true;
				$._spritely.instances[el_id]['_stopped_f1'] = bool;
				if ($._spritely.instances[el_id]['type'] == 'sprite') {
					$(this).spSet('fps', 0);
				}
				if (bool) {
					// set background image position to 0
					var bp_top = $._spritely.getBgY($(this));
					$(this).css('background-position', '0 ' + bp_top);
				}
			});
			return this;		
		},
		spStart: function() {
			$(this).each(function() {
				var el_id = $(this).attr('id');
				var fps = $._spritely.instances[el_id]['_last_fps'] || 12;
				$._spritely.instances[el_id]['_stopped'] = false;
				if ($._spritely.instances[el_id]['type'] == 'sprite') {
					$(this).spSet('fps', fps);
				}
			});
			return this;		
		},
		spToggle: function() {
			var el_id = $(this).attr('id');
			var stopped = $._spritely.instances[el_id]['_stopped'] || false;
			var stopped_f1 = $._spritely.instances[el_id]['_stopped_f1'] || false;
			if (stopped) {
				$(this).spStart();
			} else {
				$(this).spStop(stopped_f1);
			}
			return this;		
		},
		fps: function(fps) {
			$(this).each(function() {
				$(this).spSet('fps', fps);
			});
			return this;
		},
		spChangeDir: function(dir) {
			$(this).each(function() {
				$(this).spSet('dir', dir);
			});
			return this;
		}
	})
})(jQuery);

// Stop IE6 re-loading background images continuously
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

$(document).ready(function() {
	var stripe = $('<b class="menu-item-stripe"></b>');
	var stripe_noplay = $('<b class="menu-item-stripe menu-item-stripe_no-play"></b>');

	$('.b-previews-item-in[href*=/portfolio/print/] .b-previews-item-ico').addClass('b-previews-item-ico_zoom');
	$('.b-content-i.t-text:has(.b-previews)').removeClass('t-text').find('H2').addClass('t');
	$('.current-menu-ancestor').siblings().removeClass('current_page_parent');
	$('.current-menu-item:not(.current-page-ancestor)').prepend(stripe);
	$('.current_page_parent:not(.current-menu-item):not(.current-page-ancestor):not(.current-menu-ancestor)').prepend(stripe_noplay);
	$('.b-sidebar-menu:not(:has(.current-menu-item)):not(:has(.menu-item-stripe)) .current-menu-ancestor:last').prepend(stripe_noplay);

	$('.b-cube').sprite({fps: 24, no_of_frames: 99}).click(function() {gogoSplashScreen();});
	$('.menu-item-stripe:not(.menu-item-stripe_no-play)').sprite({fps: 24, no_of_frames: 22, play_frames: 22});
	
	$('.b-contacts-map,.b-content-i IMG[width=549]').click(function() {
		if(!isAnIOS){
			if ($('.b-splash_image').length) {
				$('.b-splash_image').fadeIn();
				return false;
			} else {
				var imageAddress = $(this).attr('href') || $(this).attr('src');
				$(this).addClass('g-clickable');
				if (imageAddress) {
					var imageSplash = $('<div class="b-splash b-splash_image" style="display:none;" title="Click to close"><img class="b-splash-image" src="' + imageAddress + '" alt="Map" title="" /></div>').fadeIn().click(function() {
						$(this).fadeOut();
					});
					$('body').append(imageSplash);
					return false;
				}
			}
		}
	});
});

