$(function(){
      $('#loading').ajaxStart(function(){
          $(this).show();
      }).ajaxStop(function(){
          $(this).hide();
      });
        
        
        $("ul.colorconttop li a[title='Open'], a.no_resolved_q").click(function(e){
            $('ul.colorconttop li a').addClass('firstlia');
            $("ul.colorconttop a[title='Resolved']").removeClass();
            $("#open_questions").show();
            $("#resolved_questions").hide();
        });

        $("ul.colorconttop li a[title='Resolved'], a.no_open_q").click(function(e){
            $('ul.colorconttop li a').addClass('firstlia');
            $("ul.colorconttop a[title='Open']").removeClass();
            $("#open_questions").hide();
            $("#resolved_questions").show();
        });      
     
      $('.talkbacks').children(':gt(3)').hide();
    


      
      
      $('input:text:not(.donterase), input:password:not(.donterase)').live('focus', function() {
            if(this.value==this.defaultValue){this.value=''}
      }).live('blur', function(){
            if(!this.value){this.value=this.defaultValue;}
      });
     
      $('.search-panel ul a:not(#answer-tab_form a)').live('click',function(e){
         e.preventDefault();
         $(this).parent().addClass('selected');
         $(this).parent().siblings().removeClass('selected');
         form_id = $(this).parent().attr('class').split(' ',1) + '_form';
         
         
         if( typeof prev_form_id == 'undefined' ) {
        	 prev_form_id = "search-tab_form";
         }

         if(prev_form_id != form_id)
         {
        	 $('#top_search_bar').children().hide();
        	 $('#top_search_bar #' + form_id).show();
         }
         prev_form_id = form_id;
      });
      
      $('#home_content_tabs a').click(function(e){
         e.preventDefault();
         $(this).parent().addClass('active');
         $(this).parent().siblings().removeClass('active');
         content_id = $(this).parent().attr('class').split(' ',1);
         $('#tabs_content').children().hide();
         $('#tabs_content #' + content_id).show();;
      });
      
      /******* CODE FOR CATEGORY TOOLTIP ********/
      $('.categories ul li div.tooltip-wrap').hide();
      
      /*
      $('.categories ul li a.parent_category.qanda.open').click(function(e) {
            e.preventDefault();
            if ($(this).next('.submenu').css('display') == 'none')
            {
                  $(this).addClass('open');
                  $(this).next().show();                  
            }
            
            else
            {
              $(this).removeClass('open');
              $(this).next().hide();                  
            }
      });
		*/
      //$('.categories ul li a:not(.open)').css('color', 'red');
       /* :not(.open) */
      
      $('.categories ul li a:not(.open)').live('mouseenter', function(){
            //alert('hello');
            $(this).parent().find('div.tooltip-wrap').show();
            $(this).parent().find('div.tooltip-wrap-small').show();
      });
      
      $('.categories ul li').mouseleave(function(){
            $(this).parent().find('div.tooltip-wrap').hide();
            $(this).parent().find('div.tooltip-wrap-small').hide();
      });
      
      $('.categories ul li div.tooltip-wrap-small').hide();

      /******* ENDING CODE FOR CATEGORY TOOLTIP ********/

      /******* CODE FOR ADD QUESTION CHOOOSE CATEGORY ********/
      $('#go_to1 input').click(function(e) {
    	  var cat_radios = $('#go_to2 input');
    	  for (var i = 0; i < cat_radios.length ; i++)
    	  {
    		  cat_radios[i].checked = false;
    	  }
      });
      
      $('#go_to2 input').click(function(e) {
    	  var cat_radios =  $('#go_to1 input');
    	  for (var i = 0; i < cat_radios.length ; i++)
    	  {
    		  cat_radios[i].checked = false;
    	  }
      });
      
      $('#go_to_tab1').live('click', function(){ 
    	  $('#go_to1').show();
          $('#go_to2').hide();
          $('#go_to_tab1').addClass('tohe_selected');
          $('#go_to_tab2').removeClass('tohe_selected');
      });
      
      $('#go_to_tab2').live('click', function(){ 
    	  $('#go_to2').show();
          $('#go_to1').hide();
          $('#go_to_tab2').addClass('tohe_selected');
          $('#go_to_tab1').removeClass('tohe_selected');
      });
      
      $('#go_to_tab1_link').live('click', function(){ 
    	  $('#go_to2').show();
          $('#go_to1').hide();
          $('#go_to_tab2').addClass('tohe_selected');
          $('#go_to_tab1').removeClass('tohe_selected');
      });
      /******* ENDING CODE FOR ADD QUESTION CHOOOSE CATEGORY ********/

      /*********** CODE FOR RECENT Q&A ON HOME PAGE ***********/
      $('#qanda .closed_questions').hide();
      $('.answer-tabs .recent-q a').click(function(e) {
            e.preventDefault();
            $('#qanda .closed_questions').hide();
            $('#qanda .open_questions').show();
            $('.answer-tabs .recent-q').addClass('active');
            $('.answer-tabs .recent-ans').removeClass('active');
      });
      
      $('.answer-tabs .recent-ans a').click(function(e) {
            e.preventDefault();
            $('#qanda .closed_questions').show();
            $('#qanda .open_questions').hide();
            $('.answer-tabs .recent-ans').addClass('active');
            $('.answer-tabs .recent-q').removeClass('active');
      });      
      /********* END CODE FOR RECENT Q&A ON HOME PAGE**********/
      
      
      
      
      // TODO: make this code shorter... it's jquery after all
      
		/****** STARTING SINGLE GENERALIZED CODE TO HANDLE ALL THE DIFFERENT BUTTON MODES ******/
		$('.button-with-modes').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -100px'})
		}) 
		$('.button-with-modes').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.button-with-modes').mousedown(function(){
			$(this).css({backgroundPosition:'0px -200px'})
		})
		
		$('.button-with-modes').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})


		$('.refinesearch-button input').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.refinesearch-button input').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.refinesearch-button input').mousedown(function(){
			$(this).css({backgroundPosition:'0px -61px'})
		})

		$('.refinesearch-button input').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})



		$('.subaccount').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		}) 
		$('.subaccount').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.subaccount').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		
		$('.subaccount').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		$('.btnsub').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		}) 
		$('.btnsub').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.btnsub').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		
		$('.btnsub').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		$('.btnedit').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		}) 
		$('.btnedit').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.btnedit').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		
		$('.btnedit').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		$('.subaccount2').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -29px'})
		}) 
		$('.subaccount2').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.subaccount2').mousedown(function(){
			$(this).css({backgroundPosition:'0px -58px'})
		})
		
		$('.subaccount2').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		
		
		$('.btnprevious').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		}) 
		$('.btnprevious').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.btnprevious').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		
		$('.btnprevious').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		
		
		
		$('.btnrreview').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		}) 
		$('.btnrreview').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.btnrreview').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		
		$('.btnrreview').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		$('.raksub').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		}) 
		$('.raksub').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.raksub').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		
		$('.raksub').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		
		$('.signin_login').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		}) 
		$('.signin_login').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.signin_login').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		
		$('.signin_login').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
		$('.key_btn').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -29px'})
		}) 
		$('.key_btn').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		}) 
		
		$('.key_btn').mousedown(function(){
			$(this).css({backgroundPosition:'0px -58px'})
		})
		
		$('.key_btn').mouseup(function(){
			$(this).css({backgroundPosition:'0px 0px'})	
		})
                $('.see_store_btn1').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.see_store_btn1').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.see_store_btn1').mousedown(function(){
			$(this).css({backgroundPosition:'0px -60px'})
		})

		$('.see_store_btn1').mouseup(function(){
			$(this).css({backgroundPosition:'0px -90px'})
		})
                $('.see_store_btn2').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.see_store_btn2').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.see_store_btn2').mousedown(function(){
			$(this).css({backgroundPosition:'0px -60px'})
		})

		$('.see_store_btn2').mouseup(function(){
			$(this).css({backgroundPosition:'0px -90px'})
		})
                $('.revstorebtn').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.revstorebtn').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.revstorebtn').mousedown(function(){
			$(this).css({backgroundPosition:'0px -60px'})
		})

		$('.revstorebtn').mouseup(function(){
			$(this).css({backgroundPosition:'0px -90px'})
		})
                $('.storeinforevbtn').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.storeinforevbtn').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.storeinforevbtn').mousedown(function(){
			$(this).css({backgroundPosition:'0px -60px'})
		})

		$('.storeinforevbtn').mouseup(function(){
			$(this).css({backgroundPosition:'0px -90px'})
		})
                $('.storeprevsearchbtn').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.storeprevsearchbtn').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.storeprevsearchbtn').mousedown(function(){
			$(this).css({backgroundPosition:'0px -60px'})
		})

		$('.storeprevsearchbtn').mouseup(function(){
			$(this).css({backgroundPosition:'0px -90px'})
		})

                $('.storeprevshowhbtn').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.storeprevshowhbtn').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.storeprevshowhbtn').mousedown(function(){
			$(this).css({backgroundPosition:'0px -60px'})
		})

		$('.storeprevshowhbtn').mouseup(function(){
			$(this).css({backgroundPosition:'0px -90px'})
		})
                 $('.check_accountpagergt').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -30px'})
		})
		$('.check_accountpagergt').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.check_accountpagergt').mousedown(function(){
			$(this).css({backgroundPosition:'0px -60px'})
		})

		$('.check_accountpagergt').mouseup(function(){
			$(this).css({backgroundPosition:'0px -90px'})
		})

            $('#add_continue').find('input').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		})
		$('#add_continue').find('input').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('#add_continue').find('input').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})

		$('#add_continue').find('input').mouseup(function(){
			$(this).css({backgroundPosition:'0px -96px'})
		})

             $('.btnrreview input').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		})
		$('.btnrreview input').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.btnrreview input').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})
		$('.btnrreview input').mouseup(function(){
			$(this).css({backgroundPosition:'0px -96px'})
		})

            $('.check_accountpagergt input').mouseenter(function(){
			$(this).css({backgroundPosition:'0px -32px'})
		})
		$('.check_accountpagergt input').mouseleave(function(){
			$(this).css({backgroundPosition:'0px 0px'})
		})

		$('.check_accountpagergt input').mousedown(function(){
			$(this).css({backgroundPosition:'0px -64px'})
		})

		$('.check_accountpagergt input').mouseup(function(){
			$(this).css({backgroundPosition:'0px -96px'})
		})
		
		/****** ENDING SINGLE GENERALIZED CODE TO HANDLE ALL THE DIFFERENT BUTTON MODES ******/
		
        $('#store_review_login').hide();
		$('#add_question_login').hide();
      
      $('#article_email .sendbtn input').live("click",function(e){
            var email_filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
            var email_to = $('#article_email_to_email').val();
            var email_from = $('#article_email_from_email').val();
            var errors = [];
            errors['article_email_to_email'] = [];
            errors['article_email_from_email'] = [];
            
            if (email_to == '')
            {
                errors['article_email_to_email'].push('This field is required');
            }
            else if (!(email_filter.test(email_to)))
            {
                errors['article_email_to_email'].push('Please enter a valid email address');
            }
            if (email_from == '')
            {
               
                errors['article_email_from_email'].push('This field is required');
            }

            if ( errors['article_email_to_email'].length > 0 ||  errors['article_email_from_email'].length > 0)
            {

                for (key in errors)
                {
                     var id = '#' + key;
                     var label_id = key + '_error';

                     for (inner_key=0; inner_key<errors[key].length;inner_key++)
                     {
                         var error = '<label id="' + label_id + '" class="error">' + errors[key][inner_key] + '</label>';
                         if ($('#' + label_id).length == 0)
                         {
                             $(error).insertAfter(id);
                         }
                         else
                         {
                            $('label.error').remove();
                            $(error).insertAfter(id);
                         }

                     }
                }
            }
            
            else
            {
                $('label.error').remove();
                var url = "/articles/" + $('#article_email_article_id').val() + "/update/send_email/1";
                $('#send_article_wrapper').load(url, 
                            {'to_email' : $('#article_email_to_email').val(),  
                            'from_email' : $('#article_email_from_email').val(),
                            'notes' : $('#notes').val(),
                            'id' : $('#article_email_article_id').val()
                      }, function(){
                    setTimeout(function(){
                        $('#send_article_window').hide();
                        var url = "/articles/" + $('#article_id').val() + "/update/refresh_form/1";
                        $.post(url, function(data) {
                            $('#send_article_wrapper').html(data);
                        });
                    }, 2700);
                });
            }
            return false;
      });
      
      
      

      
      $('#close_article_sent').live("click", function(e){
            e.preventDefault();
           $('#send_article_window').hide();
      });
      
      $('#close_article_thank_you').live("click", function(e){
          e.preventDefault();
          $('#send_article_window').hide();
      });
      
      $('form.search_scope input').click(function(e){
            this.form.submit();
      });
      $('form.search_scope select').change(function(e){
            this.form.submit();
      });
      

});

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}

