/************************************************************
 *  HTML5 PLACEHOLDER PLUGIN
 ************************************************************/
(function($){

	var placeholder_color='#999999';

	$.fn.placeholder = function( color ) {

		if ( color )
			placeholder_color = color;
		else
			color = placeholder_color;

		$(this).each( function(i){

			if ( $(this).attr('type') == 'password' ) {

				var
					$field = $(this)
						.focusout(function(e){
							if ( $field.val() == '' )
								$([ $field[0], $dummy[0] ]).toggle();
						})
						.toggle(),

					$dummy = $('<input/>', { type: 'text' })
						.focusin(function(e){
							$([ $field[0], $dummy[0] ]).toggle();
							$field.focus();
						});

					if ( ( 'class' in $field[0] ) )
						$dummy.attr('class', $field.attr('class'));

					if ( ( 'style' in $field[0] ) )
						$dummy.attr('style', $field.attr('style'));

					$dummy
						.val( $field.attr('placeholder') )
						.css('color', color)
						.toggle()
						.insertAfter( $field );
			}
			else {

				var orig = $(this).css('color');

				var el = $(this);

                if(el.val() == '')
                {
                    el.val( $(this).attr('placeholder') )
                }

				$(this)
					.css( 'color', color )

					.focusin(function( e ){
						if ( $(this).val() == $(this).attr('placeholder') ) {
							$(this).val('');
							$(this).css( 'color', orig );
						} })

					.focusout(function( e ){
						if ( $(this).val() == '' ) {
							$(this).val( $(this).attr('placeholder') );
							$(this).css( 'color', color );
					} });
			}

		} );

	}

})(jQuery);

/************************************************************
 *  JLayerS - Java Script Layer for Select elements
 *
 *  Author: nexik
 *  Copyright: bomb.pl
 *  Websites: http://nexik.net/ http://bomb.pl/
 *
 ************************************************************/

(function($) {
    jQuery.fn.JLayerS = function() {
        return this.each(function()
		{
            var select = jQuery(this);
            var select_id = jQuery(select).attr('id');
            
            // schowaj html'owego selecta
            $(select).hide();

            // utworz layera z dropdownem i ustaw aktulna wartosc z selecta na layerze
            var current_value = jQuery(select).find('option:selected').text();
            var div_layer = jQuery('<div/>').addClass('js_select').attr('id', 'js_select_' + select_id).html(current_value + '<span class="select_arrow"></span>');
            var div_elements = jQuery('<div/>').addClass('js_select_elements').attr({rel: select_id, id: 'js_select_elements_' + select_id});

            div_layer.insertAfter(select);
            div_elements.insertAfter(jQuery('#js_select_' + select_id)).hide();

            // pobierz wsyztskie wartosci z selecta i dodaj je do dropdowna
            var select_options = jQuery(select).children('option');

            jQuery.each(select_options, function(i){
                var option = jQuery(this);
                var element_span = jQuery('<span/>').addClass('js_select_elements_span').attr('value', option.val()).html(option.text());

                element_span.appendTo(jQuery('#js_select_elements_' + select_id));
            });

            // wyswietlanie dropdowna
            jQuery('#js_select_' + select_id).live('click', function() {
                jQuery('#js_select_elements_' + select_id).show();
            });

            // chowanie dropdowna
            jQuery('.in').hover(function() {}, function(){
                jQuery('#js_select_elements_' + select_id).hide();
            });

            // wybieranie elementu z dropdowna
            jQuery('#js_select_elements_' + select_id).find('.js_select_elements_span').live('click', function(){
                span_clicked = jQuery(this);

                // ukryj dropdown
                jQuery('#js_select_elements_' + select_id).hide();

                // zazancz nowa wartosc w tagu select
                span_clicked_value = $(span_clicked).attr('value');
                jQuery(select).val(span_clicked_value);

                // zaktualizuj wartosc layera
                jQuery(div_layer).html($(select).find('option:selected').text() + '<span class="select_arrow"></span>');
            });


        });
    };
})(jQuery);

/************************************************************
 *  Wallpaper Selector - Change Select HTML element into
 *  ul HTML elemnt with the wallpapers thumbnail
 *
 *  Author: nexik
 *  Copyright: bomb.pl
 *  Websites: http://nexik.net/ http://bomb.pl/
 *
 ************************************************************/

