function initHomeSlide() {
	if (!$('#home_slide').length)
		return false;

	$('#thumb_side a.thumb').mouseover(function(){if ($(this).hasClass('selected'))return false; $(this).fadeTo('fast', .5)});
	$('#thumb_side a.thumb').mouseout(function(){$(this).fadeTo("fast", 1)});
	$('#thumb_side a.thumb').click(loadHomeImage);
}

function loadHomeImage() {
	if ($(this).hasClass('selected'))
		return false;
	
	var that = this;
	$('#thumb_side a.selected').removeClass('selected');
	$(this).addClass('selected');
	
	var newImage = new Image();
	$(newImage).ready(function () {
		$('#home_image').hide();
		$('#home_image > div.new').hide();
		$('#home_image > span').html('')
		$('#home_image > span').attr('id');
		$('#home_image > span').append(newImage);		
		$(newImage).css({'margin-top':Math.ceil((600-$(that).attr('rel'))/2)+'px'});
		$('#home_image').fadeTo('slow', 1);
		if ($('div.star', that).length>0)
			$('#home_image > div.new').show();
	});
	
	newImage.src = $(this).attr('href');
	$('a.popup').attr('rel',$(this).attr('id'));
	//newImage.id = $(this).attr('id');
	
	return false;
}

function mooveGallery (){
   //TODO click
   $('#slide_up').live('click',slideUp);
  
  $('#slide_down').live('click',slideDown);
  return false;
} 

function slideUp() {
       $('#slide_up').die('click',slideUp );
       var cnt=$('#thumb_side').children();    
      
       var ml=parseInt(cnt.css('margin-top').replace('px',''));
       //if (ml<=$('#gallery').height()-cnt.height()+161)
        //return false;
       if(ml == 0)
          return false;
       cnt.stop().animate({'margin-top':(ml+450)+'px'},200, function (){
        $('#slide_up').live('click',slideUp );

       } );
       
       return false;
   }
   
function slideDown() {
    $('#slide_down').die('click',slideDown );
    var cnt=$('#thumb_side').children();
    
    var ml=parseInt(cnt.css('margin-top').replace('px',''));
    if (Math.abs(ml)>=cnt.height()-322)
      return false;
    cnt.stop().animate({'margin-top':(ml-450)+'px'},200, function(){
      $('#slide_down').live('click',slideDown );
 
    } );
    
   return false;
   }   

function initSlideDisco(){

  $('.openDisco').click(closeComments);
  $('.closeDisco').click(openComments);
  return false;
}
      //947 con div
      function closeComments(){
        $("#slide_discografia").css("width","48");
        $(".openDisco").hide();
        $("#cnt_scroll_home").hide();

        $(".closeDisco").show();
        return false;
      }
      function openComments(){
        $("#slide_discografia").css("width","947");
        $("#cnt_scroll_home").show();
        $(".openDisco").show();

        $(".closeDisco").hide();
        return false;
      }

function showDiscoList() {
  
  /*if($(this).hasClass('selected'))
    return false;*/
  
  $('.disco').removeClass('selected');
  $(this).addClass('selected');  

	var objHash = {};
	objHash.action = 'LOADNEWDISC';
	objHash.id_disco = $(this).attr('rel');
	//objHash.type = $('#type').val();

   
	$.ajax({
  	url: '/include/process.php',
  	data: objHash,
  	type: 'POST', 
  	dataType: 'json', 
  	success: function (data) {
      if (!data.res) {
  			alert(data.msg);
  		} else {  		
  			
  			if( $("#disco_selezione").attr('id') )
  			{
  			 $("#disco_selezione").fadeOut("slow",function(){
          $("#disco_selezione").remove();
          $('#cnt_scroll').after(data.cnt);
         } );
         
         
         
        } else {
          $('#cnt_scroll').after(data.cnt);
          $.scrollTo( $('#disco_selezione'), 400, {axis:'y'} );
				}
        
        /*$('#TB_actions').before(data.cnt);
  			$('#TB_question_'+data.id).scrollTo();
  			$('#TB_question_'+data.id+' a.delete').bind('click',removeQuestion);
  			if ($('#lang').attr('class').split(' ').last()=='ita')
  				$('.eng').hide();
  			else
  				$('.ita').hide();*/
  		}
  	},
  	error: function(XMLHttpRequest, textStatus){alert('Error:\n'+XMLHttpRequest.responseText)}
	});
	

  return false;
}


