function strpos(haystack, needle, offset) {
  var i = (haystack+'').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}



function show_rating(div_class){

             $("."+div_class).html('');
          //   $("#rating_result").html('');
             $("#"+div_class+"_result").html('');
      

             $("."+div_class).jRating({
                  length:5,
                  step:true,
                  rateMax: 5,
                  showRateInfo:false,
                  decimalLength:1,
                  phpPath: base_url+"ajax/ajax.php?action=rating",
                  onSuccess : function(resp){
                    $("#"+div_class+"_result").html(resp.rating+"/5").append('<a href="javascript:void(0)" class="clear_rate" onClick="show_rating(\''+div_class+'\');">change</a>');
                    $("#"+div_class+"_input").val(resp.rating);
                  } 
                });

        }
function review_vote(id,vote,current_rate) //vote=1 - positive , vote=2 - negative
{
    $.ajax({
       type: "POST",
       url: base_url + "ajax/review_votes.php",
       data: "review_id="+id+"&vote="+vote,
       success: function(){
           var rate = current_rate+1;
           if(vote==1){
                $("#up_number"+id).html("+" + rate);
                $("#rev_up"+id).addClass("up_clicked");
           }else if(vote==2){
                $("#down_number"+id).html("-" + rate);
                 $("#rev_down"+id).addClass("down_clicked");
           }
       }
    });

}
$(document).ready(function() 
{
        
        
        $('#add_review').click(function(e) {
              e.preventDefault();
              $('#myModal').reveal();
         }); 

        var currentURI = window.location.pathname;
	if (currentURI.search(/\/brand\//i) > 0)
	{
		currentURI = currentURI.slice(0, currentURI.search(/\/brand\//i));
	}
	var sort_form = $('form.pager');
	var sort_select = sort_form.find('select');
	
	var options, a;
	jQuery(function(){
	  options = { autoSubmit: true, serviceUrl:'/autocomplete.php', minChars: 3, width: 'auto', delimiter: /(,|;)\s*/ };
	  a = $('#search').autocomplete(options);
	});
	sort_select.bind('change',function()
	{
		if ($(this).attr('value') != '')
		{
			window.location = sort_form.attr('action')+'/sort/'+$(this).attr('value');
		}
		else
		{
			window.location = sort_form.attr('action');
		};
	});
	var brands_form = $('form.shopby');	
	var brands_select = brands_form.find('select');
	brands_select.bind('change',function()
	{
		if ($(this).attr('value') != '')
		{
			if (!isNaN($(this).attr('value')))
			{
				if (currentURI.indexOf('category') != '-1')
				{
					window.location = currentURI+'/brand/id/'+$(this).attr('value');
				}
				else
				{
					window.location = '/brands/id/'+$(this).attr('value');
				}
			}
			else
			{
				if (currentURI.indexOf('category') != '-1')
				{
					window.location = currentURI+'/brand/'+$(this).attr('value');
				}
				else
				{
					window.location = '/brands/'+$(this).attr('value');
				}
			};
		};
	});
    $('input[name^="s_"]').bind('keydown keyup', function() {
    	switch (this.name)
    	{
    	case 's_firstname':
    		$('input[name="b_firstname"]').val(this.value);
    		break;
        case 's_lastname':
    		$('input[name="b_lastname"]').val(this.value);
    		break;
    	case 's_email':
    		$('input[name="b_email"]').val(this.value);
    		break;
    	case 's_address':
    		$('input[name="b_address"]').val(this.value);
    		break;
    	case 's_city':
    		$('input[name="b_city"]').val(this.value);
    		break;
    	case 's_town':
    		$('input[name="b_town"]').val(this.value);
    		break;
	    case 's_postal':
	    	$('input[name="b_postal"]').val(this.value);
	    	break;
	    case 's_phone':
	    	$('input[name="b_phone"]').val(this.value);
	    	break;
		}
    });
    $('select[name^="s_"]').bind('change', function() {
    	switch (this.name)
    	{
	    	case 's_gender':
	    		$('select[name="b_gender"]').val(this.value).select();
	    		break;
	    	case 's_country':
	    		$('select[name="b_country"]').val(this.value).select();
	    		$.ajax({
                           type: "POST",
                           url: base_url + "order.php",
                           data: "action=countryMethods&country_id="+this.value,
                           dataType: 'json',
                           success: function(responce){
                                        $("#no_shipping_message").css('visibility',"hidden");
                                         $("#no_shipping_message").css('display',"none");
                                        $('#deliveryMethod option').remove();
                                        $.each(responce, function(id, name)
                                        {
                                             $('#deliveryMethod').append($("<option></option>").attr("value",id).text(name));
                                        })
                                         $("#choose_method").css("display","block");
                                },
                           error: function(responce){
                                        $('#deliveryMethod option').remove();
                                        $('#deliveryMethod').append($("<option></option>").attr("value", '').text($('#nomethod').val()));
                                        $("#no_shipping_message").css('display',"block"); 
                                        $("#no_shipping_message").css('visibility',"visible");
                                         
                                        $("#choose_method").css("display","none");
                           }
                         });	
	    		break;
		}
    });
    $('a[href="#top"]').click(function() {
		jQuery('html,body').animate({scrollTop: 0}, 500);
		return false;
	});		
	$( "#left" ).imageScroller( {} );

    
   

     
    

});

	
		//$(function () {
			
			
			//$( "#right" ).imageScroller( {speed:'4000', direction:'right'} );
			
		//	$( "#top" ).imageScroller( {direction:'top'} );

			//$( "#bottom" ).imageScroller( {speed:'3500', direction:'bottom'} );
		//})  
	 
