currentTab = 1;
$( document ).ready( function(){
    $('.tab_link' ).click(function () { 
        /*$(this).removeClass('act');
        currentTab = 
        if ( $( this ).attr( 'class' ) != 'active' ) {
            $( this ).parent().find( 'dt' ).removeClass( 'active' );
            $( this ).parent().find( 'dd' ).slideUp( 200 );
            
            $( this ).addClass( 'active' );
            $( this ).next( 'dd' ).slideDown( 200 );
        }*/
        $('#action_'+currentTab).hide();
        $('#tab_link_id_'+currentTab).removeClass('act');
        
        currentTab = ($(this).html());
        $('#tab_link_id_'+currentTab).addClass('act');
        $('#action_'+currentTab).show();
    });
    
    $('#select_orderby').change( function(){
        var type =  $('#select_orderby').val();
        var url = location.href;
        $.get(base_url+'products/setDefaultOrderBy/', {type:type, url:url}, function(){
            window.location = url;    
        });
        
    });
 
    $(".fancy_popup").fancybox({
        'transitionIn'        : 'none',
        'transitionOut'        : 'none',
        'type'                : 'ajax'
    });


    
});

// suggest user price for add
function suggest(id)
{
    $('#error_label').html('');
    var email = $('#suggest_email').val();
    var price = $('#suggest_price').val();  
    var bot = $('#form_bot').val();
    var phone = $('#suggest_phone').val();
    var no_errors = true;
    
    if(bot == 1)
    {
        bot = 2;
    }
    
    if(email == '' && phone == '')
    {
        $('#error_label').append('Laukelis "El. paštas" arba "Telefonas" turi būti užpildytas<br/>'); 
        no_errors = false;      
    }
    if(email != '' && !is_valid_email(email))
    {
        $('#error_label').append('Laukelis "El. paštas" užpildytas nekorektiškai<br/>');
        no_errors = false;   
    }
    if(price == '')
    {
        $('#error_label').append('Laukelis "Siūloma kaina" negali būti tuščias<br/>'); 
        no_errors = false;      
    }
    
    if(no_errors)
    {
        $.post(base_url+'ads/addSuggest/'+id,{bot:bot, email:email, price:price, phone:phone}, function(data){
            
            if(data)
            {
                $('.inside').addClass('good');
                $('.inside').html('Jūsų kainos siūlymas buvo priimtas sėkmingai');                 
            } else {
                $('.inside').addClass('bad');
                $('.inside').html('Atsiprašome įvyko klaida');                     
            }
            
        });
    }
}

// add user data for information about add price fall
function inform(id)
{
    $('#error_label').html('');
    var email = $('#inform_email').val();
    var bot = $('#form_bot').val();
    var phone = $('#inform_phone').val();
    var no_errors = true;
    
    if(bot == 1)
    {
        bot = 2;
    }
    
    if(email == '' && phone == '')
    {
        $('#error_label').append('Laukelis "El. paštas" arba "Telefonas" turi būti užpildytas<br/>'); 
        no_errors = false;      
    } 
    
    if(email != '' && !is_valid_email(email))
    {
        $('#error_label').append('Laukelis "El. paštas" užpildytas nekorektiškai<br/>');
        no_errors = false;   
    }
    
    if(no_errors)
    {
        $.post(base_url+'ads/addInform/'+id,{bot:bot, email:email, phone:phone}, function(data){
            
            if(data)
            {
                $('.inside').addClass('good');
                $('.inside').html('Jūs buvote sėkmingai užregistruotas');                 
            } else {
                $('.inside').addClass('bad');
                $('.inside').html('Atsiprašome įvyko klaida');                     
            }
            
        });
    }   
}