/*---MEDIA---*/
function initMedia(selected){
  $('.media1').click(switchTab);
  $('.media2').click(switchTab);
  
 	$('#cnt_scroll_TV').tinyscrollbar({ axis: 'x'});
  $('#cnt_scroll_CINEMA').tinyscrollbar({ axis: 'x'});
  $('#cnt_scroll_DVD').tinyscrollbar({ axis: 'x'});
  $('#cnt_scroll_SPOT').tinyscrollbar({ axis: 'x'});

	switch (selected) {
	case 'cine':
		$('#cnt_scroll_DVD').hide();
		$('#cnt_scroll_SPOT').hide();
		break;
	case 'tv':
		$('#cnt_scroll_CINEMA').hide();
		$('#cnt_scroll_SPOT').hide();
		break;
	case 'pub':
		$('#cnt_scroll_CINEMA').hide();
		$('#cnt_scroll_TV').hide();
		break;
	default:
		$('#cnt_scroll_CINEMA').hide();
		$('#cnt_scroll_SPOT').hide();
		break;
	}
}

function switchTab(){
  if($(this).hasClass('selected'))
   return false;
  
  if($(this).hasClass('media1'))
  {
    $('.media1').removeClass('selected');
    if($(this).hasClass('dvd'))
    {
      $('#cnt_scroll_CINEMA').hide();
      $('#cnt_scroll_DVD').show();
      
    }  
    else
    {
      $('#cnt_scroll_CINEMA').show();
      $//('#cnt_scroll_CINEMA').tinyscrollbar({ axis: 'x'});
      $('#cnt_scroll_DVD').hide();
    }
  }
  else
  {
    $('.media2').removeClass('selected');
    if($(this).hasClass('tv'))
    {
      $('#cnt_scroll_SPOT').hide();
      $('#cnt_scroll_TV').show();
    }  
    else
    {
      $('#cnt_scroll_SPOT').show();
    	//$('#cnt_scroll_SPOT').tinyscrollbar({ axis: 'x'});
      $('#cnt_scroll_TV').hide();
    }
  }    
  $(this).addClass('selected');
  
  return false; 
}

function initExtra(){
	//IMAGES
	$('a.image_popup').fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'overlayColor' : '#ffffff',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Immagine ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	//AUDIO - VIDEO
	$('a.popup').fancybox({
		'titleShow' 	: false
	});
  
  return false;
}


var galleryLenght=0;
var galleryProducts={};
var galleryIndex=0;
var first = 0;
var numero = 0;

function openPopup()
{
  showPopup();
  var id_content = $(this).attr('rel').split('_').last();
  var type_content = $(this).attr('rel').split('_').first();

  var num = $(this).attr('id').split('_').last();

  var test = $(this).attr('rel').split('_');

  var content = "";
  for(i=1; i != test.length; i++)
  {
    content = content+test[i]+'_'; 
  }

  //alert(content.substr(0,content.length-1));
  id_content = content.substr(0,content.length-1);
//  return false;
  numero = $('.image_popup').length;

  if(num)
  {
    galleryIndex = 0; 
    galleryIndex = num-1; 
  }
   
  var tabella = $('#tabella').val();

  $.ajax({
  	url: '/include/process.php',
  	data: {action:'SHOWPOPUP', immagini:numero, id:id_content, type:type_content, tabella:tabella, num:num}, 
  	dataType: 'json', 
  	success: function (data) {
          
      if (!data.res) {
  			closePopup();
  			printMessage(data.msg);
  		} else {
  			console.log(data.size);
  			setPopupContent (data.cnt,data.size,true);
  			if(type_content == 'g' || type_content == 'i')
  			{
          galleryImage=data.gallery ;
    			galleryLenght=galleryImage.length;
    			showGalleryImage();
        }
  			if(type_content == 'b')
  			{
           initBranoPopup();
        }

  		}
  	},
  	error: function(XMLHttpRequest, textStatus){closePopup(); printMessage('Error:\n'+XMLHttpRequest.responseText);}
	});
	
  
  return false;
}