function node_rate(node_type, node_id, field_name)
{
    $(function(){
          $("#"+field_name+"s").stars({
                cancelShow: false,
                oneVoteOnly: true,
                
                callback: function(ui, type, value)
                {
                      $("#"+field_name+"_msg").text("Saving...").fadeOut(500);
                      $.post("/" + node_type + "/" + node_id + "/rate", {rate: value},function(data){
                            $("#"+field_name+"_msg").text("Thank you - rated " + value).fadeIn(1000); 
                      });
                }
          });
    });
}

function get_articles(article_type,category)
{
    if (category!=0)
    {
        $('#recent_articles').load('/articles/'+category+'/'+article_type);
    }
    else
    {
        $('#recent_articles').load('/articles/'+article_type);
    }
}

function show_all_talkbacks()
{
     $('.talkbacks').children().show();
}

function show_member_form()
{
      $('#store_review_register').hide();
      $('#store_review_login').show();
      	
      //for add question
      $('#add_question_register').hide();
      $('#add_question_login').show();     
}

function show_register_form()
{
      $('#store_review_register').show();
      $('#store_review_login').hide();
      
      //for add question
      $('#add_question_register').show();
      $('#add_question_login').hide();
}

function print_article()
{
	my_window= window.open ("","", "status=1, outerWidth=1, outerHeight=1");
	my_window.document.write(document.getElementById("full_article").innerHTML);  
	my_window.print();
	my_window.close();
}

