if( window.jQuery){
	window.IE = jQuery.browser.msie;
	window.is_ie6 = (function() { var b = jQuery.browser; return b.msie && (b.version == '6.0'); }());
}

function getProfileName(){
 var _profile;
 try { _profile = jQuery('#profileName').val(); } catch(e) {  }
 if( _profile == null ){
	 _profile = "KOTEK";
 }
 return _profile;
}

var profil_birthday = {
 daySelect: {},
 monthSelect: {},
 yearSelect: {},
 init: function(){

  var me = this;

  me.daySelect = jQuery('#i_birthday_day');
  me.monthSelect = jQuery('#i_birthday_month');
  me.yearSelect = jQuery('#i_birthday_year');

  if(me.daySelect.length <= 0 || me.monthSelect.length <= 0 || me.yearSelect.length <= 0) { return; }

  me.monthSelect.change( me.seNoOfDays );
  me.yearSelect.change( me.seNoOfDays );

 },
 seNoOfDays: function(){
  var IE = jQuery.browser.msie;
  var me = profil_birthday;

  var year = parseInt(me.yearSelect.val());
  var month = parseInt(me.monthSelect.val());
  var day = parseInt(me.daySelect.val());


  switch(month){
   case 2:
	if(year%4) {
	 me.daySelect.find("option[value='29'], option[value='30'], option[value='31']").attr('disabled','disabled');
	 me.daySelect.find("option[value='29'], option[value='30'], option[value='31']").attr('selected','');
	 if(IE){
	  me.daySelect.find('option').remove();
	  for(var i = 1; i < 29; i++){
	   me.daySelect.append('<option value="'+i+'">'+i+'</option>');
	   if(day < 29) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');
	  }
	 }
	} else {
	 me.daySelect.find("option[value='29']").attr('disabled','');
	 me.daySelect.find("option[value='30'], option[value='31']").attr('disabled','disabled');
	 me.daySelect.find("option[value='30'], option[value='31']").attr('selected','');
	 if(IE){
	  me.daySelect.find('option').remove();
	  for(var i = 1; i < 30; i++){
	   me.daySelect.append('<option value="'+i+'">'+i+'</option>');
	   if(day < 30) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');
	  }
	 }
	}
	break;
   case 4:
   case 6:
   case 9:
   case 11:
	me.daySelect.find("option[value='29'], option[value='30']").attr('disabled','');
	me.daySelect.find("option[value='31']").attr('disabled','disabled');
	me.daySelect.find("option[value='31']").attr('selected','');
	if(IE){
	 me.daySelect.find('option').remove();
	 for(var i = 1; i < 31; i++){
	  me.daySelect.append('<option value="'+i+'">'+i+'</option>');
	  if(day < 31) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');
	 }
	}
	break;
   default:
	me.daySelect.find("option[value='29'], option[value='30'], option[value='31']").attr('disabled','');
	if(IE){
	 me.daySelect.find('option').remove();
	 for(var i = 1; i < 32; i++){
	  me.daySelect.append('<option value="'+i+'">'+i+'</option>');
	  if(day < 32) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');
	 }
	}
	break;
  }
 }
}

var profil_switchSection = {
 root: {},
 init: function(){

  var me = this;

  me.root = jQuery('.switchSection');

  me.root.find('.tabs a').click( me.switchTab );

 },
 switchTab: function(e){

  if((jQuery(e.target).parent()).hasClass('active')) { return; }

  var me = profil_switchSection;
  var clickedElem = jQuery(e.target);

  me.root.find('.active').removeClass('active');
  clickedElem.parent().addClass('active');
  me.root.find('#'+clickedElem.attr('rel')).addClass('active');
 }
}

var profil_photoSection = {
 root: {},
 init: function(){

  var me = this;

  me.root = jQuery('.photoSection');

  me.root.find('.zwin').click( me.closeForm );
  me.root.find('.iCancel').click( me.closeForm );
  me.root.find('.rozwin').click( me.openForm );
  me.root.find('.edytuj').click( me.openForm );
  me.root.find('.serwisy img').click( me.changeSerwis );

 },
 openForm: function(){

  var me = profil_photoSection;

  me.root.find('.rozwin').addClass('hidden');
  me.root.find('.zwin').removeClass('hidden');
  me.root.find('.formularz').removeClass('hidden');
  me.root.find('.middle').addClass('hidden');
 },
 closeForm: function(){

  var me = profil_photoSection;

  me.root.find('.zwin').addClass('hidden');
  me.root.find('.rozwin').removeClass('hidden');
  me.root.find('.formularz').addClass('hidden');
  me.root.find('.middle').removeClass('hidden');
 },
 changeSerwis: function(e){

  if((jQuery(e.target).parent()).hasClass('active')) { return; }

  var me = profil_photoSection;
  var serwisName = jQuery(e.target).attr('alt');

  me.root.find('.serwisy li').removeClass('active');
  jQuery(e.target).parent().addClass('active');
  me.root.find("input[name=photoSerwis]").val(serwisName);
  me.root.find("label[for=i_photoSerwis_login] span").html(serwisName);
 }
}

