$(window).bind("load", function() {
	$("div#mygalone").slideView(); //if leaved blank performs the default kind of animation (easeInOutExpo, 750)
	$("div#mygaltwo").slideView({
		easeFunc: "easeInOutBounce",
		easeTime: 2200,
		toolTip: true
	});
});

function slider() {
$("#mygalone").trigger('click'); // where #mygalone is the id of your images main div container
}

$(function() {
    $('.inp_search').focus(function(){
        if ( $(this).val() == 'Поиск...' ) $(this).val("");
    }).blur(function(){
        if ( $(this).val() == '' ) $(this).val("Поиск...");
    });
    $('.rowElem').jqTransform({imgPath:'images/'});
    $('#mycarousel').jcarousel({
        start: 1
    });
    $('#mycarousel2').jcarousel({
        start: 1
    });
    $('#bigimg a').fancybox({'overlayShow':false});
    $('.ajaxload').fancybox({'overlayShow':false});

    $('a.buy').fancybox({'overlayShow':false, onComplete:cartStatus });

    $('.admin_tbl tr').hover(function () { $(this).addClass('td_hover') }, function () { $(this).removeClass('td_hover') });
});

function changePrice ( price, price_old, percent ) {
    var html = '';
    if ( price_old ) html = '<u>'+price_old+' р.</u> ';
    html = html+price+' р.';
    if ( percent ) html = html+' ('+percent+'%)';
    $('#price_block').html(html);
}

function changeImg ( middle, width, height, bigimg ) {

    img = '<img src="' + middle + '" width="' + width + '" height="' + height + '" />';
    if ( bigimg ) img = '<a href="' + bigimg + '">' + img + '</a>';
    $('#bigimg').html( img );
    $('#bigimg a').fancybox({'overlayShow':false});
    return false;
}

function cartStatus () {
    $('#cart span').load('/cart/ajax/action:status/', null, function(){
        var tmp = $('#cart_status_total').html();
        var discount = parseInt($('#user_discount').html());
        $('#cart_total').html( tmp );

        if ( discount ) {
            var disc_sum = '';
            if ( tmp != null ) {
                var total = parseInt(tmp.replace('`',''));
                total = Math.ceil((total/100)*(100-discount));
                disc_sum = number_format(total, 0, '.', '`' )+' р.';
            }
            $('#cart_total_discount').html(disc_sum);
        }
    });
}

function refreshAdres () {
    $('#adres_list').load('/my/ajax/refresh_adres/');
    return false;
}
function addAdres () {
    window.open('/my/add_address/short:true/','','top=50, width=600, height=700, scrollbars=auto');
    return false;
}

function cartDelete ( item_id, param ) {
    id = '#cart_'+item_id+'_'+param;
    href = '/cart/ajax/action:update/item_id:' + item_id + '/param:' + param + '/number:0/'
    $.ajax({
        type: "GET",
        url: href,
        success: function(msg){
            $(id).hide('slow');
            cartStatus();
        }
    });
    return false;
}

function cartUpdate ( number, item_id, param, isAbsValue ) {
    var id = '#cart_'+item_id+'_'+param;
    var num = '#number_'+item_id+'_'+param;
    var new_number = number;
    if ( !isAbsValue ) new_number += parseInt($(num).html());

    href = '/cart/ajax/action:update/item_id:' + item_id + '/param:' + param + '/number:' + new_number + '/'
    $.ajax({
        type: "GET",
        url: href,
        success: function(msg){
            $(num).html(new_number);
            if ( new_number == 0 ) $(id).hide('slow');
            cartStatus();
        }
    });
    return false;
}

function askNumber ( item_id, param ) {
    var num = parseInt($('#number_'+item_id+'_'+param).html());
    var number = parseInt(prompt( "Введите желаемое количество:", num ));
    if ( isNaN(number) ) return false;
    else return cartUpdate( number, item_id, param, true );
}

function number_format( number, decimals, dec_point, thousands_sep ) {	// Format a number with grouped thousands
	//
	// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +	 bugfix by: Michael White (http://crestidg.com)

	var i, j, kw, kd, km, minus = "";

    if(number < 0){
        minus = "-";
        number = number*-1;
    }

	// input sanitation & defaults
	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	//kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");

	return minus + km + kw + kd;
}

function addparam() {
    var param = $('#addparam').val();
    if ( param.length ) {
        var href=$('#addparamlink').attr('href') + 'param:'+param+'/';
        $('#addparamlink').attr('href', href);
        return true;
    } else return false;
}

function phoneSwitch (what) {
    $('#msk,#spb,#rus').hide();
    $('#phonenavi a').removeClass('active');
    $('#'+what).show();
    $('#'+what+'_l').addClass('active');
    return false;
}

function trackForm ( id ) {
   var name = $('#'+id+'-name').val();
   var email = $('#'+id+'-email').val();
   var phone = $('#'+id+'-phone').val();
   var str = '';
   if ( name.length ) str += name;
   if ( phone.length ) str += ', tel: '+phone;
   if ( email.length ) str += ', mail: '+email;
   return str;
}