// add user data for information about add price fall
function recommend(id, url)
{
   
    $('#error_label').html('');
    var email = $('#recommend_email').val();
    var bot = $('#form_bot').val();
    var name = $('#recommend_name').val();
    var fname = $('#recommend_fname').val();
    var femail = $('#recommend_femail').val();    
    var no_errors = true;
    
    if(bot == 1)
    {
        bot = 2;
    }
    if(name == '')
    {
        $('#error_label').append('Laukelis "Jūsų vardas" turi būti užpildytas<br/>'); 
        no_errors = false;      
    }
    if(name != '' && is_alpha(name))
    {
        $('#error_label').append('Laukelyje "Jūsų vardas" įvesti blogi duomenys<br/>'); 
        no_errors = false;      
    }   
    if(email == '')
    {
        $('#error_label').append('Laukelis "Jūsų el. paštas" turi būti užpildytas<br/>'); 
        no_errors = false;      
    } 
    if(fname == '')
    {
        $('#error_label').append('Laukelis "Draugo vardas" turi būti užpildytas<br/>'); 
        no_errors = false;      
    }
    if(fname != '' && is_alpha(fname))
    {
        $('#error_label').append('Laukelyje "Draugo vardas" įvesti blogi duomenys<br/>'); 
        no_errors = false;      
    }  
    if(femail == '')
    {
        $('#error_label').append('Laukelis "Draugo el. paštas" turi būti užpildytas<br/>'); 
        no_errors = false;      
    } 
    
    if(email != '' && !is_valid_email(email))
    {
        $('#error_label').append('Laukelis "Jūsų el. paštas" užpildytas nekorektiškai<br/>');
        no_errors = false;   
    }
    
    if(femail != '' && !is_valid_email(femail))
    {
        $('#error_label').append('Laukelis "Draugo el. paštas" užpildytas nekorektiškai<br/>');
        no_errors = false;   
    }
    
    if(no_errors)
    {
        $.post(base_url+'ads/sendRecommendation/'+id+'/'+url,{bot:bot, email:email, name:name, femail:femail, fname:fname}, function(data){
            
            if(data)
            {
                $('.inside').addClass('good');
                $('.inside').html('Jūs laiškas buvo išsiųstas sėkmingai');                 
            } else {
                $('.inside').addClass('bad');
                $('.inside').html('Atsiprašome įvyko klaida');                     
            }
            
        });
    }   
}


function is_valid_email (email)
{
    return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}
// replace not digits to empty value
function AcceptDigits(obj)
{
    var exp = /[^\d]/g;
    obj.value = obj.value.replace(exp,'');
}
function is_alpha(string)
{
    return  /[^A-Za-zĄČĘĖĮŠŲŪŽąčęėįšųūž)\d ]/.test(string);
}
function remove_class_email(){
    $('#suggest_email').removeClass('required');
    $('#suggest_email').removeClass('error');  
    $('#suggest_phone').addClass('required');
}

// remove required, when change inform type 
function remove_class_phone(){
    $('#suggest_phone').removeClass('required');
    $('#suggest_phone').removeClass('error');
    $('#suggest_email').addClass('required');
}

function changePaymentType(value)
{
    $('#payment_type').val(value);
    var license_agree = $("input[name='license_check']:checked").val(); 
    if(parseInt(license_agree) == 10)
    {       
        $('#pay_form').submit();
    } else {
        $('#notice').show();
    }
}



function activeProductOver(currentId, imgActive)
{
    $('#status_img_'+currentId).attr( 'src', imgActive);
    
    
}

function activeProductOut(currentId, imgActive, imgOut)
{
    $('#status_img_'+currentId).attr( 'src', imgOut);
    
    $('#status_img_'+activeProduct).attr( 'src', imgActive);
}

function activeProductIn(currentId, imgOut, imgActive, ajaxUrl)
{
    imgUrl = 'http://www.pc-shop.lt/static/images/tmp/loading.gif';
    
    document.getElementById('product_content').innerHTML = '<div id="product_content"><div style="padding: 50px 0 0 0px; width: 306px; height: 500px"><img src="'+imgUrl+'" /></div></div>';

    $('#status_img_'+activeProduct).attr( 'src', imgOut);
    activeProduct = currentId;
    $('#status_img_'+activeProduct).attr( 'src', imgActive);

    $.ajax({
   type: "GET",
   url: ajaxUrl,
   success: function(msg){
   //    alert(msg);
   document.getElementById('product_content').innerHTML = msg;
     $('#product_content').html(msg);
   }
 });

}


