﻿$(document).ready(function(){
  Cufon.replace('.futura');
  Cufon.replace('.cta');

  $('.rating').stars();
  
  $("#tabs").tabs();
  
  initHittaRecept();
  
  initDefaultInputText();
  
  initSpelcialTabs();
  
  initModelLinks();

  initSortable();

  handleIngredientsHeadings();
  
  
  
  $('#login-google').click(function() {
      $('#openid_identifier').val('https://www.google.com/accounts/o8/id');
      $('#openid_login').submit();
  });

  $('#login-yahoo').click(function() {
      $('#openid_identifier').val('http://yahoo.com');
      $('#openid_login').submit();
  });
});



function handleIngredientsHeadings() {
    $('#ingredientsContainer input:checked').siblings('input, select').each(
      function() {
        SetHeading($(this));
      }
    );
    $("#ingredientsContainer input[type=checkbox]").click(
      function() {
        if($(this).attr("checked")) {
          $(this).siblings('input, select').each(
            function() {
              SetHeading($(this));
            }
          );
        } else {
           $(this).siblings('input, select').each(
            function() {
              RemoveHeading($(this));
            }
          );
        }
      }
     );
}

function SetHeading(obj) {
  var id = obj.attr("id");
        if ( id != "" ) {
         switch( id.split("_")[0]){
               case "Quantity":
               case "Unit":
                 obj.addClass("disabled");
                 break;
                 
               case "Ingredient":
                 obj.addClass("bold");
                 break;
                    }
        }
}

function RemoveHeading(obj) {
   var id = obj.attr("id");
        if ( id != "" ) {
         switch( id.split("_")[0]){
               case "Quantity":
               case "Unit":
                 obj.removeClass("disabled");
                 break;
                 
               case "Ingredient":
                 obj.removeClass("bold");
                 break;
                    }
        }
}

function initSortable() {
  $(".sortable").sortable({
     stop: function(event, ui) { 
       $("li", this).each( function(i) {
         $("input, select", this).each( function(n) {
           var _id = $(this).attr("id");
           if ( _id != "" ) {
             var _idArr = _id.split("_");
             $(this).attr("id", _idArr[0] + "_" + i);
           }
           var _name = $(this).attr("name");
           if ( _name != "" ) {
             var _nameArr = _name.split("_");
             $(this).attr("name", _nameArr[0] + "_" + i);
           }
         });
         $("label.lblInstruction", this).each( function (n) {
           $(this).text("Steg " + (i + 1));
         });
       }); 
     }
  });
}


function initModelLinks() {
    $("a.opendialog").click(function(){
      $($(this).attr("href")).dialog({
            bgiframe: true,
            resizable: false,
            height: 350,
            modal: true            
          }).dialog('open');
      return false;
      });


    $("a.opentellafriend").click(function(){
      $($(this).attr("href")).dialog({
            bgiframe: true,
            resizable: false,
            height: 340,
            modal: true            
          }).dialog('open');
      return false;
 	 });


	$("a.openvote").click(function(){
      $($(this).attr("href")).dialog({
            bgiframe: true,
            resizable: false,
            height: 510,
            modal: true            
          }).dialog('open');
      return false;
  	});

	
	$("a.openrules").click(function(){
      $($(this).attr("href")).dialog({
            bgiframe: true,
            resizable: false,
            height: 300,
            modal: true            
          }).dialog('open');
      return false;
      });

  }
  

  
function initHittaRecept() {
  /* injectar markup f�r css */
  $("#hitta_recept").after('<div id="hitta_recept_flik"></div>');
  $("#hitta_recept span").before('<div id="hitta_recept_skugga"></div>');
  /* hooka events 
  $("#hitta_recept").mouseenter(function(){
      $(this).stop().animate( { width:"191px" }, { queue:false, duration:300 } )
  });
  $("#hitta_recept").mouseleave(function(){
      $(this).stop().animate( { width:"151px" }, { queue:false, duration:100 } )
  });*/
}

function initDefaultInputText () {
  
    $(".defaultText").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
    
    $(".defaultText").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
    
    $(".defaultText").blur();     
    
    $("form").submit(function() {
      $(".defaultText").each(function() {
        if($(this).val() == $(this)[0].title) {
          $(this).val("");
        }
      });
     }); 
  }
  
function initSpelcialTabs() {
  
  $("#large-banner-content-nav").each(function(index) {
      $("a", this).each(function(index) { if ( index == 0) $(this).addClass("ui-state-active"); });
      $("#tabs div").each(function(index) { if ( index > 0 ) $(this).hide(); /*addClass("ui-tabs-hide");*/ });
      
      $("a", this).click(function() {
        //event.preventDefault();
        $("#large-banner-content-nav a").each(function(index) { $(this).removeClass("ui-state-active");});
        $(this).addClass("ui-state-active");
        $("#tabs div").each(function(index) { $(this).slideUp("fast"); /*addClass("ui-tabs-hide");*/ });
        $($(this).attr("href")).slideDown("fast"); /*removeClass("ui-tabs-hide");*/
        return false;
      });
      });

  }

  function shareonfacebook(longUrl, message, shortDesc, imageUrl) {
      //var message = "Hittade ett fantastiskt recept";
      var templatedata = { "href": longUrl, "name": message, "description": shortDesc, 'media': [{ 'type': 'image', 'src': imageUrl, 'href': longUrl}] };
      FB_RequireFeatures(['XFBML'], function() {
          FB.ensureInit(function() {
          FB.Connect.streamPublish('', templatedata, null, null, 'Skriv en kommentar! (Valfritt)', null);
          });
      });
  }