function openPopupUrlTalk(url)
{
  showPopup();
  var id_content = url.split('_').last();
  var type_content = url.split('_').first();
  var num = url.split('_').last();

  if(num)
  {
    galleryIndex = 0; 
    galleryIndex = num-1; 
  }
   
  var tabella = $('#tabella').val();

  $.ajax({
  	url: '/include/process.php',
  	data: {action:'SHOWPOPUP',  id:id_content, type:type_content, tabella:tabella}, 
  	dataType: 'json', 
  	success: function (data) {

      if (!data.res) {
  			closePopup();
  			printMessage(data.msg);
  		} else {
  			setPopupContent (data.cnt,data.size,true);
  			if(type_content == 'g' || type_content == 'i')
  			{
          galleryImage=data.gallery ;
    			galleryLenght=galleryImage.length;
    			showGalleryImage();
        }
  			if(type_content == 'b')
  			{
           initBranoPopup();
        }

  		}
  	},
  	error: function(XMLHttpRequest, textStatus){closePopup(); printMessage('Error:\n'+XMLHttpRequest.responseText);}
	});
	
  
  return false;
}



function showGalleryImage () {
  
    galleryIndex++;
    $('.image_gallery').html('<img style=\"display:none;\" src="'+galleryImage[galleryIndex].immagine+'" /><br class="clearfloat" />');
		$('.image_gallery img').fadeIn(1000);
		$('.didascalia_gallery').html(galleryImage[galleryIndex].didascalia);

    if (galleryIndex==(galleryLenght))  
		{
      galleryIndex=0;
      first = 1;
    }
    return false;		  
}

function showPrevImage () {

    //alert(galleryIndex+' - '+galleryLenght);
    
    if (galleryIndex == 0 )
    {
      galleryIndex=(galleryLenght);
      first = 1;
	  }
	  
      galleryIndex--;
    
		
		
		
    $('.image_gallery').html('<img style=\"display:none;\" src="'+galleryImage[galleryIndex].immagine+'" /><br class="clearfloat" />');
		$('.image_gallery img').fadeIn(1000);
    $('.didascalia_gallery').html(galleryImage[galleryIndex].didascalia);
    
    return false;		  

}


function initBranoPopup(){
  if ($('#info_brano').lenght ==0)
  	return false;
  $('.testo').click(function ()
    {
      $('#info_brano').hide();
      $('#scroll_popup').show();
      $('#scroll_popup').tinyscrollbar();
      return false;  
    }
  );
  $('.info').click(function ()
    {
      $('#scroll_popup').hide();
      $('#info_brano').show();
      return false;  
    }
  );
  return false;
  
}

function printMessage (txt) {
	alert (txt);
}

function openIntro(){

    $('.tot_intro').show();
		$('.open_intro').hide();
	  $('.close_intro').click(function ()
	    {
	      $('.tot_intro').hide();
	      $('.open_intro').show();
	     	$(window).scrollTop(0);
	      return false; 
	       
	    }
	  );
    return false;

}































