(function($) {

    jQuery.fn.pngFix = function(settings) {

        // Settings
        settings = jQuery.extend({
            blankgif: 'blank.gif'
        }, settings);

        var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
        var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

        if (jQuery.browser.msie && (ie55 || ie6)) {

            //fix images with png-source
            jQuery(this).find("img[src$=.png]").each(function() {

                jQuery(this).attr('width',jQuery(this).width());
                jQuery(this).attr('height',jQuery(this).height());

                var prevStyle = '';
                var strNewHTML = '';
                var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
                var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
                var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
                var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
                var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
                var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
                if (this.style.border) {
                    prevStyle += 'border:'+this.style.border+';';
                    this.style.border = '';
                }
                if (this.style.padding) {
                    prevStyle += 'padding:'+this.style.padding+';';
                    this.style.padding = '';
                }
                if (this.style.margin) {
                    prevStyle += 'margin:'+this.style.margin+';';
                    this.style.margin = '';
                }
                var imgStyle = (this.style.cssText);

                strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
                strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
                strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
                strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
                strNewHTML += imgStyle+'"></span>';
                if (prevStyle != ''){
                    strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
                }

                jQuery(this).hide();
                jQuery(this).after(strNewHTML);

            });

            // fix css background pngs
            jQuery(this).find("*").each(function(){
                var bgIMG = jQuery(this).css('background-image');
                if(bgIMG.indexOf(".png")!=-1){
                    var iebg = bgIMG.split('url("')[1].split('")')[0];
                    jQuery(this).css('background-image', 'none');
                    jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
                }
            });
		
            //fix input with png-source
            jQuery(this).find("input[src$=.png]").each(function() {
                var bgIMG = jQuery(this).attr('src');
                jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
                jQuery(this).attr('src', settings.blankgif)
            });
	
        }
	
        return jQuery;

    };

    jQuery.fn.sds = function(photos, skins) {
        
        var html = '<div id="sds-friendsGallery" style="width: 452px; width: 339px; position: relative;"><img id="sds-friendsPic" src="' + photos + '/0.png" style="width: 452px; height: 339px; position: absolute;" /><div style="background-image: url(\'' + skins + '/ribbon.png\'); font-family: Arial; height: 40px; width: 452px; top: 250px; position: absolute; left: 0px; color: white; font-weight: bold;"><div id="sds-friendsName" style="margin: 2px 0 0 5px; font-size: 16px;"></div><div id="sds-friendsCity" style="margin-left: 5px; font-size: 13px; font-style: italic;"></div></div><img id="sds-curtain" src="' + photos + '/0.png" style="width: 452px; height: 339px; position: absolute;" /><div id="sds-friendsGalleryLeftArrow" style="display: none; position: absolute; top: 150px; left: 10px;"><img src="' + skins + '/leftArrow.png" /></div><div id="sds-friendsGalleryRightArrow" style="display: none; position: absolute; top: 150px; left: 403px;"><img src="' + skins + '/rightArrow.png" /></div></div>';

        $(this).html(html);

        var friends = [];
        var img = $('#sds-friendsPic');
        var curtain = $('#sds-curtain');
        var gallery = $('#sds-friendsGallery');
        var leftArrow = $('#sds-friendsGalleryLeftArrow');
        var rightArrow = $('#sds-friendsGalleryRightArrow');
        var file = 0;

        function showFriend(friend) {
            curtain.show();
            img.attr('src', photos + friend.file + '.jpg');
            $('#sds-friendsName').text(friend.name);
            $('#sds-friendsCity').text(friend.city);
        }

        img.load(function() {
            curtain.hide();
        });

        gallery.hover(
            function() {
                leftArrow.show();
                rightArrow.show();
                leftArrow.pngFix();
            }, function() {
                leftArrow.hide();
                rightArrow.hide();
            });
        
        leftArrow.click(function() {
            file = --file < 0 ? friends.length - 1 : file;
            showFriend(friends[file]);
        });
        
        rightArrow.click(function() {
            file = (file + 1) % friends.length;
            showFriend(friends[file]);
        });
        
        $.ajax({
            url: photos + 'friendsList.xml',
            dataType: 'xml',
            async: false,
            success: function(xml) {
                $(xml).find('friend').each(function() {
                    friends.push({
                        name: $(this).text(), 
                        city: $(this).attr('city'), 
                        file: $(this).attr('file')
                    });
                });
            }
        });

        file = Math.round(Math.random() * friends.length - 1);
        showFriend(friends[file]);
    }

})(jQuery);
