$(document).ready(function(){

  $("a.fancybox").fancybox({
    'onClosed'		: function() { $("#cw").show(); },
    'onStart'		: function() { $("#cw").hide(); },
  });

  $("a[href='#']").click(function(){
    return false;
  });

  $("input.date").datepicker({
    dateFormat: "yy-mm-dd",
    buttonImageOnly: true,
    buttonImage:  'img/calendar.gif',
    firstDay: 1,
    showAnim: 'slideDown',
    showOn: 'both'
  });

  $("a.show_result").click(function(){

    $(this).parent().parent().find(".choice").hide();
    $(this).parent().parent().find(".result").show();
    return false;

  });

  $("#market_form select[name='form']").change(function(){

    var saved_form = $("#market_form select[name='form']").val();
    if(saved_form != '0'){ document.location = '?page=market&action=load&form=' + saved_form; }
    return false;

  });

  $("#market_form input[name='save']").click(function(){

    var saved_form = $("#market_form select[name='form']").val();
    var title = '';
    if(saved_form == '0'){

      while(!title){
        title = prompt(title_prompt_text);
      }

      if(!title) return false;

      $("#market_form input[name='title']").val(title);

    }

  });

  $("#delete_saved_form").click(function(){

    var saved_form = $("#market_form select[name='form']").val();
    if(saved_form == '0') return false;

    document.location = '?page=market&action=delete&form=' + saved_form;
    return false;
  });

  $("#mail .delete").click(function(){

    var link = $(this).attr("href")+"&method=ajax";
    var elem = $(this);
    $.ajax({
      url: link,
      async: false,
      success: function(data){
        if(data == '1'){
          elem.parent().parent().remove();
        }
      }
    });
    return false;

  });

  $(".confirm").click(function(){

    if($(this).is("input")){

      return confirm("Really want to: " + $(this).attr("value") + " ?");

    }else if($(this).is("a")){

      return confirm("Really want to: " + $(this).text() + " ?");

    }else{

      return confirm("Really?");

    }

  });


  $("#lang_menu").change(function(){

    var lang = $(this).find("select").val();
    window.location = "?lang=" + lang;

  });

  $("#left ul.menu li:nth-child(2) a").addClass("top");

  $("a.register").fancybox({
    'onClosed'		: function() { $("#cw").show(); },
    'onStart'		: function() { $("#cw").hide(); },

  });

  $(".mf_wrapper a img").click(function(){

    var id = $(this).parent().parent().parent().find("textarea").attr("id");

    switch($(this).attr("id")){
      case 'h1': addTags('[h1]','[/h1]', id);return;
      case 'h2': addTags('[h2]','[/h2]', id);return;
      case 'quote': addTags('[quote]','[/quote]', id);return;
      case 'user': addTags('[user id=xxx]','', id);return;
      case 'player': addTags('[player id=xxx]','', id);return;
      case 'team': addTags('[team id=xxx]','', id);return;
      case 'country': addTags('[country id=xxx]','', id);return;
      case 'league': addTags('[league id=xxx]','', id);return;
      case 'bold': addTags('[b]','[/b]', id);return;
      case 'italic': addTags('[italic]','[/italic]', id);return;
      case 'underline': addTags('[underline]','[/underline]', id);return;
      case 'link': addTags('[link url=xxx text=xxx]','', id);return;
      case 'img': addTags('[img src=xxx]','', id);return;
   }

  });


  $("img.revert").click(function(){

    var id = $(this).attr("id");
    var elem = $(this);

    $.ajax({
      url: 'index.php?page=market&method=ajax&action=revert&transfer=' + id,
      async: false,
      success: function(data){
        if(data == 1){
          elem.parent().parent().remove();
        }else{
          $("#content").prepend("<div class='error'>" + data  + "</div>");
        }
      }
    });

  });


  $("#username").focus();

  $('input[type="checkbox"]').ezMark();

  $("#tour").click(function() {
		$.fancybox([
      {
				'href'	: 'img/screens/new/team.png',
				'title'	: 'After registration you will get a brand new team fully under your control'
			},
			{
				'href'	: 'img/screens/new/players.png',
				'title'	: 'Set-up your best line-up and tactics!'
			},
      {
				'href'	: 'img/screens/new/market.png',
				'title'	: 'If your team is lack of good players, visit transfer market and search for best in business players'
			},
      {
				'href'	: 'img/screens/new/league.png',
				'title'	: 'Your goal could be to climb up to the top division in your country and fight your way to international cups'
			},
      {
				'href'	: 'img/screens/new/youths.png',
				'title'	: 'Search, buy and train future super stars, if that\'s your strategy. Got future golden boot? Change his name and...'
			},
      {
				'href'	: 'img/screens/new/progress.png',
				'title'	: '... follow his training progress in a nice graph!'
			},
      {
				'href'	: 'img/screens/new/estimate.png',
				'title'	: 'Estimate his future and make your decisions!'
			},
      {
				'href'	: 'img/screens/new/preformance.png',
				'title'	: 'Your super star is old enough to join your main team? Add him to your squad and follow his preformance!'
			},
      {
				'href'	: 'img/screens/new/nt.png',
				'title'	: 'Got strongest team or not yet? Give a try to the National or U18 teams coach position. Join elections and write heart-breaking speech, to get most votes!'
			},
      {
				'href'	: 'img/screens/new/wc.png',
				'title'	: 'If you have succeeded in elections, select best players, choose best tactics and outsmart your opponents in World Cup.'
			},
      {
				'href'	: 'img/screens/new/wc_group.png',
				'title'	: 'Even if you\'re not a national coach, keep your fingers crossed for your country from the qualification phase till the World Cup final!'
			},
      {
				'href'	: 'img/screens/new/forum.png',
				'title'	: 'We are building strong but friendly community, so jump into forums and share your oppinions. Don\'t worry, you will be heard'
			},
      {
				'href'	: 'img/screens/new/stats.png',
				'title'	: 'Check our constantly increasing stats page and give your ideas, how to improve it'
			},

      {
				'href'	: 'img/screens/new/pbp.png',
				'title'	: 'Watch all games in a play-by-play match report!'
			},


		], {
			'padding'			: 10,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'            : 'image',
			'titlePosition'   : 'inside',
			'changeFade'      : 500,
      'onStart' : function(){ $("#cw").hide() },
      'onClosed' : function(){ $("#cw").show() }

		});

    return false;
	});

});



function addTags(tag1,tag2,id){

  var textarea = document.getElementById(id);
  if (document.selection){
    textarea.focus();
    var sel = document.selection.createRange();
    sel.text = tag1 + sel.text + tag2;
  }else{
    var textarea = document.getElementById(id);
    var len = textarea.value.length;
    var start = textarea.selectionStart;
    var end = textarea.selectionEnd;
    var sel = textarea.value.substring(start, end);
    var replace = tag1 + sel + tag2;
    textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
  }
   textarea.focus();

}



function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