var profil_blogSection = {
 root: {},
 init: function(){

  var me = this;

  me.root = jQuery('.blogSection');

  me.root.find('.zwin').click( me.closeForm );
  me.root.find('.iCancel').click( me.closeForm );
  me.root.find('.rozwin').click( me.openForm );
  me.root.find('.edytuj').click( me.openForm );

 },
 openForm: function(){

  var me = profil_blogSection;

  me.root.find('.rozwin').addClass('hidden');
  me.root.find('.zwin').removeClass('hidden');
  me.root.find('.formularz').removeClass('hidden');
  me.root.find('.middle').addClass('hidden');
 },
 closeForm: function(){

  var me = profil_blogSection;

  me.root.find('.zwin').addClass('hidden');
  me.root.find('.rozwin').removeClass('hidden');
  me.root.find('.formularz').addClass('hidden');
  me.root.find('.middle').removeClass('hidden');
 }
}

var profil_photoArrows = {
 init: function(){
  var _photoCtr = jQuery('#photoCtr');
  if(_photoCtr.length < 1) return;

  var _prevPhotoLink = _photoCtr.find('.head a[rel=prev]');
  var _nextPhotoLink = _photoCtr.find('.head a[rel=next]');

  var _photoPhoto = _photoCtr.find('#photoPhoto');
  var _photoPhotoImg = _photoCtr.find('#photoPhoto img');

  if(_photoPhotoImg.length < 0) return;

  var _pOvHeight = _photoPhotoImg.height();
  var _pOvWidth = _photoPhotoImg.width();

  //create & append overaly
  var _html = '<div id="photoOverlay" style="display: none;">';
  if(_prevPhotoLink.length > 0){
   _html += '<a class="ovPrev" href="'+_prevPhotoLink.attr("href")+'" title="Poprzednie zdjêcie"><span class="ico"></span></a>';
  }
  if(_nextPhotoLink.length > 0){
   _html += '<a class="ovNext" href="'+_nextPhotoLink.attr("href")+'" title="Nastêpne zdjêcie"><span class="ico"></span></a>';
  }
  _html += '</div>';

  _photoPhoto.append(_html);

  //add styles to overaly and show it
  var _photoOverlay = _photoCtr.find('#photoOverlay');
  _photoOverlay.css({width: _pOvWidth + "px", height: _pOvHeight + "px"})
  _photoOverlay.show();

  var _prevbtn = _photoCtr.find('.ovPrev span');
  _prevbtn.css({height: _pOvHeight + "px"});

 }
}

