$(function() {

	$.get('functions.php?func=rss', function(html) {
		$('.bl.jj .entry').html(html);
	});
	
	$.get('functions.php?func=twitter', function(html) {
		$('.bl.tw .entry').html(html);
	});

	pos();

	$('#dragme').draggable();
	
	$('#captainb .bb').click(function() {
		$('#captainb .balloon').show();
		$(this).hide();
	});

	$('#on').click(function() {
		$(this).hide();
		$('#tv').effect('shake', {direction: 'down', times: 5}, 50, function() {
			$('#tv').removeClass('noise');
			$('#channel-l, #channel-r').show();
		});
	});

	$('*[id^=channel]').click(function() {
		$('#channel-l, #channel-r, #newsb .balloon').hide();
		$('#tv').addClass('noise').css({backgroundImage: 'url(img/tv-noise.gif)'});;
		var pics = ['img/tv-rapist.jpg', 'img/tv-superjail.jpg', 'img/tv-japan.jpg', 'img/tv-napoleon.jpg', 'img/tv-futurama.jpg', 'img/tv-pres.jpg', 'img/tv-pron.jpg', 'img/tv-hitler.jpg'];
		var id = parseInt($('#tv span').html());
		var img;
		if ($(this).attr('id') == 'channel-l') {
			if (typeof pics[id - 2] != 'undefined') {
				img = pics[id - 2];
				id = id - 1;
			} else {
				img = pics[pics.length - 1]
				id = pics.length;
			}
		} else if ($(this).attr('id') == 'channel-r') {
			if (typeof pics[id] != 'undefined') {
				img = pics[id];
				id = id + 1;
			} else {
				id = 1;
				img = pics[0];
			}
		}
		/*if ($(this).attr('id') == 'channel-l') {
			var el = $('.inside.current').prev();
		} else if ($(this).attr('id') == 'channel-r') {
			var el = $('.inside.current').next();
		}
		if (!el.length) {
			if ($(this).attr('id') == 'channel-l') {
				var el = $('.inside:last');
			} else if ($(this).attr('id') == 'channel-r') {
				var el = $('.inside:first');
			}
		}
		$('.inside').hide().removeClass('current');
		$(el).show().addClass('current');*/
		/*setTimeout(function() {
			$('#tv').removeClass('noise').css({backgroundImage: 'url(' + img + ')'});
			$('#tv span').html(id);
			$('#channel-l, #channel-r').show();
		}, 500);*/
		var image = new Image();
		image.src = img;
		image.onload = function() {
			setTimeout(function() {
				$('#tv').removeClass('noise').css({backgroundImage: 'url(' + img + ')'});
				$('#tv span').html(id);
				$('#channel-l, #channel-r').show();
			}, 500);
		}
	});

	$('#nav-america').click(function() {
        if ($('#prof').hasClass('ololo')) {
	        $('#prof').qtip('destroy');
            $('#prof').animate({bottom: -246}, 200).removeClass('ololo');
        }
		$('#dragme').animate({left:-10000, top:0}, 200);
		$('body').removeClass('klukwa');
		$('#logo').css({backgroundPosition: '0px 0px'});
		location.hash = '#america';
	});

	$('#nav-tv').click(function() {
	    if ($('#prof').hasClass('ololo')) {
	        $('#prof').qtip('destroy');
            $('#prof').animate({bottom: -246}, 200).removeClass('ololo');
        }
		$('#dragme').animate({left:-10000, top:-1400}, 200);
		$('body').removeClass('klukwa');
		$('#logo').css({backgroundPosition: '0px 0px'});
		location.hash = '#news';
	});

	$('#nav-cv').click(function() {
	    if ($('#prof').hasClass('ololo')) {
	        $('#prof').qtip('destroy');
            $('#prof').animate({bottom: -246}, 200).removeClass('ololo');
        }
		$('#dragme').animate({left:-10000, top:-4400}, 200);
		$('body').removeClass('klukwa');
		$('#logo').css({backgroundPosition: '0px 0px'});
		location.hash = '#cv';
	});

    $('#nav-cont').click(function() {
		$('#prof').animate({bottom: 0}, 200, function() {
			if ($('#ui-tooltip-contact').length) {
				return;
			}
			$('#prof').qtip({
				id: 'contact',
				content: {
					text: function() {
						return $('#prof').html();
					}
				},
				position: {
					my: 'right bottom',
					at: 'left left',
					target: $('#prof'),
					adjust: {
						x: 0,
						y: 110
					}
				},
				show: {
					event: false,
					ready: true
				},
				hide: false,
				style: {
					classes: 'ui-tooltip-tipsy'
				}
			});
		}).addClass('ololo');
	    //pos();
	    location.hash = '#contact';
	    
	});

	$('#nav-soc').click(function() {
		if ($('#prof').hasClass('ololo')) {
	        $('#prof').qtip('destroy');
            $('#prof').animate({bottom: -246}, 200).removeClass('ololo');
        }
		$('#dragme').animate({left:-10000, top:-3000}, 200, function() {
			$('body').addClass('klukwa');
			//$('#logo').css({backgroundPosition: '0px -53px'});
		});
		location.hash = '#communism';
	});
    
    $('#ui-tooltip-contact input[name=name]').live('focus', function() {
        if ($(this).val() == 'имя') {
            $(this).val('').removeClass('ph');
        }
    }).live('blur', function() {
        if (!$(this).val()) {
            $(this).val('имя').addClass('ph');
        }
    });
    
    $('#ui-tooltip-contact input[name=email]').live('focus',function() {
        if ($(this).val() == 'электрическая почта') {
            $(this).val('').removeClass('ph');
        }
    }).live('blur', function() {
        if (!$(this).val()) {
            $(this).val('электрическая почта').addClass('ph');
        }
    });
    
     $('#ui-tooltip-contact textarea[name=text]').live('focus', function() {
        if ($(this).val() == 'с какой целью интересуетесь?') {
            $(this).val('').removeClass('ph');
        }
    }).live('blur', function() {
        if (!$(this).val()) {
            $(this).val('с какой целью интересуетесь?').addClass('ph');
        }
    });

	$('#ui-tooltip-contact input[type=button]').live('click', function() {
		var name = $('#ui-tooltip-contact input[name=name]').hasClass('ph') ? '' : $('#ui-tooltip-contact input[name=name]').val();
		var email = $('#ui-tooltip-contact input[name=email]').hasClass('ph') ? '' : $('#ui-tooltip-contact input[name=email]').val();
		var text = $('#ui-tooltip-contact textarea[name=text]').hasClass('ph') ? '' : $('#ui-tooltip-contact textarea[name=text]').val();
		$('#ui-tooltip-contact :input').removeClass('error');
		if (!$('#ui-tooltip-contact input[name=email]').hasClass('ph') && !email.match(/.+@.+\..+/)) {
			$('#ui-tooltip-contact input[name=email]').addClass('error');
			return false;
		}
		if ($('#ui-tooltip-contact textarea[name=text]').hasClass('ph')) {
			$('#ui-tooltip-contact textarea[name=text]').addClass('error');
			return false;
		}
		$.post('copulation.php', {name:name, email:email, text:text}, function(data) {
			if ($('#prof').hasClass('ololo')) {
				//$('#prof .balloon').hide();
	            $('#prof').qtip('destroy');
				$('#prof').animate({bottom: -246}, 200).removeClass('ololo');
			}
		});
	});
	
	if (location.hash) {
		switch (location.hash.replace('#', '')) {
			case 'captain':
				$('#nav-america').click();
				break;
			case 'news':
				$('#nav-tv').click();
				break;
			case 'communism':
				$('#nav-soc').click();
				break;
			case 'contact':
				$('#nav-cont').click();
				break;
			case 'cv':
				$('#nav-cv').click();
				break;
		}
	}

	$('.years div span').qtip({
		content: {
			text: function() {
				return $('#' + $(this).html()).html();
			}
		},
		position: {
			my: 'left center',
			at: 'bottom center',
			adjust: {
				x: 5,
				y: -3
			}
		},
		show: {
			event: 'click',
			modal: true
		},
		hide: false,
		style: 'ui-tooltip-tipsy'
	});
	
	$('.period').qtip({
		content: {
			text: function() {
				return $('#period-' + $(this).attr('class').replace('period', '').replace(/^\s+|\s+$/g, '')).html();
			}
		},
		position: {
			my: 'center',
			at: 'center'
		},
		show: {
			event: 'click',
			modal: true
		},
		hide: false,
		style: 'ui-tooltip-tipsy'
	});
	
	$('.logos li div').qtip({
		content: {
			text: function() {
				return $(this).next('.hidden').html();
			}
		},
		position: {
			my: 'bottom center',
			at: 'top center',
			adjust: {
				x: 5
			}
		},
		show: {
			event: 'click',
			modal: true
		},
		hide: false,
		style: 'ui-tooltip-tipsy'
	});
	
});

$(window).resize(function() {
	pos();
});

function pos() {
	var ww = $(window).width();
	var wh = $(window).height();
    if (ww < 1200) {
        ww = 1200;
    }
    $('#wrapper').css({width: 40000, height: 40000});
    $('#dragme').css({width: 20000, height: 20000, left: -10000});
	$('#soc, #cvb').css({width: ww, height: wh});
	$('#captainb').css('left', ww / 2 - $('#captainb').width() / 2 + 10000);
	$('#newsb').css('left', ww / 2 - $('#newsb').width() / 2 + 10000);
	$('#soc').css('left', ww / 2 - $('#soc').width() / 2 + 10000);
	$('#cvb').css('left', ww / 2 - $('#cvb').width() / 2 + 10000);
}

/**
 * @author Alexander Farkas
 * v. 1.21
 */


(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

