/**
 * Common javascript
 *
 * Created by: Topsitemakers
 * http://www.topsitemakers.com/
 */
$(document).ready(function(){
  
  // Tabs
  if($(".tabs").length>0){
    $(".tab-title").click(function(){
      $("#"+$(this).attr("rel")).hide();
      $("#"+$(this).attr("name")).show().addClass("active");
      $("a[rel="+$(this).attr("name")+"]").removeClass("active");
      if(!$(this).hasClass("active")) $(this).addClass("active");
    });
    // Hide listing content
    $("#sidebar .listing h4, #sidebar .listing>a, #sidebar .listing .excerpt, #sidebar .listing .read-more").remove();
    $(".tab-content.active").show();
  }
  
  // Last breadcrumb style
  if($("#breadcrumbs").length>0){
    $("#breadcrumbs a").eq(-1).addClass("last");
  }
  
  // Last blog category <li>
  $("#blog-categories li").eq(-1).addClass("last");
  
  // Login box
  if(logged==1){
    $("#login-box").remove();
    $("#nav-top li").eq(0).html('Hi '+loggedUser+'<span>|</span><a>Log out</a>');
    $("#nav-top").addClass("logged");
    $("#nav-top #register").remove();
  } else {
    $("a[rel=login-box]").click(function(){
      if($("#"+$(this).attr("rel")).is(":visible")){
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#search-box").hide();
        $("#login-box").fadeOut("fast");
        $("#username").val("");
        $("#password").val("");
      } else if($("#search-box").is(":visible")) {
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#search-box").fadeOut("fast");
        $("#CAT_Search").val("");
        $("#nav-top").animate({"margin-top":65}, 400, function(){
          $("#login-box").fadeIn("fast");
          $("#username").focus();
        });
      } else if($("#cart-box").is(":visible")) {
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#cart-box").fadeOut("fast");
        $("#nav-top").animate({"margin-top":65}, 400, function(){
          $("#login-box").fadeIn("fast");
          $("#username").focus();
        });
      } else {
        $("#nav-top").animate({"margin-top":65}, 400, function(){
          $("#login-box").fadeIn("fast");
          $("#username").focus();
        });
      }
    });
    $("#forgot-link").click(function(){
      $("#login-form").hide();
      $("#forgot-form").show();
    });
    $("#back").click(function(){
      $("#login-form").show();
      $("#forgot-form").hide();
    });
  }
  
  // Search box
  $("a[rel=search-box]").click(function(){
      if($("#"+$(this).attr("rel")).is(":visible")){
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#search-box").fadeOut("fast");
        $("#CAT_Search").val("");
      } else if($("#login-box").is(":visible")) {
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#login-box").fadeOut("fast");
        $("#username").val("");
        $("#password").val("");
        $("#nav-top").animate({"margin-top":40}, 400, function(){
          $("#search-box").fadeIn("fast");
          $("#CAT_Search").focus();
        });
      } else if($("#cart-box").is(":visible")) {
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#cart-box").fadeOut("fast");
        $("#nav-top").animate({"margin-top":40}, 400, function(){
          $("#search-box").fadeIn("fast");
          $("#CAT_Search").focus();
        });
      } else {
        $("#nav-top").animate({"margin-top":40}, 400, function(){
          $("#search-box").fadeIn("fast");
          $("#CAT_Search").focus();
        });
      }
  });
  
  // Cart box
  $("a[rel=cart-box]").click(function(){
      if($("#"+$(this).attr("rel")).is(":visible")){
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#search-box").fadeOut("fast");
        $("#CAT_Search").val("");
      } else if($("#login-box").is(":visible")) {
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#login-box").fadeOut("fast");
        $("#username").val("");
        $("#password").val("");
        $("#nav-top").animate({"margin-top":40}, 400, function(){
          $("#cart-box").fadeIn("fast");
        });
      } else if($("#search-box").is(":visible")) {
        $("#nav-top").animate({"margin-top":0}, 400);
        $("#search-box").fadeOut("fast");
        $("#CAT_Search").val("");
        $("#nav-top").animate({"margin-top":40}, 400, function(){
          $("#cart-box").fadeIn("fast");
        });
      } else {
        $("#nav-top").animate({"margin-top":40}, 400, function(){
          $("#cart-box").fadeIn("fast");
        });
      }
  });
  
  /**
   * Main images on single pages
   */
  // Left text alignment
  if($(".main-image-left").length>0){
    $(".main-image-left").eq(0).insertBefore($("#content"));
    var imageTitle = $(".main-image-left").eq(0).attr("alt");
    var imageText = $(".main-image-left").eq(0).attr("longdesc");
    $(".main-image-left").eq(0).wrap('<div id="main-image-left"></div>');
    $("#main-image-left").prepend('<div id="main-image-text"><h2>'+imageTitle+'</h2><h4>'+imageText+'</h4>');
  }
  // Right text alignment
  if($(".main-image-right").length>0){
    $(".main-image-right").eq(0).insertBefore($("#content"));
    var imageTitle = $(".main-image-right").eq(0).attr("alt");
    var imageText = $(".main-image-right").eq(0).attr("longdesc");
    $(".main-image-right").eq(0).wrap('<div id="main-image-right"></div>');
    $("#main-image-right").prepend('<div id="main-image-text"><h2>'+imageTitle+'</h2><h4>'+imageText+'</h4>');
  }
  if($(".main-image").length>0){
    $(".main-image").eq(0).insertBefore($("#content"));
  }
  
  
  /**
   * Link categories below the articles to category listing pages
   */
  $("#categories li").eq(-1).addClass("last");
  if($("#categories").length>0){
    $("#categories a").each(function(){
      var text = $(this).html();
      var link = '<a href="'+$(this).attr("href")+'">'+text+'</a>';
      $("#posted-in").html($("#posted-in").html().replace(text, link));
    });
  }
  
  /**
   * Sidebar video player
   */
  $("#sidebar-player a").fadeTo("fast", 0.8).hover(function(){
    $(this).fadeTo("fast", 1);
  }).mouseout(function(){
    $(this).fadeTo("fast", 0.8);
  }).click(function(){
    $("#sidebar-player embed").show();
    $("#sidebar-player a").remove();
  });
  
  /**
   * Shopping cart and newsletter widgets
   */
  $("#open-cart").toggle(
    function(){$("#shopping-cart").animate({right: 0});},
    function(){$("#shopping-cart").animate({right: -232});}
    //function(){$("#shopping-cart").animate({right: -157});}
  );
  $("#open-newsletter").toggle(
    function(){$("#newsletter-widget").animate({right: 0})},
    function(){$("#newsletter-widget").animate({right: -157});}
  );
  
  /**
   * Labels in newsletter widget
   */
  $("#CaptchaV2").attr("title", "Enter Word Verification");
  $("#newsletter-widget input[type=text]").each(function(){
    if(!$(this).val()) $(this).val($(this).attr("title"));
    $(this).focus(function(){
      if($(this).val()==$(this).attr("title")) $(this).val("");
    }).blur(function(){
      if(!$(this).val()) $(this).val($(this).attr("title"));
    });
  });
  
});


