function return_large_to_default() {
  var no_images = $("#flickr-gallery.splash_screen a").size();
  var randomnumber = Math.floor(Math.random()*no_images);
  $("#flickr-gallery.splash_screen a:eq("+randomnumber+")").click();
}
function power_homepage_slider(){
  no_images = $("#flickr-gallery.splash_screen a").size();
  $("#portfolio_figures").css("width", no_images+"00%");
  $("#flickr-gallery.splash_screen a").each(function(index){
    $(this).attr("rel", ".client"+(index+1));
    $("#portfolio_figures").append('<div class="portfolio_details  client'+(index+1)+'"><a href="'+ $("img", this).attr("alt")+ '"><span><h3>'+ $(this).attr("title")+ '</h3><p>'+ $("img", this).attr("title")+ '</p></span><img src="'+ $(this).attr("href")+'" /></a></div>');
    $(".client"+(index+1)).css("width", 100/no_images+"%");
  });
  
  var current_selection = 1;
  $("#flickr-gallery.splash_screen a").click(function(){
    targeted_name = $(this).attr("rel");
    target_width = $(targeted_name).width();
    target_number = parseInt(targeted_name.substring(7));
    
   
    if (current_selection != target_number) {
      if(current_selection < target_number) {
        $("#portfolio_figures").animate({
            left: -(target_width * (target_number-1))
            }, 300);
            
      } else {
        $("#portfolio_figures").animate({
            left: -(target_width * (target_number-1))
            }, 300);
      }}
    current_selection = target_number;
    $("#flickr-gallery.splash_screen .selected").removeClass("selected").css({ "background-color": "#ccc" });
    $(this).addClass("selected");
    clearInterval(return_large_to_default_timeout);
    return_large_to_default_timeout = setInterval("return_large_to_default()", 10000);
    return false;
  });
  return_large_to_default_timeout = setInterval("return_large_to_default()", 10000);
}

function power_slowfade() {
  $(".portfolio_details a span").hide();
  $(".portfolio_details a").hover(function(){
    $(this).children("span").show();
  }, function(){
    $(this).children("span").fadeOut(400);
  });
}

function power_clients_slider(){
  var current_selection = 1;
  
  $(".clients-category ul").each(function(){
    no_images = $(this).find("a").size();
    $(this).parents('.work_type').find(".clients-images ul").css("width", no_images+"00%");
    $(this).parents('.work_type').find(".clients-images ul li").css("width", 100/no_images+"%");
  });
  
  $(".clients-category ul a").mouseover(function(){
    targeted_name = $(this).attr("rel");
    target_width = $(targeted_name).width();
    target_number = parseInt(targeted_name.substring(7));
    
   
    if (current_selection != target_number) {
      if(current_selection < target_number) {
        
        $(this).parents(".work_type").find(".clients-images ul").animate({
            left: -(target_width * (target_number-1))
            }, 300);
            $(this).parents(".work_type").find(".clients-images ul li").fadeTo(200, 0.5, function(){
              $(this).parents(".work_type").find(".clients-images ul "+targeted_name).fadeTo(200, 1);
            });
            
      } else {
        $(this).parents(".work_type").find(".clients-images ul").animate({
            left: -(target_width * (target_number-1))
            }, 300);
            $(this).parents(".work_type").find(".clients-images ul li").fadeTo(200, 0.5, function(){
              $(this).parents(".work_type").find(".clients-images ul "+targeted_name).fadeTo(200, 1);
            });
      }}
    current_selection = target_number;
    $(this).parents(".work_type").find(".clients-category li a.selected").removeClass("selected");
    $(this).addClass("selected");
    //clearInterval(return_large_to_default_timeout);
    //return_large_to_default_timeout = setInterval("return_large_to_default()", 10000);
    return false;
  });
}

