var get_string;
var poll_desc = new Array(10);
poll_desc[1] = 'Отвратительный';
poll_desc[2] = 'Плохой';
poll_desc[3] = 'Плохенький';
poll_desc[4] = 'Слабоват';
poll_desc[5] = 'Нормальный';
poll_desc[6] = 'Неплохой';
poll_desc[7] = 'Хороший';
poll_desc[8] = 'Очень хороший';
poll_desc[9] = 'Отличный';
poll_desc[10] = 'Шедевр киноматографа';

// Для авторизации и регистрации по OpenID
function openidAuth(provider)
{
   switch (provider) {
      case 'yandex':
         jQuery('#openid_url').attr('value','http://openid.yandex.ru');
         jQuery('#openIDform').submit();
         break;
      case 'mailru':
         jQuery('#openid_url').attr('value','http://openid.mail.ru');
         //jQuery('#openIDform').submit();
         jQuery('#mailruOpenIDurl').attr('value','');
         jQuery("#mailruOpenID").toggle();
         break;
      case 'google':
         jQuery('#openid_url').attr('value','https://www.google.com/accounts/o8/id');
         jQuery('#openIDform').submit();
         break;
      case 'rambler':
         jQuery('#openid_url').attr('value','http://www.rambler.ru');
         jQuery('#openIDform').submit();
         break;
      case 'vk':
         jQuery('#openid_url').attr('value','http://vkontakteid.ru');
         jQuery('#openIDform').submit();
         break;
   }
}
function CheckAddPhoto()
{
   val = jQuery("#photo_file").val();
   if(val=='' || val == 'undefined')
   {
      alert('Вы не выбрали файл для загрузки');
      jQuery("#photo_file").focus();
      return false;
   }
}
function AddFoto(cat){
	if(cat == 1) {
		MP.ShowBlock('hideshowlink',1000,1);
		MP.ShowBlock('reduce',1000,1);
		MP.HideBlock('addfotolink',1000,1);
	}
	else {
		MP.HideBlock('hideshowlink',1000,1);
		MP.HideBlock('reduce',1000,1);
		MP.ShowBlock('addfotolink',1000,1);		
	}
	return false;
}
jQuery(document).ready(function()
{
   get_string = MP.ParseQueryString(window.location.search);
   if(get_string['an'] == 'callback')
   {
	   jQuery("#calbackformtable input[type=submit]").bind('click',function()
	   {
		   error = true;
		   jQuery("#calbackformtable input[type=text]").each(function() {
			   if(jQuery(this).val() == '') {
				   if(error) alert('Заполните все поля формы');
				   jQuery(this).focus();
				   error = false;
			   }
		   });
		   jQuery("#calbackformtable textarea").each(function() {
			   if(jQuery(this).val() == '') {
				   if(error) alert('Заполните все поля формы');
				   jQuery(this).focus();
				   error = false;
			   }			   
		   });
		   return error;
	   });
   }
   // Вешаем обработчик на голосование за фильм
   if(get_string['an'] == 'aboutmovie')
   {
	   jQuery('#pollblock li[id^=starpoll]').bind({
		   mouseover: function(){
			   	bal = parseInt(jQuery(this).attr('id').replace('starpoll',''));
			   	if(isNaN(bal)) {return false;}
			   	jQuery('#helppoll').html('Бал '+bal+' - '+poll_desc[bal]);
			   	for(i = 1; i <= 10; i++) {
			   		jQuery('#starpoll'+i).toggleClass('pollactive_cur',i<=bal);
			   		jQuery('#starpoll'+i).toggleClass('pollno_cur',i>bal);
			   	}
		   },
		   click: function(){
			   param = new Object();
			   param['bal'] = parseInt(jQuery(this).attr('id').replace('starpoll',''));
			   param['id'] = get_string['id'];
			   if(isNaN(param['bal'])) {alert('Внутренняя ошибка голосования');return false;}
			   jQuery('#pollblock li[id^=starpoll]').unbind('mouseover click');
			   jQuery('#pollblock li[id^=starpoll]').css('cursor','default');
			   jQuery('#pollblock').unbind('mouseout');
			   return MP.LoadContent('/?an=poll_result','helppoll',param);
		   }
	   });
	   jQuery('#pollblock').bind('mouseout',function(){
		   jQuery('#pollblock li[id^=starpoll]').removeClass('pollactive_cur pollno_cur');
		   jQuery('#helppoll').empty();
	   });
	   jQuery('#add_review').live('click',function(){
		   param = new Object();
		   param['title'] = jQuery.trim(jQuery('#titlereview').val());
		   if(!param['title']) {
			   alert('Необходимо указать название рецензии');
			   jQuery('#titlereview').focus(); return false;
		   }
		   param['review'] = jQuery.trim(jQuery('#reviewtext').val());
		   if(!param['review'] || param['review'].length < 30) {
			   alert('Ваша рецензия слишком маленькая. Опишите подробней Ваше мнение');
			   jQuery('#reviewtext').focus(); return false;
		   }
		   url = '/?an=addreview&id='+get_string['id'];
		   return MP.LoadContent(url,'formreview',param);
	   });
	   count_over_name = jQuery('.over_name').length;
	   jQuery('#showall a').live('click',function(){
		   if(this.id=='small_link') {
			   MP.ShowBlock('.over_name',1500,1);
			   jQuery(this).remove();
			   //jQuery('#showall').html('<a href="#" class="jslink">Скрыть</a>');
		   }
		   else {
			   //MP.HideBlock('.over_name',1500,1);
			   //jQuery('.over_name:first').show().next().show();
			   //jQuery('#showall').html('<a href="#" class="jslink" id="small_link">Показать все</a>');
		   }
		   return false;
	   });
	   if(count_over_name > 0) {
		   jQuery('.over_name:first').show().next().show();
		   if(count_over_name > 2) {
			   link = '<div id="showall" style="margin-top:5px"><a href="#" class="jslink" id="small_link">Показать все</a></div>';
			   jQuery('.over_name:last').after(link);
		   }
	   }
   }
   if(get_string['an'] == 'personsalary')
   {
	   jQuery("#table_salary tr:even td").css("background-color", "#F1F0F6");
   }
   jQuery('#imagesearch').bind('click',function(){
		search_val = jQuery('#searchdiv').val();
		if(search_val.length < 2 || search_val == 'Что ищем?')
		{
			alert('Введите критерий поиска');
			jQuery('#searchdiv').focus();
			jQuery('#searchdiv').val('');
			return false;
		}
		jQuery('#searchdiv').val(search_val);
		jQuery('#SearchFormfields').submit();
		return false;
   });
   jQuery('#searchdiv').bind('keyup',function(){
      if (e.keyCode == 13) {
    	  jQuery('#imagesearch').click();
       }
   });
   jQuery('#searchdiv').bind({
	   click: function() {
	   	if(jQuery('#searchdiv').val()=='Что ищем?') {
	   		jQuery('#searchdiv').val('');
	   	}
	  },
	  blur: function() {
   		if(jQuery('#searchdiv').val()=='') {
   			jQuery('#searchdiv').val('Что ищем?');
   		}
	  }
	});
   if(get_string['an'] == 'aboutmovie' || get_string['an'] == 'aboutperson' || get_string['an'] == 'news')
   {
	   jQuery('#social_but').social_bookmark({
		   def_img_size:'16px',
		   social_bm:true,
		   sites:'facebook,vkontakte,mail,livejournal,twitter,liveinternet,google,yandex,googlebz,friendfeed,bobrdobr,memori,misterwong,ctozakladok,mblogi'
	   });
   }
   if(get_string['an'] == 'aboutmovie' || get_string['an'] == 'aboutperson' || get_string['an'] == 'news')
   {
	   jQuery('#social_com').social_bookmark({
		   def_img_size:'16px',
		   social_li:true,
		   //sites_li:'odnoklassniki,vkontakte,moimir,facebook'
		   sites_li:'odnoklassniki,moimir,facebook'
	   });
   }
   if(get_string['an']=='login') {
	   jQuery("#showOpenIDinput").click(function(){
	     jQuery('#openid_url').attr('value','');
	     jQuery("#openIDinput").toggle();
	   });	   
   }
   if(get_string['an']=='registration') {
	   var timer_auth;
	   jQuery('#loginInput,#emailInput').bind('keyup',function(e){
	 	  check_type = e.target.id == 'loginInput'?'login':'email';
	 	  clearTimeout(timer_auth);
	 	  val_ent = jQuery(this).val();
	 	  if(val_ent.length>2) {
	 		  timer_auth = setTimeout(function(){
	 			  param = check_type+'='+val_ent;
	 			  preloadingtext = 'Идет проверка '+(check_type=='login'?'логина':'ящика');
	 			  mess = '<img src="'+MP.preloadingimageajax+'" border="0" style="vertical-align:middle;" hspace="5"><b>'+preloadingtext+'</b>';
	 			  jQuery('#'+check_type+'info').html(mess);
	 			  MP.LoadContent('/a_php/sessauth/ajax/checkuserfield.php','dsds',param,'json',function(msg){
	 				  if (msg.error!='') {
	                       jQuery('#'+check_type+'Info').html('Ошибка: '+msg.error);
	                    }
	                    else {   
	     	           	   mess = '<img src="/a_php/common/icon/'+(msg.result==1?'ok.gif':'error.png')+'" border="0" hspace="5" style="vertical-align:middle;">';
	     	           	   mess += msg.error!=''?'Ошибка: '+msg.error:'';
	     	        	   mess += check_type=='login'?'Логин ':'E-Mail ';
	     	        	   mess += msg.result==1?'свободен':'занят';
	     	        	   jQuery('#'+check_type+'info').html(mess);
	                    }
	 			  });
	 		  },500);
	 	  }
	   })	   
   }
});
