jQuery.noConflict();

jQuery(document).ready(function() {

    jQuery(document).bind("contextmenu", function(e) {
        alert('Visas tiesības aizsargātas, sazināties ar seline.lv!');
        return false;
    });


    jQuery("#catalog_menu").toggle(
        function() {
           jQuery("#products_menu_list").slideDown('slow');
        },
        function () {
           jQuery("#products_menu_list").slideUp('slow');
        }
    );

    jQuery(".product_pic img").hover(function(){
        jQuery(this).animate({
            opacity: 0.70
        }, 500);
    });

    jQuery(".product_pic img").mouseout(function(){
         jQuery(this).animate({
            opacity: 1
        }, 500);
    });
	
	jQuery('#search_form input').keyup(function(e) {
		if(e.keyCode == 13) {
			doSearch();
		}
	});


})


function submitSmallLogin() {
    var dataString = jQuery('#loginForm').serialize();
    //alert(dataString);
    jQuery('#login').hide();
    jQuery('#login_loading').show();
        jQuery.ajax({
            type:'post',
            data: dataString,
            url:'/'+language+'/login/small/',
            success: function (data) {
                 //jQuery("#login").html(data);
                 jQuery('#login_loading').hide();
                 jQuery('#login').show();
                 //alert(data);
                 jQuery('#login').append(data);
              }
            }
        );
}

function submitLargeLogin() {
    var dataString = jQuery('#loginLargeForm').serialize();
    //jQuery('#login_small').hide();
    jQuery('#login_loading_circle').show();
        jQuery.ajax({
            type:'post',
            data: dataString,
            url:'/'+language+'/login/large',
            success: function (data) {
                 jQuery(".content").html(data);
                 jQuery('#login_loading_circle').hide();
                 //jQuery('#login_').show();
              }
            }
        );
}

function passwordReset() {
   var dataString = jQuery('#passwordResetForm').serialize();
    //jQuery('#login_small').hide();
    jQuery('#login_loading').show();
        jQuery.ajax({
            type:'post',
            data: dataString,
            dataType: 'json',
            url:'/'+language+'/login/password_reset/',
            success: function (data) {
                 //jQuery("#login").html(data);
                 jQuery('#login_loading').hide();
                 //jQuery('#login').show();
                 if (data.success==1) {
                    jQuery("#login").html(data.form_data);
                 }
                 alert(data.message);
              }
            }
        );
}

function chooseSize(size) {
    jQuery("#size").val(size);
    jQuery("#product-size-table a").removeClass('active');
    jQuery("."+size).addClass('active');
}

function chooseColor(color) {
    jQuery("#color").css({
        'background':'#'+color
    });
    jQuery("#color_hint").val(color);
    jQuery("#product-color-table a").removeClass('active');
    jQuery("."+color).addClass('active');
}

function addToCart(check) {
  if (check==1) {
    if (jQuery('#size').val()=='') {
        alert('Lūdzu izvēlieties izmēru!');
        return false;
    }
  }
  else if (check==2) {
    if (jQuery('#color_hint').val()=='') {
        alert('Lūdzu izvēlieties krāsu!');
        return false;
    }
  }
  else if (check==4) {
    if (jQuery('#color_hint').val()=='') {
        alert('Lūdzu izvēlieties krāsu!');
        return false;
    }
    if (jQuery('#size').val()=='') {
        alert('Lūdzu izvēlieties izmēru!');
        return false;
    }
  }
   var dataString = jQuery('#product_form').serialize();
        jQuery('a.add-to-cart span.add').hide();
        jQuery('a.add-to-cart span.added').fadeIn('slow', function(){
            jQuery('a.add-to-cart span.added').delay(900).hide();
            jQuery('a.add-to-cart span.add').fadeIn('slow');
        });

        jQuery.ajax({
            type:'post',
            data: dataString,
            url:'/'+language+'/cart/add/',
            success: function (data) {
                 jQuery('#cart_small').html(data);
              }
            }
        );
}

function deleteFromCart(item) {
   var dataString = jQuery('#cart_form').serialize();
   var in_cart;
   var count_in_table;
   var price_sum;
   var current_item_price;
   var total_count;
   //alert('/'+language+'/cart/delete/'+item+'/');
        jQuery.ajax({
            type:'post',
            data: dataString,
            url:'/'+language+'/cart/delete/'+item+'/',
            success: function (data) {
                 count_in_table = jQuery('#'+item+' td.count').html();
                 in_cart = jQuery('.cart_info span').html();
                 price_sum = jQuery(".all td.sum span").html();
                 price_sum = price_sum.replace(",", ".");
                 price_sum = parseFloat(price_sum);
                 current_item_price = jQuery('#'+item+' td.price').html();
                 current_item_price = current_item_price.replace(",", ".");
                 current_item_price = parseFloat(current_item_price);

                 in_cart = in_cart-1;
                 if (in_cart<1) {
                    in_cart = '0';
                    jQuery('#cart_details').slideUp('slow');
                    jQuery('#info_message').slideDown('slow');
                 }
                 if (count_in_table>=1) {
                    count_in_table -= 1;
                    total_count -= 1;
                    if (count_in_table==0) {
                      jQuery("#"+item).fadeOut('slow');
                    }
                    price_sum -= current_item_price;
                    price_sum = sprintf("%01.2f",price_sum);
                    var total = price_sum.replace(".",",");
                    jQuery('.all td.sum span').html(total);
                    jQuery('#'+item+' td.count').html(count_in_table);
                    //jQuery('#cart_table').attr('class',total_count);
                 }

                 jQuery('.cart_info span').html(in_cart);

				 location.reload();

              }
            }
   );
}