/*JQUERY*/
(function($) {
	//JSON CHECK
	String.prototype.isJson = function () {
 		if (jQuery.trim(this) == '')
 			return false;
 		str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
 		return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
	}

	//OBJ - UP
	$.fn.up = function () {
		var _type=arguments[0];
		var _parents=$(this).parents();
		
		if (_type==undefined)
			return (_parents[_level]);
		
		_parent=false;
		_parents.each(function () {			
			if (this.tagName==_type.toUpperCase()) {
				_parent=$(this);
				return false;
			}
		});
		
		return _parent;
	}	
	
	//OBJ SCROLLTO
	$.fn.scrollTo = function () {
		var offset = $(this).offset().top;
  	$('html,body').animate({
   		scrollTop: offset
		}, 500);
	}
	
	//OBJ SWITCH
	$.fn.switchStatus = function () {	
		if ($(this).hasClass('offline')) {
			margin=-61;
			$(this).removeClass('offline');
			$(this).addClass('online');
		}
		else {
			margin=0;
			$(this).removeClass('online');
			$(this).addClass('offline');
		}
		
		$('.switch_cnt',this).animate({'margin-left':margin+'px'},200);
	}
	
	//LANG SWITCH
	$.fn.switchLang = function (lang) {		
		if ($(this).hasClass('ita')) {
			margin=-61;
			var lang=Array('ita','eng');
		}
		else {
			margin=0;
			var lang=Array('eng','ita');
		}
		
		$(this).removeClass(lang[0]);
		$(this).addClass(lang[1]);
		
		$('#lng_cnt').animate({'margin-left':margin+'px'},200);		
		
		$('.'+lang[0]).hide();
  	$('.'+lang[1]).show();
  	
  	if (tinymce) {
			for( id in tinyMCE.editors ) {			
				if (id.endsWith(lang[1])) {
					tinyMCE.editors[id].setContent(tinyMCE.editors[id].getContent());
				}
			}
		}  
	};


	//FORM - FORM ELEMENTS ENABLE
	$.fn.enable = function() {	
		if (this.get(0).tagName=='FORM')
			$(':input', this).removeAttr('disabled');
		else
			$(this).removeAttr('disabled');
	};
	
	//FORM - FORM ELEMENTS DISABLE
	$.fn.disable = function() {
		if (this.get(0).tagName=='FORM')
			$(':input', this).attr('disabled', 'disabled');
		else
			$(this).attr('disabled', 'disabled');
	};
	
	//FORM CHECK REQUIRED FIELDS
	$.fn.checkRequired = function() {
		var _required=true;
		$('.required', this).each (function () {
			$(this).removeClass('input_error');
			if ($(this).val()=='') {
				$(this).addClass('input_error');
				_required=false;
			}
		});
		return _required;
	}
	
	//FORM + ACTION ENABLE/DISABLE
	$.fn.setForm = function(status) {
		if (this.get(0).tagName!='FORM')
			return false;
		$('.tinymce',this).each (function (){
			$(this).tinymce().setProgressState(!status);
		});
		var button=$('.button',this);
		if (status) {
			$(this).enable();
			if (button.length>0) {
				button.removeClass('disabled');
				if (button.get(0).tagName=='A')
					button.html('salva');
				else
					button.val('salva');
				button.next('span').show();
			}
		} else {
			$(this).disable();			
			if (button.length>0) {	
				button.addClass('disabled');
				if (button.get(0).tagName=='A')
					button.html('salvataggio');
				else
					button.val('salvataggio');
				button.next('span').hide();
			}
		}
	}
	
	//ARRAY FIRST OBJ
	Array.prototype.first = function() {
		return this[0];
	}
	//ARRAY LAST OBJ
	Array.prototype.last = function() {
		return this[this.length-1];
	}
	//ARRAY SEARCH
	Array.prototype.array_search = function( needle, strict ) {
		var strict = !!strict;

		for(var key in this){
			if( (strict && this[key] === needle) || (!strict && this[key] == needle) ) {
					return key;
			}
		}

		return false;
	}
	
	//STRING CAPITALIZE
	String.prototype.capitalize = function() {
		return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
	}
	//STRING START WITH
	String.prototype.startsWith = function(text) {
		return (text == this.substring(0, text.length));
	}
	//STRING END WITH
	String.prototype.endsWith = function(text) {
		return (text== this.substring(this.length - text.length));
	}
	//STRING STRIP TAGS
	String.prototype.stripTags = function() {
		return this.replace(/<\/?[^>]+>/gi, '');
	};
	
})(jQuery);