function load_basic_compare()
{
    $('#compare').load("/main/index/basic_compare/1");
}

function load_advanced_compare()
{
    $('#compare').load("/main/index/advanced_compare/1");
}


function captcha_reload()
{
    document.getElementById('siimage').src = '/securimage_show.php?sid=' + Math.random();
}

function text_resize(el)
{
    var ourText = $('.articlemaincont');
    var currFontSize = ourText.css('font-size');
    var finalNum = parseFloat(currFontSize, 10);
    if(el.id == 'larger_text') {
          finalNum *= 1.2;
    }
    else if (el.id == 'smaller_text'){
          finalNum /=1.2;
    }
    if(finalNum < 25 && finalNum > 10)
    {
          ourText.css('font-size', finalNum);
          ourText.children().css('font-size', finalNum);
          document.cookie = ['font_size', '=', encodeURIComponent(finalNum)].join('');
    }
}


function search_panel_switch(el)
{
    $(el).live('click',function(e){
       e.preventDefault();
       $(el).parent().addClass('selected');
       $(el).parent().siblings().removeClass('selected');
       form_id = $(el).parent().attr('class').split(' ',1) + '_form';
       
       
       if( typeof prev_form_id == 'undefined' ) {
           prev_form_id = "search-tab_form";
       }
    
       if(prev_form_id != form_id)
       {
           $('#top_search_bar').children().hide();
           $('#top_search_bar #' + form_id).show();
       }
       prev_form_id = form_id;
    });
}

