function getCookie(c_name){
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays, path){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) +
    ((path) ? ";path=" + path : "" );

}

function checkCookie(){
    username = getCookie('username');
    if (username != null && username != "") {
        alert('Welcome again ' + username + '!');
    }
    else {
        username = prompt('Please enter your name:', "");
        if (username != null && username != "") {
            setCookie('username', username, 365);
        }
    }
}

(function($) { 
	$(function() {
		
		$("#contrast_switch_on").click(function () {
			setCookie('contrast', true, 365, "/");			
			$("#contrast_switch_on").addClass("invisible");
			$("#contrast_switch_off").removeClass("invisible");
			$("body").addClass('contrast');
			return false;
		});
		
		$("#contrast_switch_off").click(function () {
			setCookie('contrast', '', 365, "/");
			$("#contrast_switch_off").addClass("invisible");
			$("#contrast_switch_on").removeClass("invisible");
			$("body").removeClass('contrast');
			return false;
		});		
		
		$(function () {	
			$(".b-partners-viewer .right-arrow").click(function () {
				var next_partner = $(".b-partners-article:visible:first").next();
				$(".b-partners-article").hide();
				if(next_partner.length) {
					next_partner.fadeIn("slow");
				} else {
					$(".previews-inside dl:first").fadeIn("slow");
				}	
			});	
			$(".b-partners-viewer .left-arrow").click(function () {
				var next_partner = $(".b-partners-article:visible:first").prev();
				$(".b-partners-article").hide();
				if(next_partner.length) {
					next_partner.fadeIn("slow");
				} else { 
					$(".previews-inside dl:last").fadeIn("slow");
				}
			
			});
		});
		
		$(function () {
			if($("#ru").length) {
				var flash_required_message = 'Для просмотра необходим Adobe Flash player';
			} else if($("#en").length) {
				var flash_required_message = 'Adobe Flash player required';
			} else if($("#fr").length) {
				var flash_required_message = 'Adobe Flash player requis';
			}			
			$("body").data('flash_required_message', flash_required_message);
		});
		
		/* + simple overlay */
		$(".overlay-trigger").overlay(
		{
			target:".b-overlay",
			top:"center",
			expose:"#000",
			close:".b-overlay-close",
			onBeforeLoad: function () {
				var overlay_title = this.getTrigger().attr('title');
			    var overlay_conent_url = this.getTrigger().attr('href');
			    if (overlay_conent_url) {
			    	this.getOverlay().remove('img').append('<img/>').find('img').attr('src', overlay_conent_url);
			    }		    
			    if (overlay_title) {
			    	this.getOverlay().find('.b-overlay-title').text(overlay_title);
			    }
			},
			onClose:function () {
				this.getOverlay().find('img').remove();
			}
		});
		/* - simple overlay */
		
		/* + photo blue overlay hide */
		$(".b-photos-article-image-wrapper img").mouseover(function () {
			$(this).parent().next(".information").stop().removeAttr("style");
		});		
		$(".b-photos-article-image-wrapper img.picture").mouseout(function () {
			$(this).parent().next(".information").fadeOut();
		});
		/* - photo blue overlay hide */

		/* + sports icons blue overlay hide */
		$(".b-games-sports-article img.picture").mouseover(function () {
			$(this).next(".information").stop().removeAttr("style");
		});	
		$(".b-games-sports-article img.picture").mouseout(function () {
			$(this).next(".information").fadeOut();
		});
		/* - sports icons overlay hide */
		
		$("embed").attr("wmode", "opaque").css("position", "relative");

		/* + gallery overlay */
		
		$(".sidebar #b-overlay-gallery-photo").clone().appendTo("body");
		$(".sidebar #b-overlay-gallery-photo").remove();
		
		var obPhotoOverlayGallery = $("#b-overlay-gallery-photo").overlay({top: 'center', expose: '#000', close: '.b-overlay-gallery-close', api: true});
		var obPhotoCarouselGallery = $("#b-overlay-gallery-photo div.b-photo-carousel").scrollable({size: 4, api: true}); 
		
		$("#b-overlay-gallery-video").clone().appendTo("body");
		$("#b-overlay-gallery-video:first").remove();
		
		var obVideoOverlayGallery = $("#b-overlay-gallery-video").overlay({top: 'center', expose: '#000', close: '.b-overlay-gallery-close', api: true});
		var obVideoCarouselGallery = $("#b-overlay-gallery-video div.b-photo-carousel").scrollable({size: 4, api: true}); 
				
		/**
		 * Получает объект листалки в активном оверлее
		 */
		function getCurrentCarousel() {
			if(typeof(obPhotoOverlayGallery) != 'undefined') {
				if (obPhotoOverlayGallery.isOpened()) {
					return obPhotoCarouselGallery;
				}
			} 
			
			if(typeof(obVideoOverlayGallery) != 'undefined') {
				if(obVideoOverlayGallery.isOpened()) {
					return obVideoCarouselGallery;
				}
			}
		}
			
		$(document).bind('keydown', 'Ctrl+right', function () {
			var carousel = getCurrentCarousel();				
			if (typeof(carousel) != 'undefined') {
				if(!$(carousel.getVisibleItems()[3]).hasClass("active")) {
					$(carousel.getVisibleItems()[3]).click();
				}
			}
		});
		
		$(document).bind('keydown', 'Ctrl+left', function () {
			var carousel = getCurrentCarousel();
			if (typeof(carousel) != 'undefined') {
				if(!$(carousel.getVisibleItems()[1]).hasClass("active") && !$(carousel.getVisibleItems()[0]).hasClass("active")) {
					$(carousel.getVisibleItems()[1]).click();
				} else if(!$(carousel.getVisibleItems()[0]).hasClass("active")) {		
					$(carousel.getVisibleItems()[0]).click();
				}
			}
		});
		
		/* - gallery overlay */
		
		/* + video gallery */
		function ie_stop_video () {
			// Hack for ie to stop video playing
			if (jQuery.support.leadingWhitespace == false) {
				var current_video_li = $("#b-overlay-gallery-video .b-overlay-gallery-image li:visible");
				if (current_video_li.length) {
					setTimeout(function () {
					$("embed", current_video_li).clone().appendTo(current_video_li);
					$("embed:first", current_video_li).remove();
					}, 100);
				}
			}
		}
		if (typeof(obVideoOverlayGallery) != "undefined") {
			obVideoOverlayGallery.onBeforeClose(function () {
				ie_stop_video();
			});
		}
		
		$(".b-videos-article a.b-overlay-gallery-trigger").click(function () {
			obVideoOverlayGallery.load();
			$('#'+$(this).attr("data-gallery_item_id")).click();
			return false;
		});
				
		$("#b-overlay-gallery-video .b-photo-carousel li").live("click", function () { 
			
			if($($("img", this).attr("data-target")).is(":visible")) {
				return false;
			}			

			if (!swfobject.hasFlashPlayerVersion("9.0.18")) {
				$("#b-overlay-gallery-video .b-overlay-gallery-title").html('<a style="visibility:visible;" href="http://www.adobe.com/go/getflashplayer">'+$("body").data('flash_required_message')+'</a>');
				$("#b-overlay-gallery-video .b-overlay-gallery-image").hide();
				$("#b-overlay-gallery-video .b-overlay-gallery-description").hide();
				$("#b-overlay-gallery-video .b-overlay-gallery-navigation").hide();
				return false;
			}	
			
			ie_stop_video ();
			
			$("#b-overlay-gallery-video .b-photo-carousel li").removeClass("active");
			$(this).addClass("active");
			
			$("#b-overlay-gallery-video .b-overlay-gallery-image ul li").hide();
			$($("img", this).attr("data-target")).show();
			
			$("#b-overlay-gallery-video .b-overlay-gallery-image object param[name='scale']").attr('value', 'noscale');
			$("#b-overlay-gallery-video .b-overlay-gallery-image object").attr("width", "100%").attr("height", "340");	
			$("#b-overlay-gallery-video .b-overlay-gallery-image embed").attr("width", "100%").attr("height", "340");
			if ($("img", this).attr('data-url')) {
				$("#b-overlay-gallery-video .b-overlay-gallery-title").html('<a href="'+$("img", this).attr('data-url')+'">'+$("img", this).attr('title')+'</a>');
			} else {
					$("#b-overlay-gallery-video .b-overlay-gallery-title").html($("img", this).attr('title'));
			}
			$("#b-overlay-gallery-video .b-overlay-gallery-description").html($("img", this).attr('data-description'));
		    return false;
		});
		
		/* - video gallery */
		
		/* + photo gallery */
			
		$(".b-photos-previews a.b-overlay-gallery-trigger").click(function () {
			obPhotoOverlayGallery.load();
			$('#'+$(this).attr("data-gallery_item_id")).click();		
			return false;
		});
		
		$("#b-overlay-gallery-photo .b-photo-carousel li").live("click", function () { 

			$("#b-overlay-gallery-photo .b-photo-carousel li").removeClass("active");
			$(this).addClass("active");
			
			var __instance = $("img", this);	
			$("#b-overlay-gallery-photo .b-overlay-gallery-image img").fadeOut(100, function () {			
				$(this).remove();
				setOverlayGalleryImage(__instance.attr("data-target"));
				if (__instance.attr('data-url')) {
					$("#b-overlay-gallery-photo .b-overlay-gallery-title").html('<a href="'+__instance.attr('data-url')+'">'+__instance.attr('title')+'</a>');
				} else {
					$("#b-overlay-gallery-photo .b-overlay-gallery-title").html(__instance.attr('title'));
				}
				$("#b-overlay-gallery-photo .b-overlay-gallery-description").html(__instance.attr('data-description'));
			});
		    return false;
		});
		
		function  setOverlayGalleryImage (image_src) {
			
			$("#b-overlay-gallery-photo .b-overlay-gallery-image").html("<img/>").find("img").css("opacity", "0").load(function () {
		    	
		    	var image_max_height = 465;
		    	
		    	var image = $(this).css({ width: "", height: "" }).removeAttr("width").removeAttr("height");
		    	var image_width = image.width();
		    	var image_height = image.height();
		    	
		    	if(image_height > image_max_height) {
		    		image_width = (image_width * image_max_height) / image_height;
		    		$(this).css({"width": image_width+"px", "height": image_max_height+"px"});
		    	} else {
		    		//$(this).css({"width": "100%", "height": ""});		
		    	}
		    	$(this).css("opacity", "");
		    	$(this).unbind("load");
		    	
		    }).attr("src", image_src);
		}
		/* - photo gallery */

	});
})(jQuery);