function power_contact_form() {
  $("#send_contact_form input").each(function(){
    $(this).val($(this).attr("placeholder"));
  });
  $("#contact_form input, #contact_form textarea").focus(function(){
    if(!$(this).hasClass("edited")) {
      $(this).attr("data-default", $(this).val()).val("").addClass("edited");
    }
  });
  $("#contact_form input, #contact_form textarea").blur(function() {
    if($(this).val() == "") {
      var default_val = $(this).attr("data-default");
      $(this).val(default_val).removeClass("edited");
    }
  });
  if ($("#send_contact_form").size() > 0) {
    $("#send_contact_form").validate({debug: true, rules: {
         mei_contact_name: "required",
         mei_contact_email: {
           required: true,
           email: true
         },
         mei_contact_type_of_project: {required: true, minlength: 2},
         mei_contact_phone: "required",
         mei_question: {
		required: true,
		remote: "http://www.meiinteriors.com/spamcheck.php"
	 }

       },
    messages: {
      mei_question:"Please check your answer"
    }

    });
   }
   $("#contact_form p.submit").append("<div id=\"sending\"></div>");
   $("#send_contact_form").submit(function(){
      $("#send_contact_form input").each(function(){
        if($(this).val() == $(this).attr("placeholder")) {
          $(this).val("");
        }
      });
     if($("#send_contact_form").validate().errorList.length < 1){
       $("#send_contact_form button").ajaxStart(function(){
         $(this).text("Sending...").attr("disabled", "disabled");
         $("#sending").show();
       });
       var form = $("#send_contact_form");
       $.ajax({
         type: "POST",
         url: form.attr("action"),
         data: form.serialize(),
         dataType: "html",
         success: function(message) {
           _gaq.push(['_trackPageview', '/submit.php']);
           $("#send_contact_form button").hide();
           $("#sending").hide();
           $("#send_contact_form button").after("<p>"+message+"</p>");
         },
         error: function(message) {
           /*$("#send_contact_form button").text("Resend Message").attr("disabled", "");
           //$("#send_contact_form button").hide();
           $("#sending").hide();
           $("#send_contact_form button").after("<p>Form Error Please Check Your Answer</p>");*/
         }
       });
     } else {
       return false;
     }
     return false;
     
   });
}

function power_easeout_effects() {
  $("#nav a.inactive").hover(function(){
    $(this).css({ "background-color": "#666" });
  }, function(){
    $(this).animate({ backgroundColor: "#333" }, 300);
  });
  
  $("#flickr-gallery li").hover(function(){
    $(this).css({ "background-color": "#c00" });
  }, function(){
    if(!$(this).hasClass("selected")) {
      $(this).animate({ backgroundColor: "#ccc" }, 300);
    }
  });
  
  $(".about-image_wrapper .control_buttons a, #quick_contact a.button, #events p.more a, .search_result .search_content a, #btn-action_contact_switch, #contact_form p.submit button").hover(function(){
    $(this).css({ "background-color": "#333" });
  }, function(){
    $(this).animate({ backgroundColor: "#c00" }, 300);
  });
  
  $(".about-image_wrapper li a, #clients .clients-images li a, #clients .clients-images li, #news .column ul li, #events .event-gallery a, .related_project p.image, #search_results ul li").hover(function(){
    $(this).css({ "background-color": "#c00" });
  }, function(){
    $(this).animate({ backgroundColor: "#ccc" }, 300);
  });
  
  $("#all_about li a, #events h2 a, #contact #vcard p a, #sitemap a, .related_project h3 a, #search_results h2 a, .project .project_tags a").hover(function(){
    $(this).css({ "color": "#c00" });
  }, function(){
    $(this).animate({ color: "#333" }, 300);
  });
  
  $("#news .column h3 a, #footer-inner p a, #header #utilities ul a").hover(function(){
    $(this).css({ "color": "#c00" });
  }, function(){
    $(this).animate({ color: "#fff" }, 300);
  });
//  
//  $("#news .recent_project img").hover(function(){
//    grayscale.reset(this.parentNode);
//  }, function(){
//    grayscale(this.parentNode);
//  });
//  grayscale.prepare( $("#news .recent_project img") );
  //grayscale($("#news .recent_project img"));
}