var slidePhotos = {
 init: function(){
  me = slidePhotos;
  jQuery(".slidePhotoBox").each(function(){
	me.run(jQuery(this));
  });
 },
 readParams: function(obj){
  //read params from hidden div's and assign them to sliders
  obj.find('.params div').each(function(){
	obj.attr(jQuery(this).attr('className'),jQuery(this).html());
  });
 },
 updateParams: function(obj){
  //write new params to hidden div
  obj.find('.params div').each(function(){
	jQuery(this).html(obj.attr(jQuery(this).attr('className')));
  });
 },
 run: function(obj){
  me = slidePhotos;
  me.readParams(obj);
  me.activateArrows(obj,true);
 },
 activateArrows: function(obj,doinit){
  me = slidePhotos;
  //activate arrows in slider dependenig on site number and next siteAvaiable
  var leftArrow = obj.find('.pr').eq(0);
  var rightArrow = obj.find('.nx').eq(0);

  if((parseInt(obj.attr('gallery_start'),10)) > 0) {
   leftArrow.addClass('sliderActive');
  }else{
   leftArrow.removeClass('sliderActive');
  }
  if((obj.attr('gallery_next_page')) == 'true') {
   rightArrow.addClass('sliderActive');
  }else{
   rightArrow.removeClass('sliderActive');
  }
  if(doinit == true){
   leftArrow.click(function(e){ slidePhotos.moveLeft(e) });
   rightArrow.click(function(e){ slidePhotos.moveRight(e) });
  }

 },
 moveLeft: function(e){
  if(jQuery(e.target).hasClass('sliderActive')){
   slidePhotos.askForImages(jQuery(e.target).parents(".slidePhotoBox"),-1);
  }
 },
 moveRight: function(e){
  if(jQuery(e.target).hasClass('sliderActive')){
   slidePhotos.askForImages(jQuery(e.target).parents(".slidePhotoBox"),1);
  }
 },

 // deprecated, use getImages
 askForImages: function(obj,dir){
	slidePhotos.getImages(obj,dir,getProfileName());
 },

 //ajax
 getImages: function(obj,dir,profile){
  var imgCtrs = obj.find('li');
  if( dir>0 ){
	  imgCtrs.eq(2).html('');
  }else{
	  imgCtrs.eq(0).html('');
  }

  similarPhotosDwr.getSimilarPhotos(profile, obj.attr('gallery_key'), parseInt(obj.attr('gallery_start'),10)+3*dir, obj.attr('gallery_photo_url_base'), {
		callback:function(data) {
	  imgCtrs.each(function(i){
		   if(data.images[i]){
			var href = data.images[i].href;
			href = href.replace(/null\,/g,',');
			jQuery(this).html('<a title="'+data.images[i].title+'" href="'+href+'">'+'<img alt="'+data.images[i].alt+'" src="'+data.images[i].imgsrc+'"/></a>');
		   }else{
		   jQuery(this).html('<img alt="last" src="http://bi.gazeta.pl/i/profile/slide-last-photo.gif"/>');
		   }
		  });
		  obj.attr('gallery_start',data.start);
		  obj.attr('gallery_next_page', data.hasNext);
		  slidePhotos.updateParams(obj);
		  slidePhotos.activateArrows(obj,false);
	},
	errorHandler:function(errorString, exception) {
	}
  });
 }

}

var editTitle = {
 init: function(){
  jQuery('#photoTitle').click(function(){
	jQuery('#photoTitle_edit .iTxt').val(jQuery('#photoTitle_txt').text());
	jQuery(this).addClass('hidden');
	jQuery('#photoTitle_edit').removeClass('hidden');
  });
  jQuery('#photoTitle_edit .iSave').click(function(e){
	e.preventDefault();
	var _photoId = jQuery('#photoPhoto img').attr("id");
	var _newTitle = jQuery('#photoTitle_edit .iTxt').val();
	var _profile = getProfileName();
	photoEditDwr.saveTitle(_profile, _photoId, _newTitle, {
		callback:function(data) {
			jQuery('#photoTitle_edit').addClass('hidden');
			jQuery('#photoTitle_txt').html(data.title);
			jQuery('#photoTitle').removeClass('hidden');
		},
		errorHandler:function(errorString, exception) {
			jQuery('#photoTitle_edit').addClass('hidden');
			jQuery('#photoTitle').removeClass('hidden');
		}});
  });
 }
}

var editTags = {
 init: function(){
  jQuery('#photoTags_view').click(function(){
	var _tags = "";
	jQuery('#photoTags_view').find('li a').each(function(i){
	 if(i==0){
	  _tags += jQuery(this).text();
	 }else{
	  _tags += ", "+jQuery(this).text();
	 }
	});
	 jQuery('#photoTags_edit .iTxt').val(_tags);
	jQuery('#photoTags_view').addClass('hidden');
	jQuery('#photoTags_edit').removeClass('hidden');
  });
  jQuery('#photoTags_edit .iSave').click(function(e){
	e.preventDefault();
	var _photoId = jQuery('#photoPhoto img').attr("id");
	var _newTags = jQuery('#photoTags_edit .iTxt').val();
	var _tagUrlBase = jQuery('#photoTags_edit .iTagUrlBase').val();
	var _profile = getProfileName();
	photoEditDwr.saveTags(_profile, _photoId, _newTags, _tagUrlBase, {
		callback:function(data) {
			 var _html = "";
			 if(data.tags.length > 0){
			   for(var i = 0; i < data.tags.length; i++){
				if(i==0){
				 _html += '<li class="first"><a href="'+data.tags[i].href+'">'+data.tags[i].name+'</a></li>';
				}else{
				  _html += '<li><a href="'+data.tags[i].href+'">'+data.tags[i].name+'</a></li>';
				}
			   }
			 }
			 jQuery('#photoTags ul').html(_html);
			 jQuery('#photoTags_edit').addClass('hidden');
			 jQuery('#photoTags_view').removeClass('hidden');
		},
		errorHandler:function(errorString, exception) {
			  jQuery('#photoTags_edit').addClass('hidden');
			 jQuery('#photoTags_view').removeClass('hidden');
		}});
  });
  jQuery('#photoTags_edit .iTxt').keyup(function(e) {
	  cleanTags(this);
  });
 }
}

