jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, ';path=/', domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var bid = 0;
var bpid = 0;

$(document).ready(function() 
{
	var conundrum = $.cookie("conundrum");
	
	if(conundrum=="false")
	{
			$("div.conundrum").removeClass("wired");
			$("div.conundrum").addClass("unraveled");
	}	
	
	var w = $(".scrolling").width();

	$(".scroll").jCarouselLite({
					mouseWheel: true,
				    btnNext: ".next",
				    btnPrev: ".prev",
				    speed: 20,
				    visible: 15
				});
				
	$(".scroll").css("width",w+40+'px');
	
	$(".scroll img").thumbPopup();
	
	var fm = $('#big_foto').attr('src');

	if(bid!=undefined && bid!=0)
	{
		bigf(fm, bid);
	}
	if(bpid!=undefined && bpid!=0)
	{
		bigf2(bpid);
	}
	$(document).bind('keydown', 'ctrl+left', function (evt)
	{
		var link = $('#lhref').attr('href');
		if(link!=undefined)
		{
			window.location.href=link;
		}
	});
	$(document).bind('keydown', 'ctrl+right', function (evt)
	{
		var link = $('#rhref').attr('href');
		if(link!=undefined)
		{
			window.location.href=link;
		}
	});
});

(function($) {
	$.fn.thumbPopup = function(options)
	{
		//Combine the passed in options with the default settings
		settings = jQuery.extend({
			popupId: "thumbPopup",
			popupCSS: {},
			imgSmallFlag: "_t",
			imgLargeFlag: "_l",
			cursorTopOffset: 100,
			cursorLeftOffset: 15,
			loadingHtml: "<span style='padding: 5px;'></span>"
		}, options);
		
		
		//Attach hover events that manage the popup
		$(this)
		.hover(setPopup);
		$('div.enlarge').mouseout(hidePopup);
		
		function setPopup(event)
		{
			var fullImgURL = $(this).attr("src");
			var id = $(this).attr("rel");
			var link = $('#'+id).attr("href");
			
			$(this).data("hovered", true);
			
			//Load full image in popup
			$("<img />")
			.bind("load", {thumbImage: this}, function(event)
			{
				//Only display the larger image if the thumbnail is still being hovered
				if ($(event.data.thumbImage).data("hovered") == true) {
					updatePopupPosition(event);
					$('div.enlarge').empty().html('<a href="'+link+'"><img src="'+fullImgURL+'" alt="" /></a>');
					$('div.enlarge').show();
				}
				$(event.data.thumbImage).data("cached", true);
			})
			.attr("src", fullImgURL);
			
			updatePopupPosition(event);			
		}
		
		function updatePopupPosition(event)
		{
			var windowSize = getWindowSize();
			var popupSize = getPopupSize();
			if (windowSize.width + windowSize.scrollLeft < event.pageX + popupSize.width + settings.cursorLeftOffset){
				//$('div.enlarge').css("left", event.pageX - popupSize.width - settings.cursorLeftOffset-40);
				$('div.enlarge').css("left", event.pageX -160);
			} else {
				$('div.enlarge').css("left", event.pageX -60);
			}
		}
		
		function hidePopup(event)
		{
			$(this).data("hovered", false);
			
			$('div.enlarge').empty().hide();
		}
		
		function getWindowSize() {
			return {
				scrollLeft: $(window).scrollLeft(),
				scrollTop: $(window).scrollTop(),
				width: $(window).width(),
				height: $(window).height()
			};
		}
		
		function getPopupSize() {
			return {
				width: 232,
				height: $('div.enlarge').height()
			};
		}

		//Return original selection for chaining
		return this;
	};
})(jQuery);

function conundrum()
{
	var conundrum = $.cookie("conundrum");
	
	if(conundrum==null || conundrum=="false")
	{
		$.cookie("conundrum", true);
		$("div.conundrum").removeClass("unraveled");
		$("div.conundrum").addClass("wired");
	}
	else if(conundrum=="true")
	{
		$.cookie("conundrum", false);
		$("div.conundrum").removeClass("wired");
		$("div.conundrum").addClass("unraveled");
	}

}
function contacts()
{
	var mail = $('#email').val();
	var message = $('#message').val();
	var err = "";

	
	if(mail.length<3)
	{
		err = 'Укажите электронную почту.';
	}
	if(err!="")
	{
		err += '<br>';
	}
	if(message.length<3)
	{
		err += 'Укажите текст сообщения.';
	}

	$('#error').html(err);
	
	if(err=="")
	{
		$('#form').submit();
	}
}

var cur_big = "";
var cur_id = "";
var cur_picture_path="";
var big_photo_path="";
function bigf(link, id)
{
	if(cur_id!=undefined && cur_id>0)
	{
		var resore = '<a href="javascript:{}" onclick="bigf(\''+big_photo_path+'\','+cur_id+');"><img id="picture_'+cur_id+'" src="'+cur_picture_path+'"/></a>';
		$('#itd_'+cur_id).html(resore);
		$('#itab_'+id).removeClass('current');
	
		$('#big_foto').attr('src',link);
	}
	
	$('#itab_'+id).addClass('current');
	var picture_path = $('#picture_'+id).attr('src');
	$('#itd_'+id).html('<div><img class="mark" src="/images/dot.gif" /><img src="'+picture_path+'"/></div>');

	cur_id = id;
	
	cur_picture_path = picture_path;
	big_photo_path = link;
}

function bigf2(id)
{
	if(cur_id!=undefined && cur_id>0)
	{
		var resore = '<a href="javascript:{}" onclick="bigf2('+cur_id+');"><img id="picture_'+cur_id+'" src="'+cur_picture_path+'"/></a>';
		$('#itd_'+cur_id).html(resore);
		$('#itab_'+id).removeClass('current');
	}
	
	$('#itab_'+id).addClass('current');
	var picture_path = $('#picture_'+id).attr('src');
	$('#itd_'+id).html('<div><img class="mark" src="/images/dot.gif" /><img src="'+picture_path+'"/></div>');
	
	$('#big_foto').html('<img src="'+picture_path+'"/>');
	
	cur_id = id;
	cur_picture_path = picture_path;
}
