$(document).ready(function(){

    // ++++++++++++++++++++++++++++++++++++++++
    // Bookmarks Layer ein- und ausblenden
    // ++++++++++++++++++++++++++++++++++++++++
    $('.bookmarks').click(function() {
        if ($('#social_bookmarks').is(":hidden") ) {
            $('#social_bookmarks').fadeIn("200");
        } else {
            $('#social_bookmarks').fadeOut("200");
        }
    });

    $('#sb_close').click(function() {
        $('#social_bookmarks').fadeOut("200");
    });

    // ++++++++++++++++++++++++++++++++++++++++
    // Gallerie
    // ++++++++++++++++++++++++++++++++++++++++
    if ($(".tx-rgsmoothgallery-pi1").html() != null) {
        gallery();
    }

    // ++++++++++++++++++++++++++++++++++++++++
    // Define Fancybox
    // ++++++++++++++++++++++++++++++++++++++++
	if(jQuery().fancybox) {
		$('a.fancy_inline').fancybox();
		
		$("a.fancy_gallery").fancybox({
			'hideOnContentClick': false
		});
	  
		$("a.fancy_philosophiemodell").fancybox({
		
			'width': 820,
			'height': 940,
			'autoDimensions': true,
			'hideOnContentClick': false,
			'padding' : 0,
			'margin' : 0
		});
	}
    // ++++++++++++++++++++++++++++++++++++++++
    // Focus Outline von Links entfernen
    // ++++++++++++++++++++++++++++++++++++++++
    $('a').focus(function() {
        this.blur();
    });

    // ++++++++++++++++++++++++++++++++++++++++
    // Hover-Effect fuer Submit-Buttons
    // ++++++++++++++++++++++++++++++++++++++++
    $("input[type='submit']:not('.preisrechner_submit_widget')").hover(
        function () {
            $(this).css(
            {
                backgroundPosition: '0 -37px'
            }
            );
        },
        function () {
            $(this).css(
            {
                backgroundPosition: '0 0'
            }
            );
        }		
        );

    // ++++++++++++++++++++++++++++++++++++++++
    // Combobox
    // ++++++++++++++++++++++++++++++++++++++++

    $(".combobox").each(function() {
        combobox($(this).attr("id"));
    });
	
    // ++++++++++++++++++++++++++++++++++++++++
    // Slider Bilder mittig
    // ++++++++++++++++++++++++++++++++++++++++

    window.setInterval("galleryThumbs()", 1000);
  
    philosophiemodell();
    tabSlider();
	
	// heise 2-Click SocialPrivacy-Plugin
	initSocialPrivacy();
  
});

// combobox

var fadeOutInterval;
var mouseOutTime = 1000;

function combobox(id) {
    if($("#"+id+" .combobox_content").html() == "") {
        $("#"+id).hide();
    } else {
        $("#"+id+" .combobox_title").bind("click", function() {
            window.clearInterval(fadeOutInterval);

            if($("#"+id).hasClass("combobox_active") == true) {
                $("#"+id+" .combobox_content").slideUp('fast');
                $("#"+id).removeClass("combobox_active");	
            } else {
                $(".combobox_content").slideUp('fast');
                $("#"+id).removeClass("combobox_active");
			
                $("#"+id+" .combobox_content").slideDown('fast');
                $("#"+id).addClass("combobox_active");
            }
        });  

        $(".combobox#"+id).bind("mouseout", function() {
            window.clearInterval(fadeOutInterval);

            fadeOutInterval = window.setInterval("fadeout('#"+id+"','.combobox_content')", mouseOutTime);
        });

        $(".combobox#"+id).bind("mouseover", function() {
            window.clearInterval(fadeOutInterval);
        });
    }
}

function fadeout(id, cssClass) {
    $(id+' '+cssClass).slideUp("fast");
    $(id).removeClass("combobox_active");
}

function goToIndustryElectricity(target_url, form_target) { 
    tarifrechner_zipcode = parseInt($('#plz').val());
    tarifrechner_verbrauch = parseInt($('#verbrauch').val());
    tarifrechner_kennwort = $('#kennwort').val();
	
    if(!isNaN(tarifrechner_zipcode) && !isNaN(tarifrechner_verbrauch)) {
        $('.tarifrechner_form').attr('action', target_url);
        //alert(target_url);
        $('.tarifrechner_form').attr('target', form_target);
        $('.tarifrechner_form').submit();
    }

    return false;
}