function cleanTags(formField) {
	  var val = formField.value;
	  var cleanVal = "";
	  for(var i = 0; i < val.length; i++) {
		  if (isCharAllowedInTag(val.charAt(i))) {
			  cleanVal = cleanVal + val.charAt(i);
		  }
	  }
	  if( val != cleanVal ){
	formField.value = cleanVal;
	  }
}

function isCharAllowedInTag(ch) {
	return ("a±bcædeêfghijkl³mnñoópqrs¶tuvwxyz¼¿A¡BCÆDEÊFGHIJKL£MNÑOÓPQRS¦TUVWXYZ¬¯0123456789-, ").indexOf(ch) > -1;
}

var editDesc = {
 init: function(){
  jQuery('#photoDesc').click(function(){
	  jQuery('#photoDesc_edit .iTa').val(jQuery('#photoDesc_txt').text());
	jQuery(this).addClass('hidden');
	jQuery('#photoDesc_edit').removeClass('hidden');
  });
  jQuery('#photoDesc_edit .iSave').click(function(e){
	e.preventDefault();
	var _photoId = jQuery('#photoPhoto img').attr("id");
	var _newDesc = jQuery('#photoDesc_edit .iTa').val();
	var _profile = getProfileName();
	photoEditDwr.saveDescription(_profile, _photoId, _newDesc, {
		callback:function(data) {
			 jQuery('#photoDesc_edit').addClass('hidden');
			 jQuery('#photoDesc_txt').html(data.description);
			 jQuery('#photoDesc').removeClass('hidden');
		},
		errorHandler:function(errorString, exception) {
			  jQuery('#photoDesc_edit').addClass('hidden');
			 jQuery('#photoDesc').removeClass('hidden');
		}});
  });
 }
}

if( window.jQuery){
	jQuery(document).ready(function(){profileScInit();});
}
var gazeta_pl = gazeta_pl || {};
gazeta_pl.functionQueue = gazeta_pl.functionQueue || [];
gazeta_pl.functionQueue.push(function(){
	window.IE = jQuery.browser.msie;
	window.is_ie6 = (function() { var b = jQuery.browser; return b.msie && (b.version == '6.0'); }());
	profileScInit();
	
});

function profileScInit() {

 profil_birthday.init(); 
 profil_switchSection.init(); 
 profil_photoSection.init(); 
 profil_blogSection.init(); 

 slidePhotos.init(); 
 if(jQuery("#photoCtr").hasClass('myPhoto')){
	 editTags.init();
	 editTitle.init();
	 editDesc.init();
	 jQuery("#photoDelete .delete").click(function(e){
	  e.preventDefault();
	  jQuery(this).addClass('hidden');
	  jQuery("#photoDelete form").removeClass('hidden');
	 });
	 jQuery("#photoDelete .no").click(function(e){
	  e.preventDefault();
		jQuery("#photoDelete form").addClass('hidden');
	  jQuery("#photoDelete .delete").removeClass('hidden');
	  });
 }
 jQuery("#photoDelete .censor").click(function(e){
	  e.preventDefault();
	  var btn = jQuery("#censorButton");
	  btn.click();
	 });
 jQuery("#photoDelete .publish").click(function(e){
	  e.preventDefault();
	  var btn = jQuery("#publishButton");
	  btn.click();
	 });
 jQuery("#photoDelete .publishIgnore").click(function(e){
	 e.preventDefault();
	 var btn = jQuery("#publishIgnoreButton");
	 btn.click();
 });
 jQuery("#photoDelete .toModerate").click(function(e){
	  e.preventDefault();
	  var btn = jQuery("#toModerateButton");
	  btn.click();
	 });

 //admin moderacja
 jQuery('.profileAdminPanel .selectOptions .selectAll').click(function(){
  jQuery(this).parents('form').find('.iCb').attr("checked", "checked");
  jQuery(this).parents('form').find('.selectOptions .c0 .hidden').removeClass('hidden');
  jQuery(this).parents('form').find('.selectOptions .c0 .selectAll').addClass('hidden');
 });

 jQuery('.profileAdminPanel .selectOptions .unselectAll').click(function(){
  jQuery(this).parents('form').find('.iCb').attr("checked", "");
  jQuery(this).parents('form').find('.selectOptions .c0 .hidden').removeClass('hidden');
  jQuery(this).parents('form').find('.selectOptions .c0 .unselectAll').addClass('hidden');
 });
 
 profil_photoArrows.init(); 
}