(function($) {
    jQuery.fn.WallS = function() {
        return this.each(function() {
            var form = jQuery(this);

            // obsluga przelacznika w formularzu
            jQuery('.wallpaper_file, .wallpaper_select, .wallpaper_resolution').hide();
            jQuery('input[name=source]').change(function() {
                var radio_value = $(this).val();

                if(radio_value == 'select') {
                    jQuery('.wallpaper_file').hide();
                    jQuery('.wallpaper_resolution, .wallpaper_select').show();
                } else  if( radio_value == 'upload' ) {
                    jQuery('.wallpaper_file').show();
                    jQuery('.wallpaper_resolution, .wallpaper_select').hide();
                }
                else {
                    jQuery('.wallpaper_resolution, .wallpaper_select, .wallpaper_file').hide();
                }
            });

            // przygotowanie kilku zmiennych
            var select = jQuery('#wallpaper');
            var select_id = jQuery(select).attr('id');
            var select_options = jQuery(select).children('option');;

            // ukrycie selecta z tapetami
            select.hide();

            // lista elementow z miniaturkami tapet
            var ul_wall = jQuery('<ul/>').addClass('ul_select').attr('id', 'ul_select_' + select_id);
            jQuery('.wallpaper_select .input_text').hide();
            ul_wall.appendTo(jQuery('.wallpaper_select'));

            // wypelnienie listy miniaturkami
            jQuery.each(select_options, function(i){
                var class_float_clear = (i%4 == 0) ? ' float_clear' : '';

                var option = jQuery(this);
                var wall_img = jQuery('<img />').addClass('img_wall').attr({src: '/img/wallpapers/'+option.text()+'.jpg', id: 'img_wall_' + option.val()});
                var wall_li = jQuery('<li/>').addClass('li_wall'+ class_float_clear).html(wall_img).attr('rel', option.val());

                wall_li.appendTo(ul_wall);
            });

            jQuery('.li_wall').click(function(){
                jQuery('.li_wall').removeClass('selected');
                $(this).addClass('selected');

                // aktualizacja selecta
                jQuery(select).val($(this).attr('rel'));

            });
        });
    };
})(jQuery);

/************************************************************
 *  Mailbox Friend Chooser - warstwa z lista znajomych do
 *  wyboru przy tworzeniu nowej wiadmosci w skrzynce pocztowej
 *
 *
 *  Author: nexik
 *  Copyright: bomb.pl
 *  Websites: http://nexik.net/ http://bomb.pl/
 *
 ************************************************************/

(function($) {
    jQuery.fn.MFC = function() {
        return this.each(function() {
            var input = $(this);
            var friends_list = [];

            /**
             * pobranie AJAXem listy znajomych
             * oraz zapisanie ich w tablicy
             */
            function get_friends_list()
            {
                jQuery.ajax({
                    url: '/profile/friends_list',
                    success: function(data){
                        friends_list = jQuery.parseJSON(data);
                        display_friends();

                        if( friends_list.length > 0 )
                            jQuery('#friends_list').show();
                    }
                });
            }

            /**
             * stworzenie warstwy do wyswietlania listy uzytkownikow
             * oraz podpiecie eventu do wyswietlania listy
             * gdy pole z nazwa usera jest puste
             */
            function display_friends()
            {
                var div_friends_list = jQuery('<div/>').attr('id', 'friends_list');
                div_friends_list.appendTo(jQuery('.mail_to'));

                jQuery.each(friends_list, function(i){
                    var friend_name = this;
                    var span_friend_name = jQuery('<span />');
                    span_friend_name.attr('id', 'span_friend_' + friend_name);
                    span_friend_name.appendTo(jQuery('#friends_list'));

                    var added_span = document.getElementById('span_friend_' + friend_name);
                    added_span.innerHTML = friend_name;
                });
            }

           /**
            * glowny skrypt
            */
            jQuery(input).focus(function(){
                if(friends_list.length == 0)
                    get_friends_list();

                if(jQuery(this).val() == '')
                    jQuery('#friends_list').show();
            });

            jQuery(input).click(function(){
                jQuery('#friends_list').show();
            });

            jQuery('#friends_list').live('mouseenter mouseleave', function(e) {
                if(e.type == 'mouseout') {
                    $(this).hide();
                }
            });

            jQuery('#friends_list span').live('click', function() {
                user_name = jQuery(this).text();
                input.val(user_name);
                jQuery('#friends_list').hide();
                input.focus();
            });

        });
    };
})(jQuery);

