$(function(){
    if ($('.package_price').size() > 0 && $('.total_price').size()>0) {
//        var package_price = $('.package_price').html().replace(/<span>(.*)+<\/span>/, '');
        var package_price = parseInt($('.package_price').html());
        $('.total_price').text(package_price);
    }
     
    $('#top_nav ul li').click(function(){
        var class_name = "top_" + $(this).attr('rel');
        $('#inner_cont').html('').addClass('loading');
        $('#top_main_cont').attr('class','').addClass(class_name);
        if (class_name == 'top_contacts') {
           $('#inner_cont').load('tpl/_'+class_name+'.tpl.php',{},function(){
                   $('.loading').removeClass('loading');
            }).fadeIn();
        } else {
            $('#inner_cont').load('index.php',{doAjax:'top_info',template:class_name},function(){
                   $('.loading').removeClass('loading');
            }).fadeIn();
        }
//        setTimeout("$('.loading').removeClass('loading');", 3500);
    });
    
    $("form#fast_contact_form").submit(function(){
        sendMail("fast_contact_form");
        return false;
    });
    
    $('ul.modules_tabs li').click(function(){
        var rel = $(this).attr('rel');
        $('ul.modules_tabs li').removeClass('over');
        $('.data_block').hide();
        $('#'+rel).show();
        $(this).addClass('over');
    });
    $('ul.modules_tabs li').hover(function(){
        $(this).addClass('pointer');
    },function(){
        $(this).removeClass('pointer');
    });
    
    /* CHECKBOX actions*/
    $('.checkbox').click(function(){
        
        var total_price =parseInt($('.total_price').text());
        var chkBox = $(this).find('.design_type');
        var value = parseInt(chkBox.val());
        
        if (chkBox.attr('type') == 'radio') {
            var name = chkBox.attr('name');
            $("input[name='"+name+"'][type='radio']").each(function(){
                $(this).parent().removeClass('true');
                if($(this).is(":checked")) total_price -= parseInt($(this).val());
            });
//            total_price += parseInt(chkBox.val());
        }
        $(this).toggleClass('true');
        
        if ($(this).hasClass('true')){
            total_price += value;
            chkBox.attr('checked', "checked");
        } else {
            total_price -= value;
            chkBox.attr('checked', "");
        }
        $('.package_price').html(total_price+"<span>лв</span>");
        $('.total_price').text(total_price);
        
    });
    
    /* CAROUSEL*/
     $(".top_carousel").jCarouselLite({
        auto: 2500,
        speed: 1000
    });
});



//send ajax EMAIL CONTACT
function sendMail(form_id) {
var params = $("form#"+form_id).serialize();
	$.post("sendmail.php", params ,
		function (data) {
			show_error(data);
		}
	);
}

//send ajax EMAIL INQUERY
function sendSiteInquery(form_id) {
	var params = $("form#"+form_id).serialize();
	$.post("sendmail.php", params ,
		function (data) {
			show_error(data);
		}
	);
}

function show_error(msg){
    alert(msg);
}