var interval = null;
var social = [
	{name: 'В Контакте', url: 'http://vkontakte.ru/share.php?url='+encodeURIComponent(location.href), icon: '/public/images/social/16x16/vk.png'},
	{name: 'Facebook', url:'http://www.facebook.com/sharer.php?u='+encodeURIComponent(location.href), icon:'/public/images/social/16x16/fb.png'},
	{name: 'Twitter', url:'http://twitter.com/home?status='+encodeURIComponent(document.title)+': '+encodeURIComponent(location.href), icon:'/public/images/social/16x16/twitter.png'},
	{name: 'Одноклассники', url: 'http://www.odnoklassniki.ru/dk?st.cmd=addShare&st.s=2&st.noresize=on&st._surl='+encodeURIComponent(location.href), icon: '/public/images/social/16x16/odnoklassniki.png'},
	{name: 'Мой Мир', url: 'http://connect.mail.ru/share?share_url='+encodeURIComponent(location.href), icon: '/public/images/social/16x16/mymir.png'},
	{name: 'LiveJournal', url:'http://www.livejournal.com/update.bml?subject='+encodeURIComponent(location.href)+'&bm_notice='+encodeURIComponent('свадьба в казани'), icon:'/public/images/social/16x16/livejournal.png'},
	{name: 'Memori', url:'http://www.memori.ru/link/?sm=1&u_data[url]='+encodeURIComponent(location.href)+'&u_data[name]='+encodeURIComponent(document.title)+'&u_data[descr]='+encodeURIComponent(document.title)+'&u_data='+encodeURIComponent('свадьба в казани'), icon:'/public/images/social/16x16/memori.png'},
	{name: 'БобрДобр', url: 'http://www.bobrdobr.ru/addext.html?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&desc='+encodeURIComponent(document.title)+'&tags='+encodeURIComponent('свадьба в казани'), icon: '/public/images/social/16x16/bobrdobr.png'},
	{name: 'Закладки Google', url: 'http://www.google.com/bookmarks/mark?op=add&hl=ru&bkmk='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&labels='+encodeURIComponent('свадьба в казани')+'&annotation='+encodeURIComponent(document.title), icon: '/public/images/social/16x16/google.png'},
	{name: 'Яндекс.Закладки', url: 'http://zakladki.yandex.ru/newlink.xml?url='+encodeURIComponent(location.href)+'&name='+encodeURIComponent(document.title)+'&tags='+encodeURIComponent('свадьба в казани')+'&descr='+encodeURIComponent(document.title)+'&title='+encodeURIComponent(document.title), icon: '/public/images/social/16x16/yandex.png'},
	{name: 'Mister Wong', url: 'http://www.mister-wong.com/index.php?action=addurl&bm_url='+encodeURIComponent(location.href)+'&bm_notice=', icon:'/public/images/social/16x16/misterwong.png'},
	{name: 'Delicoius', url:'http://del.icio.us/post?v=2&url='+encodeURIComponent(location.href)+'&notes='+encodeURIComponent(document.title)+'&tags='+encodeURIComponent('свадьба в казани')+'&title='+encodeURIComponent(document.title), icon:'/public/images/social/16x16/delicious.png'}	
];

$.fn.slide = function (time, direction, callback) {
	var vis = ($(this).is(':visible') && $(this).width() > 0);
	return $(this).animate(direction == 'right' ? {
		marginLeft: (vis ? '+=': '-=') + $(this).width(),
		width: vis ? 'hide' : 'show'
	} : {
		width: vis ? 'hide' : 'show'
	},
	time, callback);
};

$(document).ready(function(){
	//ie fix
	if($('.foto_ned').length>0){
		//alert($('.foto_ned .f_ned_box').width()-$('.foto_ned img').width());
		var w=$('.foto_ned img').width();
		if(w==0)w=$('.foto_ned img').size().width;
		w+=82;
		$('.foto_ned .f_ned_box').css('width',w);
		$('.foto_ned .f_ned_top').css('width',w);
		$('.foto_ned .f_ned_bottom').css('width',w);
	}
	$(".showLogin").click(function(){					
		var item = $("#login");
		if(item.length > 0){
			item.show("slow");
		} else {
			$.ajax({
				url: '/index/login/',
				async: false,
				success: function(data){
					$('body').prepend(data).find('#login').show("slow");	
				}
			});	
		}
		return false;
	});			
	$(".question_to_autor a").click(function(){
		var item = $("#question_to_autor");
		if(item.length > 0) {
			item.show("slow");
		} else {
			$.ajax({
				url: $(this)[0].href,
				async: false,
				success: function(data){
					$('body').prepend(data).find('#question_to_autor').show("slow");	
				}
			});	
		}
		return false;
	});			
	/*$(".showReg").click(function () {					
		$("#authReg").show("slow");
		$("#login").hide("slow"); 
		return false;
	});
	$(".hideReg").click(function () {					
		$("#authReg").hide("slow");
		return false;
	});*/

	//$(".min_gallery").hide("fast");
	$("#selectPhotoFromAlbum").each(function(i, item){
		$(item).click(function () {					
			$(".min_gallery").show("slow");		
			return false;
		});
	});
	$("#submitAddForm").each(function(i, item){
		$(item).click(function(){
			var cnt;
			cnt=0;
			$("input").css('border-color','#999999');
			$(".min_gallery").css('border','none');
			if($("#FIO").val()==""){			
				$("#FIO").css('border-color','red');
				cnt=cnt+1;
			}
			if($("#email").val()==""){			
				$("#email").css('border-color','red');
				cnt=cnt+1;
			}
			if($("#city").val()==""){			
				$("#city").css('border-color','red');
				cnt=cnt+1;
			}
			if($("#selectedUserPhotoId").val()==""){
				$(".min_gallery").css('border','2px solid');
				$(".min_gallery").css('border-color','red');
				cnt=cnt+1;
			}
			if(cnt==0){
				document.sendPhoto.submit();
			}else{
				alert('Заполните обязательные поля');
			}
		});
	});	
	$(".showContents").each(function(i, item){
		$(item).click(function () {
			var id = this.id.replace('showContents', '');
			$("#contenta"+id).hide("slow");   
			$("#contents"+id).show("slow");
			return false;
		});
	});
	$(".showContenta").each(function(i, item){
		$(item).click(function () {
			var id = this.id.replace('showContenta', '');
			$("#contents"+id).hide("slow");   
			$("#contenta"+id).show("slow");
			return false;
		});
	});
	$('.social_button_vk').each(function(i, item){
		var html = '<a href="javascript:void(0);" title="'+$(item).attr('title')+'" onclick="window.open(\''+social[0]['url']+'\');$(\'#social_popup\').hide();return false;">'+$(item).html()+'</a>';
		$(item).html(html);
	});
	$('.social_button_facebook').each(function(i, item){
		var html = '<a href="javascript:void(0);" title="'+$(item).attr('title')+'" onclick="window.open(\''+social[1]['url']+'\');$(\'#social_popup\').hide();return false;">'+$(item).html()+'</a>';
		$(item).html(html);
	});
	$(".social_button_compact").each(function(i, item){
		var html = '<div class="close"></div><b>в закладки</b><br />';
		for(var i in social){
			html += '<div class="item_s"><img src="'+social[i]['icon']+'" alt="'+social[i]['name']+'" width="16" height="16" /><a href="javascript:void(0);" onclick="window.open(\''+social[i]['url']+'\');$(\'#social_popup\').hide();return false;">'+social[i]['name']+'</a></div>';
		}
		$('#social_popup').attr('left', $(item).attr('left'));
		$('#social_popup').html(html);
		$('#social_popup .close').click(function(){
			$('#social_popup').hide();
		});
		$('#social_popup').mouseout(function(e){
			if (!e) e = window.event;
			var toElement=e.toElement || e.relatedTarget;
			if(toElement === this
				|| $(toElement).parent()[0] === this
				|| $(toElement).parent().parent()[0] === this 
				|| toElement === $(".social_button_compact")[0]){
				return false;	
			}
			$(this).hide(); 
			return false;
		});
		$(item).click(function () {
			$('#social_popup').show();
			
			return false;
		});
		$(item).mouseover(function () {
			$('#social_popup').show();
			
			return false;
		});
		$(item).mouseout(function (e) {
			if (!e) var e = window.event;
			var toElement=e.toElement || e.relatedTarget;
			var popup = $('#social_popup');			
			if(toElement === popup[0]
				|| $(toElement).parent()[0] === popup[0]
				|| $(toElement).parent().parent()[0] === popup[0]){
				return false;		
			}
			popup.hide();
			return false;	
		});
	});
	if($('#vk_like').length > 0){
		VK.init({apiId: 2393708, onlyWidgets: true});
		VK.Widgets.Like("vk_like", {type: "button"});
	}	
	$('.anekdot .expand').each(function(i,item){
		$(item).click(function(){
			if($('.anekdot .text').length > 0){
				if($('.anekdot .text').css('display') == 'none'){
					$('.anekdot .text').show();
					$('.anekdot .more').hide();	
					$('.anekdot .teaser').show();
				}else{
					$('.anekdot .text').hide();
					$('.anekdot .more').show();	
					$('.anekdot .teaser').hide();
				}
			} else {
				$.ajax({
			 		url:item.href,
					success:function(data){
						$(item).parent().before(data);
						$('.anekdot .more').hide();
						$('.anekdot .teaser').show();
						$('.anekdot .collapse').click(function(){
							$('.anekdot .text').hide();
							$('.anekdot .more').show();	
							$('.anekdot .teaser').hide();
							return false;
						});
					}
				});
			}
			return false;
		});
	});	
	$('.tip').each(function(i,item){
		$(item).click(function(){
			var n = $(item).next();
			if(n.length > 0){
				n.show();
			}else{
				$.ajax({url:item.href,success:function(data){
						$(item).after(data).next().find('.close').click(function(){$(this).parent().hide();});
					}
				});
			}
			return false;
		});
	});
		
	$('.a_navigator').each(function(i,item){
		for (var i=2; i<=10; i++){
			$('#announce'+(i-1)).after('<div id="announce'+i+'" class="a_announce" style="display:none;"></div>');
		}
		
		$('#announce1').each(function(i,item){
			$(item).mouseover(function(){
				if(interval!=null){
					clearInterval(interval);
					interval = null;
				}
			}).mouseout(function(){
				if(interval==null){
					interval = window.setInterval(slideArticlesAnnounces, 5000);
				}
			});
		});		
		interval = window.setInterval(slideArticlesAnnounces, 5000);
	});
	$('.a_navigator span').each(function(i,item){
		$(item).click(function(){
			var a = $('#announce'+item.id.substr(1));
			var j = parseInt(item.id.substr(1));
			var i = parseInt(($('.a_navigator span.active')[0]).id.substr(1));
			if(a.length < 1){
				return;	
			}
			
			if(interval!=null){
				clearInterval(interval);
				interval = null;
			}				
			if(a.html().length>0){
				$('.a_navigator span').removeClass('active');
				a.show();
				if(j>i){
					$('.a_window').stop().animate({
						left: '-=100%'
		   			}, 1000, function() {
						$('.a_window').css('left','0');
						$('#announce'+i).hide();
						$('#a'+j).addClass('active');
						if(interval!=null){
							clearInterval(interval);
							interval = null;
						}
						interval = window.setInterval(slideArticlesAnnounces, 5000);
					});
				}else{
					$('.a_window').css('left','-100%');
					$('.a_window').stop().animate({
						left: '+=100%'
		   			}, 1000, function() {
						$('.a_window').css('left','0');
						$('#announce'+i).hide();
						$('#a'+j).addClass('active');
						if(interval!=null){
							clearInterval(interval);
							interval = null;
						}
						interval = window.setInterval(slideArticlesAnnounces, 5000);
					});
				}
			}else{				
				$.ajax({
					url: '/index/showarticle/',
					type: 'post',
					data: 'item='+item.id.substr(1),
					success: function(data) {
						$('.a_navigator span').removeClass('active');
						a.html(data);
						a.show();
						a.mouseover(function(){
							if(interval!=null){
								clearInterval(interval);
								interval = null;
							}
						}).mouseout(function(){
							if(interval==null){
								interval = window.setInterval(slideArticlesAnnounces, 5000);
							}
						});
						if(j>i){
							$('.a_window').stop().animate({
								left: '-=100%'
      						}, 1000, function() {
								$('.a_window').css('left','0');
								$('#announce'+i).hide();
								$('#a'+j).addClass('active');
								if(interval!=null){
									clearInterval(interval);
									interval = null;
								}
								interval = window.setInterval(slideArticlesAnnounces, 5000);
							});
						}else{
							$('.a_window').css('left','-100%');
							$('.a_window').stop().animate({
								left: '+=100%'
      						}, 1000, function() {
								$('.a_window').css('left','0');
								$('#announce'+i).hide();
								$('#a'+j).addClass('active');
								if(interval!=null){
									clearInterval(interval);
									interval = null;
								}
								interval = window.setInterval(slideArticlesAnnounces, 5000);
							});	
						}
					}						
				});	
			}
			return false;
		}).mouseover(function(){
			if($(this).hasClass('active')){
				if(interval!=null){
					clearInterval(interval);
					interval = null;
				}
			}
		}).mouseout(function(){
			if($(this).hasClass('active')){
				if(interval==null){
					interval = window.setInterval(slideArticlesAnnounces, 5000);
				}
			}
		});
	});
	$('.blogGroupsWidget .bloger_avatar').each(function(i,item){
		$('#morebloger').mouseout(function(e){
			if (!e) var e = window.event;
			var toElement = e.relatedTarget || e.toElement;
			if(this != toElement && $(toElement).parents('#morebloger').length < 1){
				$('#morebloger').hide().html('');
			}
		});
		$(item).first().mouseout(function(e){
			if (!e) var e = window.event;
			var toElement = e.relatedTarget || e.toElement;
			if($('#morebloger')[0] != toElement && $(toElement).parents('#morebloger').length < 1){
				$('#morebloger').hide().html('');
			}
		});
		$(item).mouseover(function(){
			if($('#morebloger').css('display')!='none'){
				return false;	
			}
			$.ajax({
				url:item.href.replace('blogers', 'blogerpost'),
				success:function(html){
					if(html.length>0){
						var position = $(item).position();
						$('#morebloger').css('left',position.left+'px').css('top',(position.top+$(item).first().height()-2)+'px');
						$('#morebloger').html(html).show();
					}
				}
			});	
			return false;
		});
	});

	$('.photo .gal_s,.photo .gal_s_2').click(function(){
		$.ajax({
			url:this.href,
			//dataType:'json',
			success:function(data){
				$('#bigPhoto').html(data.photo);
				$('#FIO').html(data.FIO);
				if(data.prev){
					$('.photo .gal_s').attr('href',data.prev).show();
				}else{
					$('.photo .gal_s').attr('href','').hide();
				}
				if(data.next){
					$('.photo .gal_s_2').attr('href',data.next).show();
				}else{
					$('.photo .gal_s_2').attr('href','').hide();
				}
				if(data.position && $('#position').length){
					$('#position').html(data.position);
				}
				if(data.votes && $('#votes').length){
					$('#votes').html(data.votes);
				}
			}
		});	
		return false;
	});
});

function slideArticlesAnnounces(){
	var i =  parseInt($('.a_navigator span.active').attr('id').substr(1));
	var j = i<10 ? i+1 : 1;
	var a = $('#announce'+j);
	
	if(a.length < 1){
		return;	
	}
		
	if(a.html().length>0){
		$('.a_navigator span').removeClass('active');
		a.show();
		if(j>1){
			$('.a_window').stop().animate({
				left: '-=100%'
      		}, 1000, function() {
				$('.a_window').css('left','0');
				$('#announce'+i).hide();
				$('#a'+j).addClass('active');
				if(interval!=null){
					clearInterval(interval);
					interval = null;
				}
				interval = window.setInterval(slideArticlesAnnounces, 5000);
			});
		}else{
			$('.a_window').css('left','-100%');
			$('.a_window').stop().animate({
				left: '+=100%'
      		}, 1000, function() {
				$('.a_window').css('left','0');
				$('#announce'+i).hide();
				$('#a'+j).addClass('active');
				if(interval!=null){
					clearInterval(interval);
					interval = null;
				}
				interval = window.setInterval(slideArticlesAnnounces, 5000);
			});	
		}
	}else{
		clearInterval(interval);
		interval = null;
		$.ajax({
			url: '/index/showarticle/',
			type: 'post',
			data: 'item='+j,
			success: function(data) {
				$('.a_navigator span').removeClass('active');
				a.html(data);
				a.mouseover(function(){
					if(interval!=null){
						clearInterval(interval);
						interval = null;
					}
				}).mouseout(function(){
					if(interval==null){
						interval = window.setInterval(slideArticlesAnnounces, 5000);
					}
				});
				a.show();
				$('.a_window').stop().animate({
					left: '-=100%'
      			}, 1000, function() {
					$('.a_window').css('left','0');
					$('#announce'+i).hide();
					$('#a'+j).addClass('active');
					if(interval!=null){
						clearInterval(interval);
						interval = null;
					}
					interval = window.setInterval(slideArticlesAnnounces, 5000);
				});				
			}						
		});	
	}
}

function add_favorite(a) { 
	title=document.title; 
	url=document.location; 
	try { 
		// Internet Explorer 
		window.external.AddFavorite(url, title); 
	} 
	catch (e) { 
		try { 
			// Mozilla 
			window.sidebar.addPanel(title, url, ""); 
		} 
		catch (e) { 
			// Opera 
			if (typeof(opera)=="object") { 
				a.rel="sidebar"; 
				a.title=title; 
				a.url=url; 
				return true; 
			} 
			else { 
				// Unknown 
				alert('Нажмите Ctrl-D чтобы добавить страницу в закладки'); 
			} 
		} 
	} 
	return false; 
}

function showHide(numBlock)
{
	if (document.getElementById('st1_'+numBlock).style.display == "none")
	{
		document.getElementById('st1_'+numBlock).style.display = "block";				
		document.getElementById('st2_'+numBlock).style.display = "none";
		document.getElementById('st3_'+numBlock).style.display = "block";
		document.getElementById('st4_'+numBlock).style.display = "block";
		document.getElementById('st5_'+numBlock).style.display = "none";
		document.getElementById('st6_'+numBlock).style.display = "";
	}else{
		document.getElementById('st1_'+numBlock).style.display = "none";
		document.getElementById('st2_'+numBlock).style.display = "inline";
		document.getElementById('st3_'+numBlock).style.display = "none";
		document.getElementById('st4_'+numBlock).style.display = "none";
		document.getElementById('st5_'+numBlock).style.display = "";
		document.getElementById('st6_'+numBlock).style.display = "none";
	}
}

function showHide2(numBlock)
{
	if (document.getElementById('st1_'+numBlock).style.display == "none")
	{
		document.getElementById('st1_'+numBlock).style.display = "block";
		document.getElementById('st2_'+numBlock).style.display = "block";
		document.getElementById('st3_'+numBlock).style.display = "block";
		document.getElementById('st4_'+numBlock).style.display = "block";
		document.getElementById('st5_'+numBlock).style.display = "none";
		document.getElementById('st6_'+numBlock).style.display = "";
	}else{
		document.getElementById('st1_'+numBlock).style.display = "none";
		document.getElementById('st2_'+numBlock).style.display = "none";
		document.getElementById('st3_'+numBlock).style.display = "none";
		document.getElementById('st4_'+numBlock).style.display = "none";
		document.getElementById('st5_'+numBlock).style.display = "";
		document.getElementById('st6_'+numBlock).style.display = "none";
	}
}

function addCarousel(id)
{
	$('.anyClass'+id).jCarouselLite({
		btnNext: '#gar'+id,
		btnPrev: '#gal'+id,
		speed: 500,
		circular: false,
		easing: 'easeinout'
	});	
}

function strToInt(str)
{
	var num = 0;
	for(var i = 0; i < str.length; i++){
		num += str.charCodeAt(i);
	}
	return num;
}

function addPhoto(id, url){
	$("#cli_"+id).click(function(){
		var cl=$("#cli_"+id).attr("class");
		//alert(cl);
		$('tr#bigPhotoAll').load(url);
	});	
}

$(function() {
	$(".anyClass").each(function(i, item){
		$(item).jCarouselLite({
			btnNext: ".gal_s_2",
			btnPrev: ".gal_s",
			speed: 500,
			easing: "easeinout"
		});
	});       
	
//  $(".postForm").each(function(i, item){
//		$(item).click(function(){
//		var cl=$(".nextBigPhoto").attr("href");
//      alert("post");
//      $('div.otziv_all').load('/index/addcomment/?ajax=1');
//      return false;
//  });

	$(".datepicker").each(function(i, item){
		$(item).datepicker();
	});
	//$("#dateimg2").datepicker();

	$("#dateimg2").each(function(i, item){
		$(item).click(function(){
			//alert('click');
			$("#dateinp2").datepicker('show');			
		});
	});
	
	$("#dateimg1").each(function(i, item){
		$(item).click(function(){
			//alert('click');
			$("#dateinp1").datepicker('show');			
		});
	});

    $(".newsCarausel").each(function(i, item){
		$(item).jCarouselLite({
    	    btnNext: ".next",
    	    btnPrev: ".prev",
    	    vertical: "true",
    	    speed: 500,
			easing: "easeinout",
			circular: false
    	});
	});

    $("#nextBigPhoto").each(function(i, item){
		$(item).click(function(){
    		var cl=$("#nextBigPhoto").attr("href");
    		cl=cl.replace("album","ajaxget");
        	//alert(cl);
        	$('tr#bigPhotoAll').load(cl);
        	return false;
    	});
	});
	
    $("#prevBigPhoto").each(function(i, item){
		$(item).click(function(){
    		var cl=$("#prevBigPhoto").attr("href");
    		cl=cl.replace("album","ajaxget");
        	//alert(cl);
        	$('tr#bigPhotoAll').load(cl);
        	return false;
		});
    });
	
	 $(".nonCircular .photosCarausel").each(function(i, item){
		$(item).jCarouselLite({
        	btnNext: ".nonCircular .next",
        	btnPrev: ".nonCircular .prev",        
        	speed: 500,
			easing: "easeinout",
			circular: false,
			visible: 5
    	});
	});
});
