// Firma: Medienfreunde Hofmann & Baldes GbR
// Autor: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com


(function($) {

$.fn.innerfade = function(options) {

	this.each(function(){

		var settings = {
			animationtype: 'fade',
			speed: 'normal',
			timeout: 2000,
			type: 'sequence',
			containerheight: 'auto',
			runningclass: 'innerfade',
            offset: 28
		};

		if(options) $.extend(settings, options);

		var elements = $(this).children();

		if (elements.length > 1) {

			var $ul = $(this)
                .css('position', 'relative')
                .css('height', settings.containerheight)
                .addClass(settings.runningclass)
            ;

			for ( var i = 0; i < elements.length; i++ ) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute');
				$(elements[i]).hide();
                if (i == 0 && settings.containerheight == "auto") {
                    var $img = $('img', elements[i]),
                        fakeimg = document.createElement('img')
                    ;

                    $(fakeimg).one('load', function() {
                        $ul.height((parseInt(this.height) + settings.offset) || 240);
                        fakeimg = null;
                    }).attr('src', $img.attr('src'));
                }

			};


			if ( settings.type == 'sequence' ) {
				setTimeout(function(){
					$.innerfade.next(elements, settings, 1, 0, $ul);
				}, settings.timeout);
				$(elements[0]).show();
			} else if ( settings.type == 'random' ) {
				setTimeout(function(){
					do { current = Math.floor ( Math.random ( ) * ( elements.length ) ); } while ( current == 0 )
					$.innerfade.next(elements, settings, current, 0, $ul);
				}, settings.timeout);
				$(elements[0]).show();
			}	else {
				alert('type must either be \'sequence\' or \'random\'');
			}

		}

	});
};


$.innerfade = function() {};
$.innerfade.next = function (elements, settings, current, last, $ul) {
    if (settings.containerheight == "auto") {

        var $img = $('img', elements[current]),
            fakeimg = document.createElement('img')
        ;

        $(fakeimg).one('load', function() {
            $ul.animate({ height: (parseInt(this.height) + settings.offset) || 240 }, 300);
            fakeimg = null;
        }).attr('src', $img.attr('src'));
    }

	if ( settings.animationtype == 'slide' ) {
		$(elements[last]).slideUp(settings.speed, $(elements[current]).slideDown(settings.speed));
	} else if ( settings.animationtype == 'fade' ) {
		$(elements[last]).fadeOut(settings.speed);
		$(elements[current]).fadeIn(settings.speed);
	} else {
		alert('animationtype must either be \'slide\' or \'fade\'');
	};

	if ( settings.type == 'sequence' ) {
		if ( ( current + 1 ) < elements.length ) {
			current = current + 1;
			last = current - 1;
		} else {
			current = 0;
			last = elements.length - 1;
		};
	}	else if ( settings.type == 'random' ) {
		last = current;
		while (	current == last ) {
			current = Math.floor ( Math.random ( ) * ( elements.length ) );
		};
	}	else {
		alert('type must either be \'sequence\' or \'random\'');
	};
	setTimeout((function(){$.innerfade.next(elements, settings, current, last, $ul);}), settings.timeout);
};
})(jQuery);



/*
 * jQuery ifixpng plugin
 * (previously known as pngfix)
 * Version 3.1.2  (2008/09/01)
 * @requires jQuery v1.2.6 or above, or a lower version with the dimensions plugin
 *
 * Based on the plugin by Kush M., http://jquery.khurshid.com
 *
 * Background position Fixed
 * Also fixes non-visible images
 * (c) Copyright Yereth Jansen (yereth@yereth.nl)
 * personal website: http://www.yereth.nl
 * Company website: http://www.wharf.nl
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * For a demonstration of the background-position being fixed:
 * http://www.yereth.nl/bgpos.html
 *
 * Plugin page:
 * http://plugins.jquery.com/project/iFixPng2
 *
 */

/**
 *
 * @example
 *
 * optional if location of pixel.gif if different to default which is images/pixel.gif
 * $.ifixpng('media/pixel.gif');
 *
 * $('img[@src$=.png], #panel').ifixpng();
 *
 * @apply hack to all png images and #panel which icluded png img in its css
 *
 * @name ifixpng
 * @type jQuery
 * @cat Plugins/Image
 * @return jQuery
 * @author jQuery Community
 */
