var imgwidth = 635;
var anim = new Object();
var fxs = "";
var expanding = false;
var shrinking = false;

jQuery(function () {
    var plusdiv = "<div class='plusbar' style='display:none; height:120px'><div class='plus'>+</div></div>";
    jQuery('#headlines .headline').find('h1').before(plusdiv);
    jQuery('#headlines .headline').css('text-decoration', 'none');

    jQuery('#headlines .headline').each(function () {

        if (jQuery(this).find('.pics_section .image_block').length == 2) {
            jQuery(this).find('.pics_section').addClass('two');
        }
        else {
            jQuery(this).find('.pics_section').addClass('three');
        }
    });

    var ss = document.styleSheets;
    var home_style = ss[ss.length - 1];
    var dark_color = jQuery('#footernav a').css('color');
    var light_color = jQuery('#navblock').css('backgroundColor');

    css_init = {
        'width': '350px',
        'height': '90px',
        'left': imgwidth + 'px',
        'marginLeft': '15px',
        'marginTop': '0'
    };

    css_hover = {
        'width': '390px',
        'height': '120px',
        'left': imgwidth + 'px',
        'marginLeft': '-25px',
        'marginTop': '-10px'
    };

    // BOX QUANDO EXPANDIDO
    css_exp = {
        'width': '890px',
        'height': '285px',
        'left': '110px',
        'marginLeft': '0',
        'marginTop': '4px'
    };

    css_init_color = {
        'backgroundColor': 'white'
    };

    css_focus_color = {
        'backgroundColor': dark_color
    };

    if (home_style.addRule) {

        home_style.addRule("#headlines .headline.focused", "background-color:" + dark_color, home_style.rules.length);
        home_style.addRule("#headlines .headline.focused", "border-top:" + light_color, home_style.rules.length);
        home_style.addRule("#headlines .headline.focused", "border-bottom:" + light_color, home_style.rules.length);

        home_style.addRule("#headlines .headline h2", "color:" + light_color, home_style.rules.length);

        home_style.addRule("#headlines .headline .pics_section .image_block", "background-color:" + light_color, home_style.rules.length);
        home_style.addRule("#headlines .headline .plusbar", "background-color:" + light_color, home_style.rules.length);
    }
    else {
        cssNode = document.createElement('style');
        cssNode.type = 'text/css';
        cssNode.rel = 'stylesheet';
        cssNode.media = 'screen';
        cssNode.title = 'dynamicSheet';
        document.getElementsByTagName("head")[0].appendChild(cssNode);

        var new_style = ss[ss.length - 1];
        new_style.insertRule("#headlines .headline.focused{background-color:" + dark_color + ";}", 0);
        new_style.insertRule("#headlines .headline.focused{border-color:" + light_color + ";}", 0);

        new_style.insertRule("#headlines .headline h2{color:" + light_color + ";}", 0);

        new_style.insertRule("#headlines .headline .pics_section .image_block{background-color:" + light_color + ";}", 0);
        new_style.insertRule("#headlines .headline .plusbar{background-color:" + light_color + ";}", 0);
    }

    jQuery.fn.extend({

        init_state: function (speed) {
            fxs += "(" + jQuery(this).attr('id') + "|init_state)";
            jQuery(this).removeClass('focused');
            jQuery(this).removeClass('extended');
            var pbar = jQuery(this).find('.plusbar');
            var headers = jQuery(this).find('h1, h2');

            pbar.hide();

            jQuery(this).stop();
            headers.stop();
            css_init_extend = jQuery.extend(true, {}, css_init);

            if (jQuery(this).hasClass('mini')) {
                css_init_extend.height = '60px';
            }

            if (jQuery(this).hasClass('vejamais')) {
                css_init_extend.height = '40px';
            }

            jQuery(this).animate(css_init_extend, speed);
            jQuery(this).css('z-index', '2');

            headers.animate({ 'marginLeft': '15px' }, speed);

            jQuery(".shadow").hide();
            jQuery("span.shadow").remove();

            jQuery(this).find('.separa_produto').show();
        },


        init_slide: function () {
            fxs += "(" + jQuery(this).attr('id') + "|init_slide)";
            jQuery(this).stop(true, true);
            jQuery(this).find('h1, h2').stop(true, true);

            jQuery(this).addClass('focused');

            var css_hover_extend = jQuery.extend(true, { 'z-index': (50 + jQuery(this).hasClass('mini')) }, css_hover);
            if (jQuery(this).hasClass('mini')) {
                css_hover_extend.height = '60px';
                css_hover_extend.marginTop = '2px';
            }

            if (jQuery(this).hasClass('vejamais')) {
                css_hover_extend.height = '48px';
            }

            jQuery(this).css(css_hover_extend);
            jQuery(this).find('.plusbar').height(jQuery(this).height()).css('display', 'inline');

            jQuery(this).css('margin-left', '-25px').animate({ 'marginLeft': '-25px' }, 150);
            jQuery(this).find('h1').css('margin-left', '55px').animate({ 'marginLeft': '30px' }, 200);
            jQuery(this).find('h2').css('margin-left', '55px').animate({ 'marginLeft': '30px' }, 200);

            jQuery(this).find('.separa_produto').hide();
        },

        expand: function (top, speed) {
            fxs += "(" + jQuery(this).attr('id') + "|expand)";
            var pbar = jQuery(this).find('.plusbar');

            pbar.css('height', '100%');
            pbar.find('.plus').text('-');

            if (jQuery(this).hasClass('mini')) {
                jQuery(this).find('h2').fadeIn(speed / 2);
                jQuery(this).removeClass('mini');
            }

            expanding = true;
            jQuery(this).addClass('expanded');
            var cur_id = jQuery(this).attr('id');
            var css_exp_extend = jQuery.extend(true, { 'top': top }, css_exp);
            jQuery(this).animate(css_exp_extend, speed, 'swing', function () {

                jQuery("#headlines .headline:not('#" + cur_id + "')").stop(true, true);
                jQuery("#headlines .headline h2").stop(true, true);

                var pics = jQuery(this).find('.pics_section');
                var links = jQuery(this).find('.links_section');
                jQuery(this).find('.body_text').show();
                pics.slideDown(300, function () {
                    links.slideDown(300);
                });

                expanding = false;
            });

            jQuery(this).find('h1,h2').animate({ 'marginLeft': '40px' }, speed / 4);
            jQuery(this).find('.separa_produto').hide();
        },


        shrink_max: function (top, speed) {
            fxs += "(" + jQuery(this).attr('id') + "|shrink_max)";
            jQuery(this).find('.plusbar .plus').text('+');

            jQuery(this).find('.body_text').hide();

            jQuery(this).find('.pics_section').stop(true, true).hide();
            jQuery(this).find('.links_section').stop(true, true).hide();

            var extend = { 'top': top };
            var css = jQuery.extend(true, extend, css_hover);

            if (jQuery(this).hasClass('vejamais')) {
                css.height = '48px';
            }

            var cur_id = jQuery(this).attr('id');
            shrinking = true;
            jQuery(this).animate(css, speed, 'swing', function () {

                jQuery(this).removeClass('expanded');

                if (!jQuery(this).hasClass('hovering')) {
                    jQuery(this).init_state(0);
                }

                jQuery("#headlines .headline:not('#" + cur_id + "')").stop(true, true);
                jQuery("#headlines .headline h2").stop(true, true);

                shrinking = false;
            });

            jQuery(this).find('h1,h2').animate({ 'marginLeft': '55px' }, speed / 4);
        },

        shrink_min: function (top) {
            fxs += "(" + jQuery(this).attr('id') + "|shrink_min)";

            var pbar = jQuery(this).find('.plusbar');
            pbar.find('.plus').text('+');
            pbar.hide();

            jQuery(this).find('.body_text').hide();
            jQuery(this).find('.pics_section').stop(true, true).hide();
            jQuery(this).find('.links_section').stop(true, true).hide();

            jQuery(this).find('h2').hide();
            jQuery(this).find('h1, h2').css('margin-left', '15px');

            jQuery(this).removeClass('focused');
            jQuery(this).removeClass('expanded');
            jQuery(this).addClass('mini');

            css_init_extend = jQuery.extend(true, { 'top': top }, css_init);

            if (jQuery(this).hasClass('vejamais')) {
                css_init_extend.height = '48px';
            }
            else {
                css_init_extend.height = '60px';
            }

            jQuery(this).css(css_init_extend);
            jQuery(this).css('z-index', '2');

            jQuery(this).find('.separa_produto').show();
        },

        minimize: function (top, speed) {

            fxs += "(" + jQuery(this).attr('id') + "|minimize)";

            jQuery(this).stop(false, true);
            jQuery(this).find('h1, h2').stop(false, true);
            jQuery(this).removeClass('expanded');
            jQuery(this).addClass('mini');
            css_init_extend = jQuery.extend(true, { 'top': top }, css_init);

            if (jQuery(this).hasClass('vejamais')) {
                css_init_extend.height = '40px';
            }
            else {
                css_init_extend.height = '60px';
            }

            jQuery(this).animate(css_init_extend, speed, 'swing', function () {


            });
            jQuery(this).find('h2').fadeOut(speed / 2);

            jQuery(this).find('.separa_produto').show();
        },

        maximize: function (top, speed) {

            fxs += "(" + jQuery(this).attr('id') + "|maximize)";

            jQuery(this).stop(false, true);
            jQuery(this).find('h1, h2').stop(false, true);

            jQuery(this).removeClass('mini');
            css_init_extend = jQuery.extend(true, { 'top': top }, css_init);

            if (jQuery(this).hasClass('vejamais')) {
                css_init_extend.height = '40px';
            }

            jQuery(this).animate(css_init_extend, speed, 'swing', function () {

            });
            jQuery(this).find('h2').fadeIn(speed);

            jQuery(this).find('.separa_produto').show();
        }
    });


    jQuery('#headlines .headline').hover(
			function () {
			    jQuery(this).addClass('hovering');

			    if (!jQuery(this).hasClass('expanded')) {

			        jQuery(this).init_slide();
			    }

			},
			function () {
			    jQuery(this).removeClass('hovering');

			    if (!jQuery(this).hasClass('expanded')) {

			        jQuery(this).init_state(0);
			    }

			}
		);

    jQuery('#headlines .headline').click(function () {
        if (expanding || shrinking) return;
        var pbar = jQuery(this).find('.plusbar');
        var temp = jQuery(this).attr('id').split('_');
        var exp_no = temp[1] - 1;
        if (!jQuery(this).hasClass('expanded')) {
            var there_is_expanded = jQuery('#headlines .expanded').length;
            jQuery('#headlines .headline').each(function (i) {
                var top = 297;
                if (i > exp_no) {
                    top += (i - 1) * 56 + 285;
                }
                else {
                    top += i * 56;
                }

                if (i == exp_no) {
                    jQuery(this).expand(top, 500);
                }
                else {
                    if (jQuery(this).hasClass('expanded')) {
                        if (there_is_expanded) {
                            jQuery(this).shrink_min(top);
                        }
                        else {
                            jQuery(this).shrink_max(top, 500);
                        }
                    }
                    else {
                        jQuery(this).minimize(top, 500);
                    }
                }

            });
        } else {

        }

    });

    jQuery('#headlines .headline .plusbar').live('click', function () {
        if (expanding || shrinking) return;

        var temp = jQuery(this).closest('.headline').attr('id').split('_');
        var exp_no = temp[1] - 1;

        jQuery('#headlines .headline').each(function (i) {
            var top = 301 + i * 90;

            if (i == exp_no) {
                jQuery(this).shrink_max(top, 500);
            }
            else {
                jQuery(this).maximize(top, 500);
            }

        });

    });

    jQuery('.image_block').hover(

		function () {
		    //jQuery(this).animate({'backgroundColor':'#aaaaaa'},200);
		},
		function () {
		    //jQuery(this).stop(true,true);
		    //jQuery(this).css('background-color','');

		}
		);

});
	