/************************************************************
 *  Wrapper Rounded Corner - skrypt do tworzenia zaokraglonych
 *  rogow dla obrazkow
 *
 *  Author: nexik
 *  Copyright: bomb.pl
 *  Websites: http://nexik.net/ http://bomb.pl/
 *
 ************************************************************/
(function($){
    
    jQuery.fn.wrc = function(options) {
        return this.each(function() {
          return new WRC(this, options);
        });
    }

    var WRC = function(el, options) {
        this.el = el;
        this.el_width = $(el).width();
        this.options = {
           border: '1px solid #000',
           corner: undefined
        };

        // fuzja domyslnego obiektu this.options z obiektem options przekazanym przez wywolanie pluginu
        if(options) {
            $.extend(this.options, options)
        }

        this.init();
    }

    WRC.prototype = {

        init: function() {
            var $el = $(this.el);
            var wrc_obj = this;
            var element_loaded = false;

            function el_load() {
                if( ! element_loaded)
                    $($el).ready(function(){
                        wrc_obj.wrap($el);
                    });
            }

            $($el).load(function(){
               wrc_obj.wrap($el);
               element_loaded = true;
            });

            setTimeout('el_load', 1000)
        },

        wrap: function(element) {
            var wrc_obj = this;

            $(element).wrap(function() {
                var wrapper_css = {
                    background: 'url(' + $(this).attr('src') + ') no-repeat center center',
                    border:     wrc_obj.options.border,
                    display:    'block',
                    height:     $(this).height(),
                    margin:     '0 auto',
                    width:      $(this).width()
                }

                return jQuery('<span/>').addClass('wrap_corner').css(wrapper_css);
            });

            var element_css = {opacity: '0'};

            $(element).css(element_css);
            $('.wrap_corner').corner( wrc_obj.options.corner );
        }
        
    };
  
})(jQuery);

/*
 * 	loopedSlider 0.5.6 - jQuery plugin
 *	written by Nathan Searles
 *	http://nathansearles.com/loopedslider/
 *
 *	Copyright (c) 2009 Nathan Searles (http://nathansearles.com/)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *	Compatible with jQuery 1.3.2+
 *
 */