;(function($) {

	/**
	 * helper variables and function
	 */
	$.ifixpng = function(customPixel) {
		$.ifixpng.pixel = customPixel;
	};

	$.ifixpng.regexp = {
		bg: /^url\(["']?(.*\.png([?].*)?)["']?\)$/i,
		img: /.*\.png([?].*)?$/i
	},

	$.ifixpng.getPixel = function() {
		return $.ifixpng.pixel || '/sites/all/themes/gyongyhaj/blank.gif';
	};

	var hack = {
		base	: $('base').attr('href'),
		ltie7	: $.browser.msie && $.browser.version < 7,
		filter	: function(src) {
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
		}
	};

	/**
	 * Applies ie png hack to selected dom elements
	 *
	 * $('img[@src$=.png]').ifixpng();
	 * @desc apply hack to all images with png extensions
	 *
	 * $('#panel, img[@src$=.png]').ifixpng();
	 * @desc apply hack to element #panel and all images with png extensions
	 *
	 * @name ifixpng
	 */

	$.fn.ifixpng = hack.ltie7 ? function() {
		function fixImage(image, source, width, height, hidden) {
			image.css({filter:hack.filter(source), width: width, height: height})
			  .attr({src:$.ifixpng.getPixel()})
			  .positionFix();
		}

    	return this.each(function() {
			var $$ = $(this);
			if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
				var source, img;
				if (this.src && this.src.match($.ifixpng.regexp.img)) { // make sure it is png image
					// use source tag value if set
					source = (hack.base && this.src.substring(0,1)!='/' && this.src.indexOf(hack.base) === -1) ? hack.base + this.src : this.src;
					// If the width is not set, we have a problem; the image is not probably visible or not loaded
					// and we need a work around.
					if (!this.width || !this.height) {
						$(new Image()).one('load', function() {
							fixImage($$, source, this.width, this.height);
							$(this).remove();
						}).attr('src', source);
					// If the image already has dimensions (it's loaded and visible) we can fix it straight away.
					} else fixImage($$, source, this.width, this.height);
				}
			} else if (this.style) { // hack png css properties present inside css
				var imageSrc = $$.css('backgroundImage');
				// Background repeated images we cannot fix unfortunately
				if (imageSrc && imageSrc.match($.ifixpng.regexp.bg) && this.currentStyle.backgroundRepeat == 'no-repeat') {
					imageSrc = RegExp.$1;
					var x = this.currentStyle.backgroundPositionX || 0, y = this.currentStyle.backgroundPositionY || 0;
					if (x || y) {
						var css = {}, img;
						if (typeof x != 'undefined') {
							if (x == 'left') css.left = 0;
							// if right is 0, we have to check if the parent has an odd width, because of an IE bug
							else if (x == 'right') css.right = $$.width() % 2 === 1 ? -1 : 0;
							else css.left = x;
						}
						if (typeof y != 'undefined') {
							// if bottom is 0, we have to check if the parent has an odd height, because of an IE bug
							if (y == 'bottom') css.bottom = $$.height() % 2 === 1 ? -1 : 0;
							else if (y == 'top') css.top = 0;
							else css.top = y;
						}
						img = new Image();
						$(img).one('load', function() {
							var x,y, expr = {}, prop;
							// Now the image is loaded for sure, we can see if the background position needs fixing with an expression (in case of percentages)
							if (/center|%/.test(css.top)) {
								expr.top = "(this.parentNode.offsetHeight - this.offsetHeight) * " + (css.top == 'center' ? 0.5 : (parseInt(css.top) / 100));
								delete css.top;
							}
							if (/center|%/.test(css.left)) {
								expr.left = "(this.parentNode.offsetWidth - this.offsetWidth) * " + (css.left == 'center' ? 0.5 : (parseInt(css.left) / 100));
								delete css.left;
							}
							// Let's add the helper DIV which will simulate the background image
							$$.positionFix().css({backgroundImage: 'none'}).prepend(
								$('<div></div>').css(css).css({
									width: this.width,
									height: this.height,
									position: 'absolute',
									filter: hack.filter(imageSrc)
								})
							);
							if (expr.top || expr.left) {
								var elem = $$.children(':first')[0];
								for (prop in expr) elem.style.setExpression(prop, expr[prop], 'JavaScript');
							}
							$(this).remove();
						});
						img.src = imageSrc;
					} else {
						$$.css({backgroundImage: 'none', filter:hack.filter(imageSrc)});
					}
				}
			}
		});
	} : function() { return this; };

	/**
	 * positions selected item relatively
	 */
	$.fn.positionFix = function() {
		return this.each(function() {
			var $$ = $(this);
			if ($$.css('position') != 'absolute') $$.css({position:'relative'});
		});
	};

})(jQuery);




$(document).ready( function(){


    jQuery.ifixpng('/sites/all/themes/gyongyhaj/blank.gif');


/*

    $('#image_flade').innerfade({
        speed: 'slow',
        timeout: 9000,
        type: 'sequence',
        containerheight: '224px'
    });

*/
    $('#slideshow2').innerfade({
        speed: 'slow',
        timeout: 10800,
        type: 'random',
        containerheight: '224px'
    });

    $('#slideshow').innerfade({
        speed: 'slow',
        timeout: 10200,
        type: 'random',
        containerheight: '224px'
    });
    $('.slideit').innerfade({
        speed: 'slow',
        timeout: 9000,
        type: 'sequence',
        containerheight: 'auto'
    });


});





$(document).ready(function(){

/*
   Link check

    $('#content').prepend('<div id="link_debug"></div>');
    var $cont =  $('#link_debug');

    $('#content .node_body_cont a').each(function(i) {
        var $t = $(this), href = $t.attr('href');
        $cont.append(
        '<div><a href="'+href+'">Link ' + i  + '</a>: ' + $t.attr('href') + '</div>'
        );
    });

*/



    $('#content ul.bl-accordion').each(function(i) {
        var $li = $('> li', this);
        $li.eq(0).addClass('seeing').end().not(':first').find('> div').hide();

        $('> h4', $li).click(function(e) {
            if (!$(this).parent().is('.seeing')) {
                $('> div', $li.filter('.seeing').removeClass('seeing')).slideUp(500);
                $(this).next().slideDown(500).parent().addClass('seeing');
            }
            return false;
        }).hover(function(){
            $(this).parent().addClass('bl-accordion-hover')
        }, function(e) {
            $(this).parent().removeClass('bl-accordion-hover')
        });

    });







});

Drupal.behaviors.bind_bloglist = function (context) {

    if ( $('#block-views-49').length ) {

        $('#block-views-49 .bloglist li:not(.bloglist-processed)').hover(function(e) {
            $(this).addClass('bloglist_hover');
        }, function(e) {
            $(this).removeClass('bloglist_hover');
        }).click(function (e) {
            if ( !$(e.targert).is('a') ) {
                window.location.href = $('> .views-field-title > a', this).attr('href');
            }
        }).addClass('bloglist-processed');
    }

    if ($('#load_menu_items:not(.load-processed)')) {
        $('#load_menu_items').each(function(i) {
            var cont = $('#'+ $(this).attr('rel')).next().html();
            $(this).html($('<ul>'+cont+'</ul>').find('a').removeAttr('id').end().find('li, ul').removeAttr('class').end());
        }).addClass('load-processe');
    }

      // img.frs_new, img.ic, img.frs_gar, .node img

    if ($.browser.msie && $.browser.version < 7) {
        $('img:not(.pngfixed), #content .akcio_box').ifixpng().addClass('pngfixed');
    }

};

Drupal.behaviors.initColorbox = function (context) {
    if (typeof $.fn.colorbox != "function") { return; }

    $('a:not(.initColorbox-processed)', context)
        .filter('.colorbox, .thickbox').addClass('initColorbox-processed')
        .colorbox(Drupal.settings.colorbox)
    ;
    var _checkurl = function (url) {
        	return  /\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i.test(url);
        },
        dset_copy = $.extend({},Drupal.settings.colorbox);
    ;

    $('#content .node-content').each(function(){
        var $node = $(this), nid_id = $node.attr('id');
        $node.find('img').each(function(i) {
            var $img = $(this),
                $p = $img.parent()
            ;
            if ($p.is('a')) {
                if (!$p.is('.initColorbox-processed') && _checkurl($p.attr('href'))) {
                    $p.addClass('initColorbox-processed').colorbox(Drupal.settings.colorbox);
                }
            } else if ($img.is('.inserted_image:not(.initColorbox-processed)')) {
                var shref = $img.addClass('initColorbox-processed').attr('src')
                    .replace(/\/files\/imagecache\/(content|small|mini|thumb)\//, '/files/imagecache/big/');
                $img.addClass('initColorbox-processed phantom_image_link')
                    .colorbox($.extend({},Drupal.settings.colorbox, {href: shref, rel: nid_id}));

            }
        });

    })

    ;

};
