var top10 = {  
  init: function()
  {
    if($('#top10-preview').length)
    {
      var links = $('#top10-preview .title a');
      
      links.click(function(e)
      {
        e.preventDefault();
        links.removeClass('selected');
        $(this).addClass('selected');
        $('#top10-preview-holder').html($('#' + $(this).attr('link')).html());
      });
      
      $(links.get(0)).trigger('click');
    };
  }
};

var fhSmilies = {
	txtarea: 'message',					//ID of the textarea
	messages: '.comment p',		//Identifier for smiley replacement
	smiliesHolder: 'smilies',				//UL to fill with smilies
	smilies: [	//Array with smilies (shortcut, regexpr, file)
	          	[':)','\:\)', 'smile.gif'],
	          	[':d','\:d', 'largesmile.gif'],
	          	[';)','\;\(', 'blink.gif'],
	          	[':(','\:\(', 'sad.gif'],
	          	[':$','\:\$', 'shy.gif'],
	          	[':x','\:x', 'frustrated.gif'],
	          	[':s','\:s', 'sick.gif'],
	          	[':p','\:p', 'tong.gif'],
	          	[':-ppp','\:\-ppp', 'tong2.gif'],
	          	[':licking:','\:licking\:', 'tong3.gif'],
	          	['(y)','\(y\)', 'thumb.gif'],
	          	['(l)','\(l\)', 'love.gif'],
	          	[':angry:', '\:angry\:', 'angry.gif'],
	          	['(()', '\(\(\)', 'confused.gif'],
	          	[':takepicture:', '\:takepicture\:', 'takepicture.gif'],
	          	[':hearts:', '\:hearts\:', 'hearts.gif'],
	          	['(k)', '\(k\)', 'kiss.gif'],
	          	[':hugging:', '\:hugging\:', 'hugging.gif'],
	          	[':teddy:', '\:teddy\:', 'teddy.gif']
	],
	
	init: function(){
   	this.findAndReplace(fhSmilies.messages);
   	
    if($('#' + fhSmilies.smiliesHolder).length)
    {
    	fhSmilies.fill();
    	fhSmilies.findAndReplace('#' + fhSmilies.smiliesHolder + ' a');
  		$('#' + fhSmilies.smiliesHolder + ' a').click(function()
  		{
  		  fhSmilies.insert($(this));
  		});
    }
	},
	
	insert: function(el)
	{
		var txtarea = $('#' + fhSmilies.txtarea);
		smiley = ' ' + el.attr('id') + ' ';
		fhSmilies.insertAtCursor(txtarea, smiley);
	},
	
	insertAtCursor: function (myField, myValue)
	{
    if(document.selection)
    {
      myField.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
    }
    else if(myField.selectionStart || myField.selectionStart == '0')
    {
      myField.focus();
      var startPos = myField.selectionStart;
      var endPos = myField.selectionEnd;
      myField.val(myField.val().substring(0, startPos) + myValue + myField.val().substring(endPos, myField.val().length));
      myField.setSelectionRange(endPos + myValue.length, endPos + myValue.length);
    }
    else
    {
      myField.val(myField.val() + myValue);
    };
	},
	
	findAndReplace: function(identifier)
	{
		$(identifier).each(function()
		{
		  var element = $(this);
      var text = element.html();
		  
		  var text = text.replace(/\:\(/gi, '<img src="' + imageUrl + '/smilies/sad.gif" style="display:inline;" />');
		  text = text.replace(/\:\)/gi, '<img src="' + imageUrl + '/smilies/smile.gif" style="display:inline;" />');
		  text = text.replace(/\:d/gi, '<img src="' + imageUrl + '/smilies/largesmile.gif" style="display:inline;" />');
		  text = text.replace(/\;\)/gi, '<img src="' + imageUrl + '/smilies/blink.gif" style="display:inline;" />');
		  text = text.replace(/\:\(/gi, '<img src="' + imageUrl + '/smilies/sad.gif" style="display:inline;" />');
		  text = text.replace(/\:\$/gi, '<img src="' + imageUrl + '/smilies/shy.gif" style="display:inline;" />');
		  text = text.replace(/\:x/gi, '<img src="' + imageUrl + '/smilies/frustrated.gif" style="display:inline;" />');
		  text = text.replace(/\:s/gi, '<img src="' + imageUrl + '/smilies/sick.gif" style="display:inline;" />');
		  text = text.replace(/\:p/gi, '<img src="' + imageUrl + '/smilies/tong.gif" style="display:inline;" />');
		  text = text.replace(/\:\-ppp/gi, '<img src="' + imageUrl + '/smilies/tong2.gif" style="display:inline;" />');
		  text = text.replace(/\:licking\:/gi, '<img src="' + imageUrl + '/smilies/tong3.gif" style="display:inline;" />');
		  text = text.replace(/\(y\)/gi, '<img src="' + imageUrl + '/smilies/thumb.gif" style="display:inline;" />');
		  text = text.replace(/\(l\)/gi, '<img src="' + imageUrl + '/smilies/love.gif" style="display:inline;" />');
		  text = text.replace(/\:angry\:/gi, '<img src="' + imageUrl + '/smilies/angry.gif" style="display:inline;" />');
		  text = text.replace(/\(\(\)/gi, '<img src="' + imageUrl + '/smilies/confused.gif" style="display:inline;" />');
		  text = text.replace(/\:takepicture\:/gi, '<img src="' + imageUrl + '/smilies/takepicture.gif" style="display:inline;" />');
		  text = text.replace(/\:hearts\:/gi, '<img src="' + imageUrl + '/smilies/hearts.gif" style="display:inline;" />');
		  text = text.replace(/\(k\)/gi, '<img src="' + imageUrl + '/smilies/kiss.gif" style="display:inline;" />');
		  text = text.replace(/\:hugging\:/gi, '<img src="' + imageUrl + '/smilies/hugging.gif" style="display:inline;" />');
		  text = text.replace(/\:teddy\:/gi, '<img src="' + imageUrl + '/smilies/teddy.gif" style="display:inline;" />');
		  
		  element.html(text);
		  
		});
	},
	
	fill: function()
	{
	  for(var i=0; i < fhSmilies.smilies.length; i++)
	  {
	   var smiley = fhSmilies.smilies[i];
      $('#' + fhSmilies.smiliesHolder).append('<a id="' + smiley[0] + '">' + smiley[0] + '</a>');
    };
	}
};

var portfolio =
{
  init: function()
  {
    
    
    
    var options = {
      type: 'iframe',
      width: 810,
      height: 580,
      autoScale: false
    }
    $("#profile #portfolio a.image").fancybox(options);
    options.height = 370;
    $("#place-gadget").fancybox(options);
    $("#btn-add-picture, #portfolio a.edit, #shoot #portfolio a.image").fancybox();
    
    var picture = $('#picture');
    if(picture.length)
    {
      var rating = $('#rating');
      rating.gzRating({
        id: rating.attr('ratingId'),
        amount: 10,
        maximum: 10,
        average: rating.attr('average'),
        imagePath: url + '/public/img/',
        updateUrl: url + '/models/vote/picture/{ID}/rating/{RATING}',
        onRate: function(msg)
        {
          rating.html(msg);
        }
      });
    };
    
    $('#sortable-portfolio').sortable({
      update: function(event, ui) {
        $.ajax({
          url: $(this).attr('sortableUrl'),
          data: $(this).sortable('serialize')
        });
      }
    });
  }
};

var signup = {
  init: function()
  {
    $('#birth_date_day, #birth_date_month, #birth_date_year').change(function()
    {
      $('#birth_date').val($('#birth_date_year').val() + '-' + $('#birth_date_month').val() + '-' + $('#birth_date_day').val());
    });
  }
};

var qtip = {
  init: function()
  {
    $('.error').qtip({
      show: {
        delay: 500
      },
      hide: {
        fixed: true,
        delay: 300
      },
      style: {
        width: 300,
        padding: 5,
        border: {
          width: 7,
          radius: 5
        },
        tip: 'leftMiddle',
        name: 'light' // Inherit from preset style
      },
      position: {
        corner: {
          target: 'rightMiddle',
          tooltip: 'leftMiddle'
        }
      }
    });
  }
};

$(document).ready(function()
{
  top10.init();
  fhSmilies.init();
  portfolio.init();
  signup.init();
  qtip.init();
});
