jQuery.noConflict();
jQuery(document).ready(function(){

    var baseprice;
    var addprice;
    var roofprice;
    
    baseprice = getBasePrice();
    addprice = getAddPrice();
    roofprice = getRoofPrice();
	
	barprice = getBarPrice();
    
    // alert(baseprice);
    // alert(addprice);
    // alert(roofprice);
    
    setTotalPrice();
    //-----------------------------------------------------------------------------
    // работа с ценой на странице проекта
    //
    function getPrice(price){
        if (price) 
            return parseInt(price.replace(/\./g, ""));
        else 
            return 0;
    }
    function getBasePrice(){
        var price = jQuery("#totalprice").text();
        if (price) 
            return parseInt(price.replace(/\./g, ""));
        else 
            return 0;
    }
    function getAddPrice(){
        var price = 0;
        
        jQuery.each(jQuery(".addoption"), function(key, item){
            if (jQuery(item).attr('checked') == true) 
                price = price + getPrice(jQuery(item).val());
        });
        
        return price;
    }
    function getRoofPrice(){
        var price = 0;
        
        jQuery.each(jQuery(".roofreplacement"), function(key, item){
            if (jQuery(item).attr('checked') == true) 
                price = price + getPrice(jQuery(item).attr("title"));
        });
        
        return price;
    }
	
	// -- Цены за брус
	function getBarPrice(){
		

		// -- Ищем чекбокс замены бруса
		var barChange = jQuery('.dop_opcii input[type="checkbox"]').filter(':first');
		
		var needName = (barChange.is(':checked')) ? 'wwYes' : 'wwNo';
		var needInput = jQuery('input[name="'+needName+'"]').filter(':checked').filter(':first');
		var price = needInput.val();
		var wwName = needInput.attr('rel');
		//alert(wwName);
		jQuery('input[name="wwName"]').val(wwName);
		
		return getPrice(price);
	}
	
	
	
	
    function setTotalPrice(){
        var fprice = '';
        var i = 0, j = 0;
        
        price = baseprice + addprice + roofprice + barprice + '';
        for (i = price.length - 1; i >= 0; i--) {
            if (j == 3) {
                fprice = '.' + fprice;
                j = 0;
            }
            else 
                j++;
            fprice = price.charAt(i) + fprice;
        }
		
		
		
        
		
		jQuery('#totalprice, #totalprice2').html(fprice);
		
    }
    
    
    jQuery(".addoption").change(function(){
        addprice = getAddPrice();
		barprice = getBarPrice();
        setTotalPrice();
    });
    
    jQuery(".roofreplacement").change(function(){
        roofprice = getRoofPrice();
        setTotalPrice();
    });
	
	jQuery("input[name='wwNo'], input[name='wwYes']").change(function(){
        barprice = getBarPrice();
        setTotalPrice();
    });
    
    
    //------------------------------------------------------------------------------
    // работа с ценой на странице заказа
    //
    var totalprice;
    var habitatprice;
    var electrprice;
    var deliveryprice;
    
    totalprice = getBasePrice();
    habitatprice = getHabitatPrice();
    electrprice = getElectrPrice();
    
    setTotalPrice2();
    
    function getHabitatPrice(){
        var price = 0;
        jQuery.each(jQuery(".habitat"), function(key, item){
            if (jQuery(item).attr('checked') == true) 
                price = price + getPrice(jQuery(item).attr("title"));
        });
        
        return price;
    }
    function getElectrPrice(){
        var price = 0;
        
        jQuery.each(jQuery(".electr"), function(key, item){
            if (jQuery(item).attr('checked') == true) {
                price = price + getPrice(jQuery(item).attr("title"));
            }
        });
        
        return price;
    }
    function setTotalPrice2(){
        var fprice = '';
        var i = 0, j = 0;
        
        price = baseprice + habitatprice + electrprice + '';
        for (i = price.length - 1; i >= 0; i--) {
            if (j == 3) {
                fprice = '.' + fprice;
                j = 0;
            }
            else 
                j++;
            fprice = price.charAt(i) + fprice;
        }
        jQuery("#totalprice").empty();
        jQuery("#totalprice").append(fprice);
    }
    
    jQuery(".habitat").change(function(){
        habitatprice = getHabitatPrice();
        setTotalPrice2();
    });
    jQuery(".electr").change(function(){
        electrprice = getElectrPrice();
        setTotalPrice2();
    });
    
    jQuery("#show_sectionlist").click(function(){
        jQuery("#sectionlist").toggle();
        
        return false;
    });
    
    
    //-----------------------------------------------------------------------------
    // галерея на странице дома
    //
    jQuery(".plan a").click(function(){
        jQuery(".plan_gallery").show("fast");
        return false;
    });
    jQuery(".plan_gallery_hide").click(function(){
        jQuery(".plan_gallery").hide(100);
        return false;
    });
    
    
    //-----------------------------------------------------------------------------
    // работа с флешем на главной
    //
    jQuery("#slowly").click(function(){
        jQuery("#flash").empty();
        jQuery("#flash").append('<div class="flash"><div class="flash_pos"><object type="application/x-shockwave-flash" data="swf/main_34.swf" width="981" height="611"><param name="movie" value="flash/main_34.swf" /><param name="wmode" value="transparent"></object></div></div>');
        
        jQuery.cookie("flash", "slowly", {
            expires: 365
        });
        
        return false;
    });
    jQuery("#fast").click(function(){
        jQuery("#flash").empty();
        jQuery("#flash").append('<div class="flash"><div class="flash_pos"><object type="application/x-shockwave-flash" data="swf/main_33.swf" width="981" height="611"><param name="movie" value="flash/main_33.swf" /><param name="wmode" value="transparent"></object></div></div>');
        
        jQuery.cookie("flash", "fast", {
            expires: 365
        });
        
        return false;
    });
    
    
    setTotalPrice();
    
});



function UnSelected(id){
    sel = document.getElementById(id);
    for (var i = 0; i < sel.length; i++) {
        sel.elements[i].checked = false;
    }
    return false;
}

function Selected(id){
    sel = document.getElementById(id);
    for (var i = 0; i < sel.length; i++) {
        sel.elements[i].checked = true;
    }
    return false;
}

function s_sbm(id, beg){
    sel = document.getElementById(id);
    if (beg != "no") {
        sel.style.background = "background-image: url('images/send_sbm_on.png') no-repeat";
    }
    else {
        sel.style.background = "background-image: url('images/send_sbm.png') no-repeat";
    }
}



//------------------------------------------------------------------------------
// проверка наличия флеша
//

var flashinstalled = 0;
var flashversion = 0;
MSDetect = "false";

if (navigator.plugins && navigator.plugins.length) {
    x = navigator.plugins["Shockwave Flash"];
    if (x) {
        flashinstalled = 2;
        if (x.description) {
            y = x.description;
            flashversion = y.charAt(y.indexOf('.') - 1);
        }
    }
    else 
        flashinstalled = 1;
    if (navigator.plugins["Shockwave Flash 2.0"]) {
        flashinstalled = 2;
        flashversion = 2;
    }
}
else 
    if (navigator.mimeTypes && navigator.mimeTypes.length) {
        x = navigator.mimeTypes['application/x-shockwave-flash'];
        if (x && x.enabledPlugin) 
            flashinstalled = 2;
        else 
            flashinstalled = 1;
    }
    else 
        MSDetect = "true";


function checkFlash(){
    if (flashinstalled == 2) {
        if (flashversion) 
            return 1;
        else 
            return 0;
    }
    else 
        if (flashinstalled == 1) 
            return 0;
        else 
            return 0; //Нет возможности определить флэш плагин
}



