
//HomePage

//Вставка флеша 
function FlashHeaderInsert() {

	var hasRightVersion = DetectFlashVer(9, 0, 45);
	if(hasRightVersion) { 
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
			'width', '856',
			'height', '233',
			'src', 'header3_total',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', 'header3_total',
			'bgcolor', '#ffffff',
			'name', 'header3_total',
			'menu', 'true',
			'allowScriptAccess','sameDomain',
			'allowFullScreen','false',
			'movie', 'header3_total',
			'salign', ''
			);
	} else { 
		var msg = '<div class="flash-incorrect-area">' +
		    '<img src="/i/fibk.png" width="530" height="50" class="flash-incorrect-back png" />' +
            '<div class="flash-incorrect">У Вас не установлена необходимая для просмотра сайта версия Flash проигрывателя.<br />' +
            '<a href="http://www.macromedia.com/go/getflash/">Скачать здесь</a></div></div>';
		document.write(msg);
	}
	
}	


$(function() {
    $("img[@jCorner]").each(function() {
        var t = $(this);
        var p = t.parent(), m = t;
        if (p && p.is('a')) m = p;
        m.wrap("<div></div>").parent().css({
            width: t.attr('width'),
            height: t.attr('height')
        }).corner(t.attr('jCorner')+'px');
    });
    $("img[@jCornerF]").each(function() {
        var t = $(this);
        var p = t.parent(), m = t;
        if (p && p.is('a')) m = p;
        var cw = parseInt(t.attr('jCornerF'));
        var g = $('<table style="border-collapse: collapse;"><tr><td style="padding: 0;"></td></tr></table>').insertAfter(m);
        g.css({ width: cw*2, height: cw*2 });
        $("td", g).css({ width: cw*2, height: cw*2 }).append(m).corner(cw+'px');
    });     
});





//MenuView

//Инизиализация логотипа как ссылки на главную
$(function() {
    $(".top-logo:eq(0)").css("cursor", "pointer").click(function() {
        document.location = '/';
    });
});




var jhp = {

    select: '',
        
    toggle: function (d) {
        if (!d) return;
        
        var r = $('#c1r--'+d);
        var t = $('#c1s--'+d);
      
        r.attr('href', r.attr('jHref'));
        r.removeClass('udash');
        t.slideToggle();
        
        var s = this.select;
        if (s) {
        
            n = $('#c1r--'+s);
            t = $('#c1s--'+s);
            
            n.attr('href', 'javascript: jhp.toggle("'+s+'");');
            n.addClass('udash');
            t.slideToggle();                            
        }
        
        this.select = d;

    }
}







//GallView


function sh_ph(src, desc, caption, one_image)
{

    var it = $("#IimgThumbs").empty();
    if (!one_image) {
        $('#'+src).clone().show().appendTo(it);
        $(".IimgItem", it)
        .click(function() {
		    setToImgContainer($(this).attr("fullimage"), $(this));
	    });
	}
	
	$("#Icaption").text(caption);

    __lightResize();
    
    

    $("#lightPic")
        .css('opacity', 0.0)
        .show()
        .fadeTo(300, 0.6);

    $("#blockContainer").css({'visibility': 'hidden'}).show();
    __blockGenPos();
    $("#blockContainer").css({'visibility': 'visible'});
    
    
    $(window).resize(__lightResize);
  
  
    if (one_image) {
        setToImgContainer(src);   
    }
    else {
        var t = $(".IimgItem[@default=1]:eq(0)", it);
        if (t.length)
            setToImgContainer(t.attr("fullimage"), t);
    }
        
    if (desc) {
        var d = $('#descValue').empty();
        $("#" + desc).clone().show().appendTo(d);
    }
}

function dsh_close()
{
    $(window).unbind('resize', __lightResize);

    $("#blockContainer").hide();
    $("#lightPic").fadeTo(300, 0.0, function() {
        $("#lightPic").hide();     
    });
    
    $("#IimgContainer, #descBlock").css({width: 600, height: 'auto'});
    $('#blockTable').css("marginTop", 0);
    $("#IimgContainer, #descValue").empty();
}



function __lightResize()
{

    var c = $('#lightPic, #blockContainer').hide();
    var w = $(document).width() - ($.browser.msie && $.browser.version < 7 ? 22 : 0);
    var h = $(document).height();
    c.css({width: w, height: h}).show();
}


function __blockGenPos()
{
    var b = $('#blockTable');
    var m = ($(window).height()  - b.height()) / 2 + $(document).scrollTop();      
    b.css("marginTop", m > 0 ? m : 0);
}
    
    
function setToImgContainer(image, node) {
	var t = $("#IimgContainer");
	var ldr = $("#IimgLoader").clone();
	var ah = $("#IanimateHelper");
	//var f = 0;
	t.fadeOut(400, function() {
		
		
		t.empty().show();
		ldr.appendTo(t).hide().fadeIn(200, function() {	
		
			var h = $("#IimgHelper");
			h.show().empty().append( $('<img src="'+image+'" />').load(function() {
				
				
				var i = $(this);
				ldr.fadeOut(200, function() {
                    ldr.remove();
                    
                    var b = $('#blockTable');

                    var m = parseInt(b.css("marginTop")) - (i.height()-t.height()) / 2;
                    if ($.browser.msie) m -= 1;
                    
                    b.animate({
                        "marginTop": m > 0 ? m : 0
                    }, 500, 'swing');

                    $("#descBlock").width(Math.max(Math.min(t.width(),i.width()), 600));
				
					t.animate({
						width: i.width() + ($.browser.msie ? 2 : 0),
						height: i.height() + ($.browser.msie ? 2 : 0)
					}, 500, 'swing', function() {
 
                        if (node) {
						    var os = node.offset();
						    var od = t.offset();
						    ah.css({
							    left: os.left,
							    top: os.top,
							    width: node.width(),
							    height: node.height()
						    })
						    .show()
						    .animate({
							    left: od.left,
							    top: od.top,
							    width: t.width(),
							    height: t.height()
						    }, 200, 'swing')
						    .fadeOut(100);
					    }
					    
						$("img", h).hide().appendTo(t).fadeIn(400);
				        h.hide();
				        $("#descBlock").width(Math.max(t.width(), 600));
					});
				
				});
				
			}) );
			
		});
	});
	
}
    
    
    
    
    
    
    
    
    
    