function power_project_gallery() {
  $(".project ul.gallery").after('<ul id="gallery_controls"></ul>');
  $(".project ul.gallery li:first-child").show();
  $(".project ul.gallery li").each(function(no){
    var number = no;
    $("#gallery_controls li:first-child").addClass("selected");
    $(this).clone().appendTo("#gallery_controls").click(function(){
      if(!$(this).hasClass("selected")) {
        $("#gallery_controls li").removeClass("selected");
        $(".project ul.gallery li").css("z-index", "100").hide();
        $(".project ul.gallery li").eq(no).css("z-index", "200").fadeIn(400);
        $(this).addClass("selected");
      }
      return false;
    });
  });
  
  $("#gallery_controls li").hover(function(){
    $(this).css({ "background-color": "#c00" });
  }, function(){
    $(this).animate({ backgroundColor: "#333" }, 300);
  }); 

  $(".project .gallery li").each(function(){
    var content = $(this).find("img").attr("alt");
    $(this).find("a").append(content);
    $(this).find("a").hover(function(){
      $(this).find("p").fadeIn(200);
    }, function(){
      $(this).find("p").fadeOut(400);
    });
  });
}

function power_carousel() {
   
  no_images = $("#preview_big_wrapper .preview_big li").size();
  if(no_images < 4) {
     $("#btn-next_pane").hide();
  }
  if (no_images > 0) {
    viewable_pane = 1;
    $("#btn-next_pane").click(function(){
      $("#preview_big_wrapper .preview_big").animate({
        top: -default_height*viewable_pane
      }, 400);
      viewable_pane = viewable_pane + 1;
      if (viewable_pane >= no_images/3) {
        $(this).hide();
      }
      $("#btn-prev_pane").show();
      return false;
    });
    $("#btn-prev_pane").hide().click(function(){
      $("#preview_big_wrapper .preview_big").animate({
        top: -default_height*(viewable_pane - 2)
      }, 400);
      viewable_pane = viewable_pane - 1;
      if (viewable_pane == 1) {
        $(this).hide();
      }
      $("#btn-next_pane").show();
      return false;
    });
  }
  setTimeout(function() {
    default_height = 3 * $("#preview_big_wrapper .preview_big li:first-child").height() + 40;
    $(".about-image_wrapper #preview_big_wrapper").height(default_height);
    
  }, 1000); 
  $(window).resize(function() {
    default_height = 3 * $("#preview_big_wrapper .preview_big li:first-child").height() + 40;
    $(".about-image_wrapper #preview_big_wrapper").height(default_height);
  });
}

function power_topup() {
  $(".top_up").click(function(){
    $(this).parent().addClass("toped_up");
  });
  TopUp.defaultPreset({
    shaded: 1,
    overlayClose: 1,
    onclose: "$('.toped_up').removeClass('toped_up');",
    resizable: 0
  });
  //preloads images
}

function power_action_contact(){
  $("#btn-action_contact_switch").click(function(){
    $(this).slideUp("fast").next().slideDown("fast"); return false;
    return false;
  });
}


function power_services_gallery() {
  $("#services #services-content .column ul a").each(function(no){
    $(this).parents(".column").children(".thumbs").append('<img src="'+$(this).attr("rel")+'" />');
    if(no == 0) {
      $(this).addClass("selected");
    }
    $(this).mouseover(function(){
      if(!$(this).hasClass("selected")) {
        $(this).parents(".column").find("img").fadeOut(200);
        $(this).parents(".column").find("img[src="+$(this).attr("rel")+"]").fadeIn(200);
      }
      $(this).parents("ul").find("a.selected").removeClass("selected");
      $(this).addClass("selected");
      return false;
    });
  });
  $("#services #services-content .column img:first-child").show();
}

function power_authentication() {
  $("#btn-login").toggle(function() {
    $("#header #utilities ul a.open").click();
    $("#login_panel").fadeIn("fast");
    $(this).addClass("open");
    return false;
  }, function() {
    $("#login_panel").fadeOut("fast");
    $(this).removeClass("open");
    return false;
  });
  $("#login_panel a").click(function(){
    $("#btn-login").click();
  });
  $("#btn-register").toggle(function() {
    $("#header #utilities ul a.open").click();
    $("#register_panel").fadeIn("fast");
    $(this).addClass("open");
    return false;
  }, function() {
    $("#register_panel").fadeOut("fast");
    $(this).removeClass("open");
    return false;
  });
  $("#register_panel a").click(function(){
    $("#btn-register").click();
  });
}


$(document).ready(function(){
  power_easeout_effects();
  power_contact_form();
  power_topup();
  power_clients_slider();
  power_homepage_slider();
  power_carousel();
  power_services_gallery();
  power_authentication();
  power_project_gallery();
  power_slowfade();
  power_action_contact();
});

