
$.hasFlash = function() {
    // look for a flag in the query string to bypass flash detection
    if(/hasFlash\=true/.test(location)) return true;
    if(/hasFlash\=false/.test(location)) return false;
    var pv = $.hasFlash.playerVersion().match(/\d+/g);
    var rv = String([arguments[0], arguments[1], arguments[2]]).match(/\d+/g);
    for(var i = 0; i < 3; i++) {
		pv[i] = parseInt(pv[i] || 0);
		rv[i] = parseInt(rv[i] || 0);
		// player is less than required
		if(pv[i] < rv[i]) return false;
		// player is greater than required
		if(pv[i] > rv[i]) return true;
    }
    // major version, minor version and revision match exactly
    return true;
};
$.hasFlash.playerVersion = function() {
    // ie
    try {
	try {
	    // avoid fp6 minor version lookup issues
	    // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
	    var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
	    try { axo.AllowScriptAccess = 'always';} 
	    catch(e) { return '6,0,0'; }				
	} catch(e) {}
	return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
	// other browsers
    } catch(e) {
	try {
	    if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
		return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
	    }
	} catch(e) {}		
    }
    return '0,0,0';
};
$.html5video = function($link){
    var fileName = $link.attr("href");
    fileName = fileName.replace(/#/,"");
    $link.fancybox({
	titleShow: false,
	overlayOpacity: 0.5,
	overlayColor: '#999',
	padding: 0,
	margin: 0,
	zoomSpeedIn: 0,
	zoomSpeedOut: 0,
	transitionIn: "none",
	transitionOut: "none",
	width: 900,
	height: 368,
	scrolling: "no",
	autoDimensions: "false",
	centerOnScroll: "true",
	content: "<video poster='assets/flash/" + fileName + ".jpg' controls='controls' width='900' height='368' src='assets/flash/" + fileName + ".mp4' style='margin-bottom:-16px;' />"
    });
};
		
$.flashVideo = function($link){
    var fileName = $link.attr("href");
    fileName = fileName.replace(/#/,"");
    $link.fancybox({
	titleShow: false,
	overlayOpacity: 0.5,
	overlayColor: '#999',
	padding: 0,
	margin: 0,
	zoomSpeedIn: 0,
	zoomSpeedOut: 0,
	transitionIn: "none",
	transitionOut: "none",
	autoDimensions: "false",
	centerOnScroll: "true",
	width: 900,
	height: 400,
	scrolling: "no",
	type: 'swf',
	href: 'assets/flash/mediaplayer.swf',
	swf: {
	    width: 720,
	    height: 437,
	    wmode: 'transparent',
	    allowfullscreen: 'true',
	    flashvars: 'width=900&height=400&autostart=false&file=' + fileName + '.mp4&repeat=false&showdownload=false&showdigits=true&shownavigation=true&skin=assets/flash/stylish.swf'
	}
    });
};

jQuery.fn.tabs = function() {
	var $tabsNav = $("#popupNav");
	var $popupContent = $("#popupContent");
	var $popupContentDiv = $popupContent.find("div.content");
    this.each(function(){
    	$(this).hide();
    	var tabName = $(this).find("h2:first").text();
    	if(tabName == "")
    	{
    		tabName = $(this).find("h3:first").text();
    	}
    	$tabsNavLink = $tabsNav.append('<li><a href="#">' + tabName + '</a><span>|</span></li>');
    });
    $tabsNav.find('a').click(function(event){
		event.preventDefault();
		var index = $(this).parent().index();
		$popupContent.attr('style','').attr('class','').data('jsp','');
		$popupContentDiv.hide().eq(index).show();
		$popupContent.append($popupContentDiv);
		$popupContent.find("> div").not(".content").remove();
		$popupContent.jScrollPane();
	});
    $tabsNav.find('li:first').addClass('first').find('a').trigger('click');
    $tabsNav.find('li:last').addClass('last');    
};

jQuery.fn.content_box = function(){
    var isIE6 = !$.support.opacity && !window.XMLHttpRequest;
    var isiPad = navigator.userAgent.match(/iPad/i);
    
    var $content_box_overlay = $('<div id="content_box_overlay"></div>').appendTo('body');
    var $content_box = $('<div id="content_box"><a href="#" id="content_box_close"></a><ul id="popupNav"></ul><div id="popupContent"></div></div>').appendTo('body');
    var $content_box_shadow = $('<div id="content_box_shadow"></div>').appendTo('body');
    var $content_box_close = $("#content_box_close");
    var $popupContent = $("#popupContent");
	var $popupContentDiv = $popupContent.find("div.content");
    var $popupNav = $("#popupNav");
    
    if (isIE6) {
    	$content_box_shadow.css({background: 'transparent', filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='assets/images/design/popupBg.png', sizingMethod='scale')"});
    }
    
	$content_box_overlay.bind('click', function(event){
		event.preventDefault();
		$content_box_overlay.hide().css('z-index', -500);
		$content_box_shadow.hide().css('z-index', -501);
		$content_box.hide().css('z-index', -502);
		$popupNav.empty();
		var path = $.address.path().split("/");
		$.address.path("/" + path[1]);
	});

	$content_box_close.bind('click', function(event){
		event.preventDefault();
		$content_box_overlay.hide().css('z-index', -500);
		$content_box_shadow.hide().css('z-index', -501);
		$content_box.hide().css('z-index', -502);
		$popupNav.empty();
		var path = $.address.path().split("/");
		$.address.path("/" + path[1]);
	});
	
	 function createObject (type, point, style, description) {
         var allowObjects = ["Placemark", "Polyline", "Polygon"],
             index = YMaps.jQuery.inArray( type, allowObjects),
             constructor = allowObjects[(index == -1) ? 0 : index];
             description = description || "";
         
         var object = new YMaps[constructor](point, {style: style, hasBalloon : !!description});
         object.description = description;
         
         return object;
     }
	 
     return this.each(function(){
		var $content_box_link = $(this);
		$content_box_link.bind('click', function(event){
			event.preventDefault();
			
			var linkVal = $(this).attr('href');
			
			$content_box_overlay.show().css('z-index', 500);  
		    $content_box_shadow.show().css('z-index', 501);
		    $content_box.show().css('z-index', 502);
		    $content_box_close.show();

		    $popupContent.html($(linkVal + '-content').html());
		    
		    /* Fancybox */
		    $video = document.createElement('video');
		    
		    if(!!$video.canPlayType && document.createElement('video').canPlayType('video/mp4; codecs="mp4v.20.8"')) {
				$("a.video").each(function(){
					$.html5video($(this));
				});
		    } else if($.hasFlash(10,0,0)) {
				$("a.video").each(function(){
					$.flashVideo($(this));
				});
		    }
		    
		    $("#popupContent .content a.fancybox").fancybox({titlePosition: 'over', titleShow: true});
		    
		    if(linkVal == "#contacts-map")
		    {
		    	$popupContent.append('<div id="YMapsID-3914" style="width:525px;height:340px"></div>');
		        var map = new YMaps.Map(YMaps.jQuery("#YMapsID-3914")[0]);
		        map.setCenter(new YMaps.GeoPoint(37.371325,55.506847), 10, YMaps.MapType.MAP);
		        map.enableScrollZoom();
		        map.addControl(new YMaps.Zoom());
		        map.addControl(new YMaps.ToolBar());
		        map.addControl(new YMaps.TypeControl());

		        YMaps.Styles.add("constructor#pmlbmPlacemark", {
		                iconStyle : {
		                    href : "http://api-maps.yandex.ru/i/0.3/placemarks/pmgnm.png",
		                    size : new YMaps.Point(28,29),
		                    offset: new YMaps.Point(-8,-27)
		                }
		            });

		           map.addOverlay(createObject("Placemark", new YMaps.GeoPoint(37.153914,55.417662), "constructor#pmlbmPlacemark", "<strong>Медведево</strong><br/><br/>+7 (495) 772-33-47"));
		    } else {
		    	 var $popupTabs =  $popupContent.find('.tab');
				    
				 if($popupTabs.length > 0)
				 {
				  	$popupContent.find('.tab').tabs();
				 } else {
				   	$popupContent.attr('style','').attr('class','').data('jsp','');
				   	$popupContent.append($popupContentDiv);
				   	$popupContent.find("> div").not(".content").remove();
				   	$popupContent.jScrollPane();
				 }
		    }
		});
     });
};

$(document).ready(function(){
	// For use within normal web clients 
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	
	var d = document;
	var winIE = (navigator.userAgent.indexOf("Opera")==-1 && (d.getElementById && d.documentElement.behaviorUrns)) ? true : false;
	function init() {
		if(winIE && d.documentElement.clientWidth && d.getElementById("container")) {
			var c = d.getElementById("centerer").style;
			c.width = (d.documentElement.clientWidth<980) ? "980px" : "100%";
			c.height = (d.documentElement.clientHeight<600) ? "600px" : "100%";
			$("body").attr('id','ie6');
		}
	};
	window.onload = init;
	window.onresize = init;
	$("#container .point > a, #contacts-link").content_box();
	$("#genplan-link").fancybox();
	
	$video = document.createElement('video');
    
    if(!!$video.canPlayType && document.createElement('video').canPlayType('video/mp4; codecs="mp4v.20.8"')) {
		$("#video-link").each(function(){
			$.html5video($(this));
		});
    } else if($.hasFlash(10,0,0)) {
		$("#video-link").each(function(){
			$.flashVideo($(this));
		});
    }
    if(isiPad)
    {
    	$("body").css("margin","-25px 0 0 0");
    }
});