function gallery() {
    /* code correction */
    $(".tx-rgsmoothgallery-pi1 .clear").before("</div>");
	
    var bilder = new Array();

    $(".tx-rgsmoothgallery-pi1 .imageElement").each(function (i){
        bilder[i] = new Array();
        bilder[i]["title"] = $(this).children("h3").html();
        bilder[i]["text"] = $(this).children("p").html();

        if (bilder[i]["text"]) {
            bilder[i]["text"] = bilder[i]["text"].replace(/<br>/gi,"").replace(/"/gi,"?");
        } else {
            bilder[i]["text"] = "";
        }

        bilder[i]["small"] = $(this).children("img.thumbnail").attr("src");
        bilder[i]["mid"]   = $(this).children("img.full").attr("src");
        bilder[i]["big"]   = $(this).children("a").attr("href");		
    }); 

    $(".tx-rgsmoothgallery-pi1").html("");
	
    var bigimgs = '';
    var i = '';

    for (i = 0; i < bilder.length; i++) {
        if (i == 0) {
            hide = '';
            _class = 'class="img_'+i+' active"';
        } else {
            hide = ' style="display:none"';
            _class = 'class="img_'+i+'"';
        }
    
        if (bilder[i]["title"] == null) {
            bilder[i]["title"] = '';
        }
	
        bigimgs = bigimgs+'<a'+hide+' rel="images" '+ _class +' href="'+ bilder[i]["big"] +'" title="'+bilder[i]["title"]+'"><img src="'+ bilder[i]["mid"] +'" title="'+ bilder[i]["title"] +'" alt="'+bilder[i]["title"]+'" /></a>';
		
    }

    $(".tx-rgsmoothgallery-pi1").html('</script><div class="bigimg">'+bigimgs+'</div><div class="gallery"><div id="slideLeft" onclick="slideLeft_func()">&nbsp;</div><div id="slideContainer"><div id="slider"></div></div><div id="slideRight" onclick="slideRight_func()">&nbsp;</div></div>');

    i = '';

    for (i = 0; i < bilder.length; i++) {
        $(".tx-rgsmoothgallery-pi1 #slider").append('<div class="thumb"><a href="'+bilder[i]["mid"]+'" onclick="return changeImg(\''+i+'\')" title="'+ bilder[i]["title"] +'"><img src="'+bilder[i]["small"]+'" id="rgs-thumbnail-'+i+'" title="'+ bilder[i]["title"] +'" alt="'+ bilder[i]["title"] +'"/></a></div>')
    }

    $(".bigimg a").fancybox({
        'zoomSpeedIn': 300, 
        'zoomSpeedOut': 300, 
        'overlayShow': true
    });
}

function galleryThumbs() {
    $('#slider .thumb').each(function(i) {
        var sliderHeight = 67;
        var thumbHeight = $('#rgs-thumbnail-'+i).height();
        var thumbPadding = (sliderHeight - thumbHeight) / 2;

        $('#rgs-thumbnail-'+i).css({
            'padding-top':thumbPadding
        });
    });
}

function blurLink() {
    return true;
}

function philosophiemodell() {
    var default_img = $('#philosophiemodell_item_00');

    $('.philosophiemodell_item.maps area').each(function() {
        //alert($.browser.version);
        if ($.browser.msie == true && $.browser.version != '6.0') {
            $(this).attr('onFocus', 'if(this.blur)this.blur();');
        }
        $(this).attr('href', 'javascript:void(0);');
    //alert($.browser.msie);
    });


    $('#philosophiemodell_map_marktintegration').click(function() {
        default_img.hide();
        hideElements();
        $('#philosophiemodell_item_01').show();
    });
	
    $('#philosophiemodell_map_investition').click(function() {
        default_img.hide();
        hideElements();
        $('#philosophiemodell_item_02').show();
    });
	
    $('#philosophiemodell_map_foerderung').click(function() {
        default_img.hide();
        hideElements();
        $('#philosophiemodell_item_03').show();
    });
}
function hideElements() {	
    $('#philosophiemodell_item_01').hide();
    $('#philosophiemodell_item_02').hide();
    $('#philosophiemodell_item_03').hide();
}

var tab_item_id = '';
function tabSlider() {
    
    tab_item_css_top = $('.teaser_tabs_items_item:first').css('bottom');
    
    $('.teaser_tabs_items_item').each(function(i) {
        $(this).attr('id', 'teaser_tabs_items_item_' + (i+1));
        $(this).addClass('zIndex_' + $(this).css('z-index'));
    });
    
    $('.teaser_tabs_items_item').hover(function() {
          tab_item_id = $(this).attr('id');          
          tab_item_css_zIndex = $('#' + tab_item_id).css('z-index');
          $('#' + tab_item_id).stop(true,true).animate({'zIndex': '25', bottom: '0px'},300);
        },
        function() {
            last_zIndex = parseInt(tab_item_id.match(/(\d+)/));
            $('#' + tab_item_id).css({'zIndex': last_zIndex});            
            $('#' + tab_item_id).animate({'zIndex': last_zIndex, bottom: tab_item_css_top},300);
        })
}

function initSocialPrivacy() {
	if($('#socialshareprivacy').length > 0){
	$('#socialshareprivacy').socialSharePrivacy({
		services : {
			facebook : {
				'perma_option'  : 'off',
				//'dummy_img' 	: 'fileadmin/templates/_images/_layout/socialprivacy/dummy_facebook.png',
				'dummy_img' 	: 'fileadmin/templates/_images/bookmarks/fan-werden.gif',
				'language'		: 'de_DE',
				'action'		: 'like',
				'txt_fb_off'	: '',
				'txt_fb_on'		: '',
				'txt_info'		: ''
			}, 
			twitter : {
				'status'	: 'on',
				'perma_option' : 'off',
				'language'		: 'de_DE',
				'dummy_img' 	: 'fileadmin/templates/_images/_layout/socialprivacy/dummy_twitter.png'
			},
			gplus : {
				'status'	: 'on',
				'perma_option' : 'off',
				'dummy_img' 	: 'fileadmin/templates/_images/_layout/socialprivacy/dummy_gplus.png'
			}
		},
		'txt_help' : ''
	});
	}
}