function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}



/*function checkvals(formname)
{
    empty = false;
    
    for (i=0; i<fields.length; i++)
    {
        if (document.getElementById('form_'+fields[i]).value == "")
        {
            empty = true;
            document.getElementById('label_'+fields[i]).className = 'error';
        }
        else
        {
            document.getElementById('label_'+fields[i]).className = '';
        }
    }
    
    for (i=0; i<inputs.length; i++)
    {
        if (document.getElementById('form_'+inputs[i]).checked != true)
        {
            empty = true;
            document.getElementById('label_'+inputs[i]).className = 'error';
        }
        else
        {
            document.getElementById('label_'+inputs[i]).className = '';
        }
    }
    
    
    if (empty) setTimeout('clearVals()', 1000);
    else formname.submit();
}
         */

function clearVals()
{
    return; // kad nedingtu raudoni
    
    for (i=0; i<fields.length; i++)
    {
        if (document.getElementById('form_'+fields[i]).value == "")
        {
            document.getElementById('label_'+fields[i]).className = '';
        }
    }
    
    for (i=0; i<inputs.length; i++)
    {
        if (document.getElementById('form_'+inputs[i]).checked != true)
        {
            document.getElementById('label_'+inputs[i]).className = '';
        }
    }
}

var imagePath = '';

function changePhoto(i, imagePath)
    {
        
        $('.thumb').removeClass('thumbActive');
        $('#big_photo_img').attr('src', imagePath+'/'+i+'_main.jpg');
        $('#big_photo_link').attr('href', imagePath+'/'+i+'_big.jpg');
        
        $('#thumb_'+i).addClass('thumbActive');
        currentPhoto = i;
    }

$( document ).ready( function(){
    
    $('.thumb_link').mouseover(function() {
        $(this).parent().addClass('thumbHover');
    });

    $('.thumb_link').mouseout(function() {
        $(this).parent().removeClass('thumbHover');
    });
    
});


function changeType(type)
{
    if (type == 'j')
    {
        $('#label_name').html('Įmonės pavadinimas <span class="star">*</span>');
        $('#li_code').show();
        $('#form_vat_code').addClass('required');
        $('#form_code').addClass('required');
        $('#li_vat_code').show();
    }
    else
    {
        $('#label_name').html('Vardas pavardė <span class="star">*</span>');
        $('#li_code').hide();
        $('#form_vat_code').removeClass('required');  
        $('#form_code').removeClass('required');   
        $('#li_vat_code').hide();
    }
}
function sChangeType(type)
{
    if (type == 'j')
    {
        $('#s_label_name').html('Įmonės pavadinimas <span class="star">*</span>');
        $('#s_li_code').show();
        $('#s_form_vat_code').addClass('required');
        $('#s_form_code').addClass('required');
        $('#s_li_vat_code').show();
    }
    else
    {
        $('#s_label_name').html('Vardas pavardė <span class="star">*</span>');
        $('#s_li_code').hide();
        $('#s_form_vat_code').removeClass('required');  
        $('#s_form_code').removeClass('required');   
        $('#s_li_vat_code').hide();
    }
}
function selectModification(id)
{
    if(id > 0)
    {
        $('.green').each(function(){
            $(this).removeClass('selected_modification');    
        });
        $('#matrix_'+id).addClass('selected_modification');
        $.post(base_url+'products/getAjaxProductPhotos',{modificationId:id}, function(data){
            $('#product_photos').html(data);    
        });
    }
    selectedModification = id;
}

function addToCart()
{
    var id = selectedModification;
    
    if(id > 0)
    {
        window.location = base_url+'ideti_i_krepseli/'+id+'/1';
    } else {
        alert('Negalite įsidėti šio produkto ');
    }
}

function changeStore()
{
    var id = $('#form_store').val();
    $.post(base_url+'orders/ajaxGetStore', { storeId: id},function(data){
        $('#store_data').html(data);    
    });   
}