if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			loopedSlider: function(options) {
				var settings = $.extend({}, $.fn.loopedSlider.defaults, options);

				return this.each(
					function() {
					if($.fn.jquery < '1.3.2') {return;}
					var $t = $(this);
					var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;

					var distance = 0;
					var times = 1;
					var slides = $(o.slides,$t).children().size();
					var width = $(o.slides,$t).children().outerWidth();
					var position = 0;
					var active = false;
					var number = 0;
					var interval = 0;
					var restart = 0;
					var pagination = $("."+o.pagination+" li a",$t);

					if(o.addPagination && !$(pagination).length){
						var buttons = slides;
						$($t).append("<ul class="+o.pagination+">");
						$(o.slides,$t).children().each(function(){
							if (number<buttons) {
								$("."+o.pagination,$t).append("<li><a rel="+(number+1)+" href=\"#\" >"+(number+1)+"</a></li>");
								number = number+1;
							} else {
								number = 0;
								return false;
							}
							$("."+o.pagination+" li a:eq(0)",$t).parent().addClass("active");
						});
						pagination = $("."+o.pagination+" li a",$t);
					} else {
						$(pagination,$t).each(function(){
							number=number+1;
							$(this).attr("rel",number);
							$(pagination.eq(0),$t).parent().addClass("active");
						});
					}

					if (slides===1) {
						$(o.slides,$t).children().css({position:"absolute",left:position,display:"block"});
						return;
					}

					$(o.slides,$t).css({width:(slides*width)});

					$(o.slides,$t).children().each(function(){
						$(this).css({position:"absolute",left:position,display:"block"});
						position=position+width;
					});

					$(o.slides,$t).children(":eq("+(slides-1)+")").css({position:"absolute",left:-width});

					if (slides>3) {
						$(o.slides,$t).children(":eq("+(slides-1)+")").css({position:"absolute",left:-width});
					}

					if(o.autoHeight){autoHeight(times);}

					$(".next",$t).click(function(){
						if(active===false) {
							animate("next",true);
							if(o.autoStart){
								if (o.restart) {autoStart();}
								else {clearInterval(sliderIntervalID);}
							}
						} return false;
					});

					$(".previous",$t).click(function(){
						if(active===false) {
							animate("prev",true);
							if(o.autoStart){
								if (o.restart) {autoStart();}
								else {clearInterval(sliderIntervalID);}
							}
						} return false;
					});

					if (o.containerClick) {
						$(o.container,$t).click(function(){
							if(active===false) {
								animate("next",true);
								if(o.autoStart){
									if (o.restart) {autoStart();}
									else {clearInterval(sliderIntervalID);}
								}
							} return false;
						});
					}

                    $(o.container,$t).hover(function() {
                        o.stop = true;
                    }, function() {
                        if(o.hover_action)
                            return;

                        o.hover_action = true;

                        setTimeout( function() {
                            o.stop = false;

                            if( o.restart_timer == true )
                                o.restart_timer = false;
                            else
                                animate("next");

                            o.hover_action = false;
                        }, 5000 );
                    });

					$(pagination,$t).click(function(){
						if ($(this).parent().hasClass("active")) {return false;}
						else {
                            o.stop = false;
							times = $(this).attr("rel");
                            if(times == 1)
                                $('#flash_container').css({width: "676px", height: "521px"});
                            else
                                $('#flash_container').css({width: "0px", height: "0px"})
							$(pagination,$t).parent().siblings().removeClass("active");
							$(this).parent().addClass("active");
                            o.restart_timer = true;
							animate("fade",times);
							
						} return false;
					});

					if (o.autoStart) {
						sliderIntervalID = setInterval(function(){
							if(active===false) {animate("next",true);}
						},o.autoStart);
						function autoStart() {
							if (o.restart) {
							clearInterval(sliderIntervalID);
							clearInterval(interval);
							clearTimeout(restart);
								restart = setTimeout(function() {
									interval = setInterval(	function(){
										animate("next",true);
									},o.autoStart);
								},o.restart);
							} else {
								sliderIntervalID = setInterval(function(){
									if(active===false) {animate("next",true);}
								},o.autoStart);
							}
						}
					}

					function current(times) {
						if(times===slides+1){times = 1;}
						if(times===0){times = slides;}
						$(pagination,$t).parent().siblings().removeClass("active");
						$(pagination+"[rel='" + (times) + "']",$t).parent().addClass("active");
					};

					function autoHeight(times) {
						if(times===slides+1){times=1;}
						if(times===0){times=slides;}
						var getHeight = $(o.slides,$t).children(":eq("+(times-1)+")",$t).outerHeight();
						$(o.container,$t).animate({height: getHeight},o.autoHeight);
					};

					function animate(dir,clicked){
						active = true;
                        if(o.stop == true) {return;}
                        if(o.autoStart != 7000) {
                            clearInterval(sliderIntervalID);
                            o.autoStart = 7000;
                            autoStart();
                        }
						switch(dir){
							case "next":
								times = times+1;
//                                if(times == 1 || times == 6)
//                                    $('#flash_container').css({width: "676px", height: "521px"});
//                                else
//                                    $('#flash_container').css({width: "0px", height: "0px"})
								distance = (-(times*width-width));
								current(times);
								if(o.autoHeight){autoHeight(times);}
								if(slides<3){
									if (times===3){$(o.slides,$t).children(":eq(0)").css({left:(slides*width)});}
									if (times===2){$(o.slides,$t).children(":eq("+(slides-1)+")").css({position:"absolute",left:width});}
								}
								$(o.slides,$t).animate({left: distance}, o.slidespeed,function(){
									if (times===slides+1) {
										times = 1;
										$(o.slides,$t).css({left:0},function(){$(o.slides,$t).animate({left:distance})});
										$(o.slides,$t).children(":eq(0)").css({left:0});
										$(o.slides,$t).children(":eq("+(slides-1)+")").css({ position:"absolute",left:-width});
									}
									if (times===slides) $(o.slides,$t).children(":eq(0)").css({left:(slides*width)});
									if (times===slides-1) $(o.slides,$t).children(":eq("+(slides-1)+")").css({left:(slides*width-width)});
									active = false;
								});
								break;
							case "prev":
								times = times-1;
								distance = (-(times*width-width));
								current(times);
								if(o.autoHeight){autoHeight(times);}
								if (slides<3){
									if(times===0){$(o.slides,$t).children(":eq("+(slides-1)+")").css({position:"absolute",left:(-width)});}
									if(times===1){$(o.slides,$t).children(":eq(0)").css({position:"absolute",left:0});}
								}
								$(o.slides,$t).animate({left: distance}, o.slidespeed,function(){
									if (times===0) {
										times = slides;
										$(o.slides,$t).children(":eq("+(slides-1)+")").css({position:"absolute",left:(slides*width-width)});
										$(o.slides,$t).css({left: -(slides*width-width)});
										$(o.slides,$t).children(":eq(0)").css({left:(slides*width)});
									}
									if (times===2 ) $(o.slides,$t).children(":eq(0)").css({position:"absolute",left:0});
									if (times===1) $(o.slides,$t).children(":eq("+ (slides-1) +")").css({position:"absolute",left:-width});
									active = false;
								});
								break;
							case "fade":
								times = [times]*1;
								distance = (-(times*width-width));
								current(times);
								if(o.autoHeight){autoHeight(times);}
								$(o.slides,$t).children().fadeOut(o.fadespeed, function(){
									$(o.slides,$t).css({left: distance});
									$(o.slides,$t).children(":eq("+(slides-1)+")").css({left:slides*width-width});
									$(o.slides,$t).children(":eq(0)").css({left:0});
									if(times===slides){$(o.slides,$t).children(":eq(0)").css({left:(slides*width)});}
									if(times===1){$(o.slides,$t).children(":eq("+(slides-1)+")").css({ position:"absolute",left:-width});}
									$(o.slides,$t).children().fadeIn(o.fadespeed);
									active = false;
								});
								break;
							default:
								break;
							}
						};
					}
				);
			}
		});
		$.fn.loopedSlider.defaults = {
			container: ".container", //Class/id of main container. You can use "#container" for an id.
			slides: ".slides", //Class/id of slide container. You can use "#slides" for an id.
			pagination: "pagination", //Class name of parent ul for numbered links. Don't add a "." here.
			containerClick: true, //Click slider to goto next slide? true/false
			autoStart: 1000, //Set to positive number for true. This number will be the time between transitions.
			restart: 1000, //Set to positive number for true. Sets time until autoStart is restarted.
			slidespeed: 300, //Speed of slide animation, 1000 = 1second.
			fadespeed: 200, //Speed of fade animation, 1000 = 1second.
			autoHeight: 0, //Set to positive number for true. This number will be the speed of the animation.
			addPagination: false, //Add pagination links based on content? true/false
            stop: false,
            hover_action: false,
            restart_timer: false
		};
	});
}


