$(document).ready(function(){
    $("a.fancy").fancybox({
        'titleShow' : false,
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic',
        'easingIn' : 'easeOutBack',
        'easingOut' : 'easeInBack',
        'scrolling' : 'no'
    });

    $("a.cluetip").cluetip({
        arrows: true,
        cluetipClass: 'jtip'
    });

    if ( ( jQuery.browser.webkit || jQuery.browser.opera ) && document.readyState != "complete"){
        setTimeout( arguments.callee, 100 );
        return;
    }

    $.resizeAdaptive();
    $.resizeAdaptiveImages();
    $.resizeAssocs();

    $(window).bind( "resize", function(){
        $.resizeAdaptive();
        $.resizeAssocs();
    });

    $.insertFlash();

});

$.resizeAssocs = function(){


    var width = parseInt( $("div.view-port").css("width") ) - 250;

    if($("div.adaptive-assoc").length > 1){
        var new_width = ( width / 2 ) - 30 + "px" ;

        if( new_width < 350 ){
            new_width = width - 30 + "px";
        }

        $("div.adaptive-assoc").css("width", new_width );

        var icount = Math.floor( new_width / 120 );

        var inew_width = ( ( new_width * (100/icount) / 100 ) - 21 ) + "px" ;
        $("div.adaptive-assoc ul li:not(.header)").css("width", inew_width );
    }else{
        $("div.adaptive-assoc").css("width", width - 30 );
    }

}

$.resizeAdaptive = function(){
    var width = parseInt( $(".adaptive").css("width") );
    var count = Math.floor( width / 160 );

    if($(".adaptive .item").length <= count && parseInt($(".adaptive .item").length) > 3 ){
        count = parseInt($(".adaptive .item").length);
    }

    var new_width = ( width / count - 10 ) + "px" ;
    $(".adaptive .item").css("width", new_width );
}

$.resizeAdaptiveImages = function(){
    $(" .item .photo a, ul.list li a").each(function(){
        var height = 150;
        var img_height = parseInt( $(this).find("img").css("height") );
        if( height > img_height ){
            var padding = ( height - img_height ) / 2 ;
            $(this).parent().css("padding-top",padding).css("height",height-padding);
        }
    });
}



$.message = function(message, type) {
    
    var html = "";
    if(type == "error"){
        html = "<p class='message-error'>"+message+"</p>";
    }else{
        html = "<p class='message-success'>"+message+"</p>";
    }


    $.fancybox({
        'content': html
    });
}

$.insertFlash = function(){
    <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. -->
    var swfVersionStr = "10.0.0";
    <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
    var xiSwfUrlStr = "/flash/playerProductInstall.swf";
    var flashvars = {};
    flashvars.serviceUrl = "http://ippon.md/ru/api/banners.xml";

    var params = {};
    params.quality = "high";
    params.bgcolor = "#c50105";
    params.allowscriptaccess = "sameDomain";
    params.allowfullscreen = "true";
    
    var attributes = {};
    attributes.id = "banner";
    attributes.name = "banner";
    attributes.align = "middle";
    swfobject.embedSWF(
        "/flash/DynamicFlash.swf", "banner",
        "100%", "180",
        swfVersionStr, xiSwfUrlStr,
        flashvars, params, attributes);
}