function compare_products_reset()
{
    $('#dimaond_compare_shape').val('Round');
    $('#dimaond_compare_price_min').val('0');
    $('#dimaond_compare_price_max').val('2,000,000');
    $('#dimaond_compare_cut_min').val('Fair');
    $('#dimaond_compare_cut_max').val('Ideal');
    $('#dimaond_compare_color_min').val('M');
    $('#dimaond_compare_color_max').val('D');
    $('#dimaond_compare_clarity_min').val('I2');
    $('#dimaond_compare_clarity_max').val('IF');
    $('#dimaond_compare_carat_min').val('0.1');
    $('#dimaond_compare_carat_max').val('6');
    $('#dimaond_compare_depth_min').val('50');
    $('#dimaond_compare_depth_max').val('80');
    $('#dimaond_compare_table_min').val('50');
    $('#dimaond_compare_table_max').val('80');
    $('#dimaond_compare').submit();

}

$(document).ready(function(){ 
	$('.add_question_categories_parents').live('click', function(){
		$('.add_question_categories_sub2').html("");
		$('.add_question_categories_sub1').load('/questions/'+ this.children[0].value +'/add_step2/ajax/sub1');
		this.children[0].checked = true;
	   }); 
	
	$('.add_question_categories_sub').live('click', function(){ 
		$('.add_question_categories_sub2').load('/questions/'+ this.value +'/add_step2/ajax/sub2');
		this.children[0].checked = true;
	   });

$(".search-form input").focus(function(){
              $(this).addClass("colorchange");
});


 $('#dimaond_compare').submit(function(e){
   e.preventDefault();
   $.get('/products/index',$(this).serialize(), function(data) {
       $('#compare_results').html(data);
   });
 });

}); 