/*************************************************************
 * SearchL
 *
 *************************************************************/

(function($){
    $.fn.SearchL = function() {
        return this.each(function() {
            var select = $(this);
            var select_id = $(select).attr('id');

            // schowaj html'owego selecta
            $(select).hide();

            var current_value = $(select).find('option:selected').text();
            var div_layer = $('<div/>').addClass('js_select').attr('id', 'js_select_' + select_id).html(current_value + '<span class="select_search_arrow"></span>');
            var div_elements = $('<div/>').addClass('js_select_search_elements').attr({rel: select_id, id: 'js_select_elements_' + select_id});

            div_layer.insertAfter(select);
            div_elements.insertAfter($('#js_select_' + select_id)).hide();
            
            var select_options = $(select).children('option');

            $.each(select_options, function(i){
                var option = $(this);
                var element_span = $('<span/>').addClass('js_select_elements_span').attr('value', option.val()).html(option.text());

                element_span.appendTo($('#js_select_elements_' + select_id));
            });

            // wyswietlanie dropdowna
            $('#js_select_' + select_id).live('click', function() {
                $('#js_select_elements_' + select_id).show();
            });

            // chowanie dropdowna
            $('.in').hover(function() {}, function(){
                $('#js_select_elements_' + select_id).hide();
            });

            // wybieranie elementu z dropdowna
            $('#js_select_elements_' + select_id).find('.js_select_elements_span').live('click', function(){
                span_clicked = $(this);

                // ukryj dropdown
                $('#js_select_elements_' + select_id).hide();

                // zazancz nowa wartosc w tagu select
                span_clicked_value = $(span_clicked).attr('value');
                $(select).val(span_clicked_value);

                // zaktualizuj wartosc layera
                $(div_layer).html($(select).find('option:selected').text() + '<span class="select_search_arrow"></span>');
            });

        });
    }
})(jQuery);