//Модуль CatItem


function CatItemInit() {

    $(".catitem-photo-table").corner("10px");
    
    var f = $(".catitem-photo img:eq(0)").attr('jSelected', 0);
    var i = 0;
    $(".catitem-photo-table-item img").each(function() {
        var t = $(this);
                                
        t.click(function() {
            if (f.attr('jSelected') == t.attr('jIndex')) return;
            
            t.busyOverlay({ 
                opacity: 0.5, 
                color: "white"
            },{
                color: '#0033ff', size: 50, type: 'oval'
            });
            
            $('<img src="'+t.attr('jFullImage')+'" />')
            .css({left: -1000, position: 'absolute'}).appendTo('body')
            .load(function() {
                var g = $(this);                               
                $(".catitem-photo-table-item").css('background', 'none');
                t.busyOverlay().parent().css('background', '#999');
                f.attr({src: g.attr('src'), jSelected: t.attr('jIndex')});
                g.remove();   
            });
                
        }).attr('jIndex', i++).css('cursor', 'pointer');
        
    });
}




//Модуль SendReq

var sqDialog = {

    dialog: null,
    content: null,
    fadeback: null,

    init: function(vKey) {    
        var t = $("#sqDialog")/*.draggable({
            handle: ".ctDlgHandle",
            containment: "document"
        })*/;
        $(".sqDlgClose", t).click(function() {
            sqDialog.close();
        });
        
        this.dialog = t;
        this.content = $(".sqDlgContent", t);
        this.fadeback = $("#sqFadeBack"); 



        $("input:text, "+(!$.browser.msie ? "select," : "")+" textarea", this.content).each(function() {
            var n, t = $(this);
            t.appendTo(
                n = $("<div></div>").css({
                    width: t.width()+10,
                    height: t.height()+9,
                    border: "1px solid #aaa",
                    background: "#fff",
                    float: "left"
                }).insertAfter(t).corner("6px")
            ).css("border", "none").css("margin", "2px");
        });
        t.hide().css({left: 0, top: 0});
        
        
        var g = this;
        $(window).resize(function() {        
            if (g.fadeback.is(":visible")) {
                g.fadeback.hide().css({
                    width: $(document).width() -($.browser.msie && $.browser.version < 7 ? 22 : 0),
                    height: $(document).height()
                }).show();          
            }
            if (parseInt(g.dialog.css("top")) < 0) {
                g.dialog.css("top", 0);
            }
        });       
    },

    
    show: function (w, h) {        
        var o = {
            width: $(window).width(),
            height: $(window).height(),
            top: $(window).scrollTop(),
            left: $(window).scrollLeft()
        }
        var d = {
            width: $(document).width(),
            height: $(document).height()
        }    
        this.fadeback.css({
            opacity: 0.6,
            width: d.width -($.browser.msie && $.browser.version < 7 ? 22 : 0),
            height: d.height
        }).fadeIn(200);
        h = Math.max(100, Math.min(d.height, h));

        var t, left = o.left + Math.max(0, (o.width-w)/2);
        if ( (t=left+w - Math.max(d.width, o.width)) > 0 ) left = Math.max(0, left-t);
        var top = o.top + Math.max(0, (o.height-h)/2);
        if ( (t=top+h - Math.max(d.height, o.height)) > 0 ) top = Math.max(0, top-t);


        var t = this.di;
        this.content.css("height", h-25);
        this.dialog.css({
            width: w,
            position: "absolute",
            zIndex: 1002,
            left: left,
            top: top
        })
        .fadeIn(300);      
    },
    
    
    close: function() {
        this.content.busyOverlay();        
        this.fadeback.fadeOut(200);
        this.dialog.fadeOut(200);
    },
    
    
    send: function(pid, iid, width, height) {      
        var t = this;
        var d = {pageid: pid, item_id: iid};
        //Собрать данные с формы
        $("input:text, input:button, textarea", t.content).each(function() {
            var i = $(this);
            d[i.attr('name')] = i.val();        
        });




        $.ajax({
            url: "index.php",
            type: "POST",
            dataType: "json",
            data: d,
            beforeSend: function() {
                $("[@jError]", t.content).hide();
                $("#sqDlgOkMsg, #sqDlgErrMsg").hide();
                
                t.content.busyOverlay({ 
                    opacity: 0.5, 
                    color: "white"
                },{
                    color: '#0033ff', size: 50, type: 'oval'
                });                
            }, 
            success: function(data) {                    
                t.content.busyOverlay();


                if (data.errors) {
                    if (data.errors instanceof Object) {                     
                        $("[@jError]", t.content).each(function() {
                            p = $(this);
                            if (data.errors[p.attr('jError')]) p.show(); else p.hide();
                        });
                    }
                    else $("#sqDlgErrMsg").show();
                }
                else if (data.success) {
                    $("#sqDlgOkMsg").show();
                }
                    

            },
            error: function() {
                t.content.busyOverlay();
                alert('Во время передачи данных возникла ошибка. Пожалуйста попробуйте снова.');
            }
        });



     
        
    
    }


}









