/***
 * This file follows old conventions for javascript file structure. Please also
 * refer to public/javascripts/modcloth/views/storefront/products.show.js for product
 * the rest of the product details javascript code.
 * If you're up to it, please refactor into new structure.
 */
var modcloth = (function(mc, $){
  mc.products = $.extend({}, mc.products, {
    /*
     * update transparent pngs for IE6
    */
    pageLoaded : function(){
      if($.browser.msie&&parseFloat($.browser.version) < 7) {
        $('.btb-badge').attr('src', '/images/btb/btb-pick-badge.gif');
        $('.btb-badge.large').attr('src', '/images/btb/btb-detail-badge.gif');
      }
    },
    /*
     * run on document ready to check params for anchors
    */
    updatePageFromParams : function(){
      var view = mc.util.getUrlParameter('view');
      if(view=='reviews'){
        $('#reviews-tab').click(); // TODO: not sure why this is here
		//mantis 3091 - scrollTo takes an ID without a hash
        if ($('#reviews-container').length > 0) {
          mc.util.scrollTo('#reviews-container', 500);
        } else if ($('#commentWrapper').length > 0) {
          mc.util.scrollTo('#commentWrapper', 500);
        }
      }

      var comments = mc.util.getUrlParameter('comments')
      if(comments=='all'){
        // click on View All button
        $('#btb-viewall, #viewall').click();
      } else if(comments=='more') {
        $('#btb-viewmore, #viewmore').click();
      }
    }
  });

  return mc;
}(modcloth || {}, jQuery));

/* *************************************************************************************************************************** */
	function validatePopupCommentForm() {
		if($('#leave_comment #your-comment').val() == "" || $('#leave_comment #your-comment').val() == "Whatever you write, please keep it polite!"){
			$.jGrowl("Please enter a comment and submit", { life: 7000, theme: 'error_growl', header: 'Comment Error' });
			return false;
		}
		return true;
	}
/* *************************************************************************************************************************** */
	function validateCommentForm(){
		if($('#your-comment').val() == "" || $('#your-comment').val() == "Whatever you write, please keep it polite!"){
			$.jGrowl("Please enter a comment and submit", { life: 7000, theme: 'error_growl', header: 'Comment Error' });
			return false;
		}

		return true;
	}
/* *************************************************************************************************************************** */
// When the user add a new Review and change the Stars rating
  function starChange(num,el, rating_modifier){
	      document.getElementById(rating_modifier).style.width=num+'px';
        document.getElementById(el).value=(num/17);
    }
/* *************************************************************************************************************************** */
    function init_popups(){
        $('.request-login').fancyZoom({width:270});
    }
/* *************************************************************************************************************************** */
    function remove_voting_links(review_element_id){
        $(review_element_id).find('span.voting-links').remove()
    }
/* *************************************************************************************************************************** */
// Load the current reviews totals
function initiate_review() {

	if($('#slider').size()>0){
        $("#fit_rating").val(3);
		$("#slider").slider({
			value:2,
			min: 0,
			max: 4.2,
			step: 1,
			slide: function(event, ui) {
				$("#fit_rating").val(ui.value+1);
			}
		});
	}

	$('#your-review-product-review').click(function() {
	  if ($(this).html() == "Talk about the item here!")
	    $(this).html('').css({'color' : '#4d4d4d', 'font-style' : 'normal'});
	});
}
/* *************************************************************************************************************************** */
function loadStyle() {

	   support_frame = document.getElementById('support_frame');
	   if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	    support_frame.location.href = '/storefront/main/livesupport?advise=true';
	   } else {
	    support_frame.src = '/storefront/main/livesupport?advise=true';
	   }
	  }
/* *************************************************************************************************************************** */
// Load the coomming soon, preorder, out of stock notifications, enable the reviews...
function detailLoaded() {
  $('#total-review_count_showing').html('' + $('.user-review').size() + '')

  //New functionality for the twitter button being test in facebook_like branch
  $('a.twitter').click(function() {
    url = 'http://twitter.com/home?status=' + encoded_u + '?t_ref=two (ModCloth)';
    var twitterWindow = window.open(url, 'twitter', 'toolbar=0,status=0,width=800,height=650,scrollbars=1');
    twitterWindow.focus();
    return false;
  })

  $("#dialog").dialog({
    bgiframe: true,
    minHeight: 420,
    minWidth: 320,
    width: 320,
    maxWidth:320,
    dialogClass: 'your-review-dialog',
    autoOpen: false,
    resizable: false,
    open: function() {
      $('div.ui-widget-content').css('height', 'auto')
    }
  });

  // Load the current reviews totals
  initiate_review();
  initiate_text_area();

  // Tab click handler
  $('div#tabs a.tablink').click(function() {
    $('div#tabs a.tablink').removeClass('selected');
    $(this).addClass('selected');
    var showTab = $(this).attr('name');
    $('div#tabs div.tabcontent').hide();
    $(showTab).show();
    return false;
  });

  //Description tab is active
  $('#desc').show();
  $('#desc_tab').addClass('selected');

  /* Handles the resizing of the description box */
  //descHeight = $('#desc').height();
  winHeight = $(window).height();
  maxHeight = winHeight * 0.45;
//    $("#notification-popup").position({ my: "left top", at: "left bottom", of: "#variant-submit-button"});
  addToBagLocation = $('#product-purchase').offset();

  $('#love_it_not_logged').fancyZoom({width:270});
  $('#add_to_site_link').fancyZoom();
  $('#tell_a_friend_link').fancyZoom();
  //$('#notify_link').fancyZoom({width:300});
  $('#login-review-link-bottom').fancyZoom({width:270});
  $('#top-login-review-link').fancyZoom({width:270});
  $('.request-login').fancyZoom({width:270});
  $('#comp_fit').fancyZoom({width:300});

  // Handling Review Form
  $('#first_review_link').click(function() {
    if ($('#review-form-container').is(":hidden")) {
      $('#review-form-container').slideDown('slow')
      $('#review-form-container').parent('#submit-review').css('margin-bottom', '57px')
    }
  });

  $("div#ask-modstylist-popup").css("opacity", "0");

  if (document.location.hash == "#kaboodle") {
    document.location.hash = "#";
    share_kaboodle(document);
  }

  var share_using = gup('share_using')
  if (share_using == 'polyvore')
    share_polyvore(document);

  $("div#ask-modstylist-popup").css("display", "block");
  if ($.browser.msie) {
    $("div#ask-modstylist-popup").css("background", "url(/images/chat-modstylist-popup.gif) transparent");
  }

  $('#more-questions-link').fancyZoom({width:300});
  $('#ask-modcloth-link').fancyZoom({width:300});
  $('#add-to-site-link').fancyZoom();

  activate_sharethis_menu();

  $('ul#reviews-comments-tabs a').click(function() {
    $(this).addClass('selected');
    var showTab = $(this).attr('href');
    $('.reviews-comments-content').hide();
    $(showTab).show();
    return false;
  });

  var review_size = $('.user-review').size();
  $('#reviews-tab').click();

  var viewUserData = gup('view');
  if (viewUserData == "comments") {
    ;
  }
  if (viewUserData == "reviews") {
    $('#reviews-tab').click();
    //mantis 3096 - when no reviews, js throws error
    if ($('#reviews-container').length > 0) scrollTo('reviews-container', 500);
  }

  if (product.variants.length == 1)
    $('#notify_link_text').click();

}