function submitForm(form) {
   jQuery("#"+form).submit();
}

function makeOrder() {
   /*var dataString = jQuery('#cart_form').serialize();
   var h = jQuery('#order_cart_form').height();
   jQuery('#order_cart_form').height(h);
   jQuery('#order_cart_form').html('');
   jQuery('.ajax-loader').show();
        jQuery.ajax({
            type:'post',
            data: dataString,
            url:'/'+language+'/cart/order/',
            success: function (data) {
                 jQuery('#order_cart_form').html(data);
                 jQuery('.ajax-loader').hide();
                 jQuery('#order_cart_form').height('');
              }
            }
        );*/
}

function vote(id) {
   jQuery.post('/'+language+'/competition/vote/', { id:id }, function(data) {
       jQuery("a#"+id).parent().html(data);
   });
}

function deliveryType() {
    var selected_id = jQuery('#delivery_type').val();
	//var sum_finally = jQuery('.sum_finally').html();
	//var sum_string = jQuery(".sum span").html();
	var selected_string = jQuery('#delivery_type :selected').html();
	
	/*sum_finally = sum_finally.replace(",", ".");
	sum_finally = parseFloat(sum_finally);*/
	
	jQuery.post('/'+language+'/cart/delivery_type/', { id:selected_id }, function(data) {
        jQuery('.delivery_price').fadeOut('slow', function(){
         	jQuery('.delivery_price').html(data);
    	})
    	jQuery('.delivery_price').fadeIn('slow');
   	});
	
}

function saveProfile() {
	var postData = jQuery("#thisForm").serialize();
    jQuery("#login_ajax_loader").show();
    jQuery.post('/'+language+'/profile/save/', postData, function(data) {
       jQuery(".message").html(data);
       jQuery("#login_ajax_loader").hide();
		//alert(data);
    });
}


function registrationType() {
	var selected = jQuery("#type :selected").val();
	jQuery.post('/'+language+'/registration/choose_type/', {type : selected}, function(data) {
       jQuery(".content").html(data);
       //jQuery("#login_ajax_loader").hide();
		//alert(data);
    });
}

function register() {
	var postData = jQuery("#thisForm").serialize();
    jQuery.post('/'+language+'/registration/save/', postData, function(data) {
       jQuery(".message").html(data);
    });
}

function changeInPageValue() {
	var per_page = jQuery("#per_page :selected").val();
	var host = window.location.host;
	var path = window.location.pathname;
	var search = window.location.search;
	if (search!='') {
		search = search + '&';
	}
	else search = '?';

	var url = 'http://'+host+path+search+'limit='+per_page;
	window.location.replace(url);
	//alert(url);
}

function orderBy(arg) {
       var url_arg = jQuery("#"+arg+" :selected").val();
       var host = window.location.host;
       var path = window.location.pathname;
       var search = window.location.search;
	
       search = search.replace('&order_price=asc','');
       search = search.replace('&order_price=desc','');
       search = search.replace('order_price=asc&','');
       search = search.replace('order_price=desc&','');
       search = search.replace('?order_price=asc','');
       search = search.replace('?order_price=desc','');
      
       if (search!='') {
            search = search + '&';
       }
       else {
            search = '?';
       }
       search += arg+'='+url_arg;
        //}
       var url = 'http://'+host+path+search;
       window.location.replace(url);
	//alert(url);
}

function doSearch() {
	var getData = jQuery("#search_form").serialize();
	/*jQuery.post('/'+language+'/registration/choose_type/', {type : selected}, function(data) {
		alert(data);
    });*/
	var host = window.location.host;
	var url = 'http://'+host+'/'+language+'/products/?'+getData;
	window.location.replace(url);
	//alert(getData);
	//alert(host);
}

function showComeFromOtherInput() {
	jQuery('#come_from_other').fadeIn('slow');
}

function bannerClick(id_banner) {
	jQuery.post('/banner_click.php', {id_banner : id_banner}, function(data) {
       //jQuery("#login_ajax_loader").hide();
		alert(data);
    });
}

function changePassword() {
    var getData = jQuery("#changePass").serialize();
    jQuery.post('/'+language+'/profile/change_pass/', getData, function(data) {
       jQuery("#messages").html(data);
        //alert(data);
    });
}

function resetPassword() {
    //var getData = jQuery("#resetPass").serialize();
    jQuery.get('/'+language+'/login/password_reset/', {type : 'resetPass'}, function(data) {
       jQuery("#login").html(data);
        //alert(data);
    });
}








