$(document).ready( function() {

	// Register rules ->
	$('.rules-accept').click (function(e) {
		$('.rules-add').hide();
		$('#rules').attr('checked',true);
    });

    $('#SubmitLink').click(function(e){
    	if($('#rules').attr('checked') == true){
    		e.preventDefault()
        	$('#RegForm').submit()
        }
    	else {
    		e.preventDefault()
    		$('.rules-add').show();
    		}
    });
	// <- Register rules
	
	
	// Menu lewe ->
	$('.mainMenuCategory').live('click', function(e) {

		e.preventDefault();
		
		var el = $(e.target);
		
		if ( el.parent().hasClass('active')) {
			el.next('ul').remove();
			el.parent().removeClass('active')
			return;
		} 
		var id = el.attr('id').split('-');

		id = id[1];

		$.get("/menu/main/" + id, function(data){
			  el.parent().append(data);
			  el.parent().addClass('active');
			});

	});

	$('.expand').live('click', function(e) {

		var el = $(e.target);

		if (el.parent().hasClass('expand') == false) {
			return;
		}

		e.preventDefault();
		
		el.parent().removeClass('expand');
		el.next('ul').remove();
		el.addClass('subCatExpand');
	});
	
	$('.subCatExpand').live('click', function(e) {

		e.preventDefault();

		var el = $(e.target);
		el.parent().addClass('expand');
		el.removeClass('subCatExpand');

		var id = el.attr('id').split('_');

		id = id[1];
		
		$.get("/menu/sub/" + id, function(data){
			  el.parent().append(data);
			  el.parent().addClass('active');
			});
	});

	// <- Menu lewe
	
	
	// Galeria firmy ->
    var nr = 1; 
    var max = $('#photoAll').html();
    
    $('#photo_'+nr).hide();
    
    $('.photoCycle').click( function(e) {

        e.preventDefault();
        var el = $(e.target);
        
        $('#photoMain').attr('src', el.attr('src').replace('_s', '_b'));
        var id = el.parent('a').attr('id').split('_');
        $('#photo_'+id[1]).hide();
        $('#photo_'+nr).show();
        ChangeClass(id[1]);

        nr = id[1];
    });

    $('#NavPrev').click( function(e) {
        e.preventDefault();
        var id = $('#photoCurrent').html();
        if(id > 1){
            id--;
            $('#photoMain').attr('src', $('#photo_'+id+' img').attr('src').replace('_s', '_b'));
            $('#photo_'+id).hide();
            $('#photo_'+nr).show();
            ChangeClass(id);
            nr = id;
        }
    });

    $('#NavNext').click( function(e) {
        e.preventDefault();
        var id = $('#photoCurrent').html();
        if(id < max){
            id++;
            $('#photoMain').attr('src', $('#photo_'+id+' img').attr('src').replace('_s', '_b'));
            $('#photo_'+id).hide();
            $('#photo_'+nr).show();
            ChangeClass(id);
            nr = id;
        }

    });
    
    function ChangeClass(nr){
        if (nr == max) { $('#NavNext').addClass('next-disable'); }
        else { $('#NavNext').removeClass('next-disable'); };
        
        if (nr == 1) { $('#NavPrev').addClass('prev-disable'); }
        else { $('#NavPrev').removeClass('prev-disable'); };
        
        $('#photoCurrent').text(nr);
    }
	// <- Galeria Firmy
	
	
	// Newsletter ->
	$('#newsletterForm').submit( function(e) {
		e.preventDefault();
		$('#newsletterBox').hide();
		$.post('/zapisz-sie-do-newslettera', {'mail':$('#emailNewsletter').val()}, function(data) {

			$('.newsletter-info').hide();
			if (data.status == 'error') {
				$('#newsletterNext').show();
				$('#newsletterInfo').show();
				$('#newsletterInfo').html(data.error);
				
			} else {
				$('#newsletterInfo').show();
				$('#newsletterInfo').html(data.msg);
			}
			}, "json");
	});

	$('#newsletterNext').click( function(e) {
		
		$('#emailNewsletter').val('Wpisz swój e-mail...');
		$('#newsletterNext').hide();
		$('#newsletterInfo').hide();
		$('#newsletterBox').show();

		e.preventDefault();
	});

	$('#emailNewsletter').focus( function() {

		if ($('#emailNewsletter').val() == 'Wpisz swój e-mail...') {
			$('#emailNewsletter').val('');
		}
	});

	$('#emailNewsletter').blur( function() {
		if ($('#emailNewsletter').val() == '') {
			$('#emailNewsletter').val('Wpisz swój e-mail...');
		}
	});
	// <- Newsletter
	
	
	// Contest sidebar ->
	if ($(".contestMini").length > 0){

		var ContestTable = new Array();
		
		$('.contestMini').each(function(e) {
			ContestTable.push( $(this) );
		});
		
	    var contest_temp = 0;
	    var contest_max = ContestTable.length-1;
	    
	    function ContestSidebar() {
	    	ChangeContest(ContestTable[contest_temp]);
	
	    	if(contest_temp == contest_max) {
	    		contest_temp = 0;
	    	} else {
	    		contest_temp++;
	    	}
	    }
	
	    function ChangeContest(object){
	    	var el = object;
			$('.contestMini').removeClass('active');
			$('#contestTitle').text(el.attr('title'));
			$('#contestData').html(el.children('.contestBody').text());
	
			var id = el.attr('id').split('_');
			el.addClass('active');
	
			$('#contestImg').attr('src', '/images/contest/avatar/avatar_' + id[1] + '_medium.jpg');
	
			$('#contestTitle').attr('href', 'http://www.nowekosmetyki.pl/konkursy-pokaz/' + id[1]);
			$('#contestMiniLink').attr('href', 'http://www.nowekosmetyki.pl/konkursy-pokaz/' + id[1]);
	    	
	    }
	    
	    
	    var ContestSlide = setInterval( ContestSidebar, 3000);
	    
		$('.contestMini').click ( function(e) {
			clearInterval(ContestSlide);
			e.preventDefault();
			ChangeContest($(e.target));
		});
	}
	
	// <- Contest sidebar
	
	
	// Superbox ->
	
    var name = ['Product', 'Advice', 'Trends', 'Events', 'Poll', 'Contest', 'Bargain'];
    var old = 0;
    var next = 1;
    var b_cycle = 1;
    var b_max = $('#BargainCount').html();
    var c_cycle = 1;
    var c_max = $('#ContestCount').html();

    function BargainCycle() {
    	$('.BargainBox').hide();
    	$('#Bargain'+b_cycle).show();
    	if(b_cycle < b_max){ b_cycle++; }
    	else {b_cycle = 1;}
    }

    function ContestCycle() {
    	$('.ContestBox').hide();
    	$('#Contest'+c_cycle).show();
    	if(c_cycle < c_max){ c_cycle++; }
    	else {c_cycle = 1;}
    }

    
	$('.advMenu').click( function(e) {
		clearInterval(slide);
		e.preventDefault();
		$('.active').removeClass('active');

		var el = $(e.target);
		
		el.parent('li').addClass('active');
		var id = el.parent('li').attr('id').split('_');
		$('.advBox').hide();	

		$('#adv' + id[1]).show(); 
	});

    function advBox() {
        var prev = name[old]; 
        var adv = name[next];
        $('#adv' + prev).hide();
        $('#menu_' + prev).removeClass('active');
        
        $('#adv' + adv).show();
        $('#menu_' + adv).addClass('active');
        if(adv == 'Bargain'){ BargainCycle(); }
        if(adv == 'Contest'){ ContestCycle(); }
        
        old++;
        if (old == name.length) { old = 0; } 
        next++;
        if (next == name.length) { next = 0; } 
    }

     var slide = setInterval( advBox, 3000);
     
    // <- Superbox
     
	$('#search_drop').change(function() {
		var a = '#';
		var i = $('#search_drop').val();
		switch (i)
	       {
	    	    case "1": a = '/wyszukiwarka-produktow';break;
                case "2": a = '/artykuly-porady';break;
                case "3": a = '/artykuly-trendy';break;
                case "4": a = '/artykuly-wydarzenia';break;
                case "5": a = '/forum-szukaj/wszystkie';break;
                case "6": a = '/wyszukiwarka-blogow';break;
                case "7": a = '/firmy';break;
                case "8": a = '/galerie';break;
                case "9": a = '/wyszukaj-uzytkownikow';break;

	       }
		$('#SearchBox').attr('action', a);
		}); 
	
	$('#recommendNext').click (function(e) {
		
		e.preventDefault();
		$('#recommendInfo').hide();
		$('#recommendData').show();
		$('#recommendMsg').text('');
		$('#recommendField').val('');
		
	});
	
	$('#recommendLink').click (function(e) {
		
		e.preventDefault();

		if ('' == $('#recommendFriendField').val()) {
			$('#recommendFriendField').css('border', '1px solid red');
			return;
		} else {
			$('#recommendFriendField').css('border', '1px solid black');
		}

		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

		if ('' == $('#recommendField').val() || reg.test($('#recommendField').val()) == false) {
			$('#recommendField').css('border', '1px solid red');
			return;
		} else {
			$('#recommendField').css('border', '1px solid black');
		}
		
		var el = $(e.target);
		
		var id = el.attr('class').split('-');

		var params = { "type": id[0],
						'id' : id[1],
						'name': $('#recommendFriendField').val()
						, 'addr': $('#recommendField').val()}
		
		
		$.post("/zarekomenduj" + "?random=" + Math.random()*999999, params,
				   function(data){
					 
					$('#recommendInfo').show();
					$('#recommendData').hide();
					$('#recommendMsg').text(data.msg);
				   }, "json");

		
		
	});
	
	$('.required').each( function(e) {
		var value = $(this).html();
		value = value + ' *';
		$(this).text(value); 
	});
	
	
	$('.checkAll').click( function(e) {
		e.preventDefault();
		$('.inboxCheckbox').attr('checked', true);

	});
	
	$('.uncheckAll').click( function(e) {
		e.preventDefault();
		$('.inboxCheckbox').attr('checked', false);

	});
	
$('.poption-clip').click( function(e) {
	
	e.preventDefault();
	
	var el = $(e.target);
	
	var id = el.attr('id').split('-');
	
	if (id[0] == 'artykul') {
		
		$.getJSON("/artykul-notatnik/" + id[1],
		        function(data){
			
				//el.remove();
		});
	} else if(id[0] == 'produkt') {
		$.getJSON("/produkt-notatnik/" + id[1],
		        function(data){
			
				//el.remove();
		});
	}
	
});

$('.vote').click( function(e) {
	
	
	var id = $(e.target).attr('id').split('_');
	
	$.getJSON("/ranking-zaglosuj/" + id[2] + "/" + id[1],
	        function(data){ 
		
		if (data.status == 'success') {
			
			$('#vote_1_counter').text(data.votes1);
			$('#vote_2_counter').text(data.votes2);
			
			$('.vote').remove();
		}
		
	});
	
	e.preventDefault();
});

$('#categories').change( function(e) {
	
	var el = $(e.target);
	
	if ('' != el.val()) {
		
		$.getJSON("/pobierz-kategorie/" + el.val(),
		        function(data){ 
			
			$('#products').html(data.msg);
			
		});
	} else {
		$('#products').html('<option value="">wybierz kategorię</option>');
	}
	
});

$('.poption-vote').click( function(e) {
	e.preventDefault();
	$('#formRating').slideToggle("normal");
});
		
$('.poption-comment').click( function(e) {
	e.preventDefault();
	$('#formRating').slideToggle("normal");
});

$('#articleLike').click (function(e) {
	
	var el = $(e.target);
	
	var id = el.parents('.column').attr('id').split('-');

	if (el.hasClass('likeable')) {
		
		$.getJSON("/artykul-lubie/" + id[1] + "/1",
		        function(data){
			
				if (data.status == 'success') {
					el.text(data.msg);
				} else {
					
				}
		});

		el.removeClass('likeable');
		$('#articleUnlike').removeClass('likeable');
	}
});

$('#articleUnlike').click (function(e) {
	
	var el = $(e.target);
	
	var id = el.parents('.column').attr('id').split('-');
	
	if (el.hasClass('likeable')) {
		
		$.getJSON("/artykul-lubie/" + id[1] + "/0",
		        function(data){
			
				if (data.status == 'success') {
						el.text(data.msg);
				} else {
					
				}
		});

		el.removeClass('likeable');
		$('#articleLike').removeClass('likeable');
	}
});


});



$(document).ready(function(){
	
	$('#select_beauty, #select_spa, #select_brands, #search_drop').sSelect({ddMaxHeight: '300px'});

	$('#rating').hide();
	$(".option-vote").click(function () {
		$("#add-comment").hide();
		$("#share").hide();
		$("#rating").slideToggle("normal");
	});  
	
	$("#add-comment").hide();
	$(".option-comment").click(function (e) {
		e.preventDefault();
		$("#rating").hide();
		$("#share").hide();
		$("#add-comment").slideToggle("normal");
	}); 
	
	$("#share").hide();
	$(".option-share").click(function () {
		$("#rating").hide();
		$("#add-comment").hide();
		$("#share").slideToggle("normal");
	}); 
	
	$(".poption-share").click(function (e) {
		e.preventDefault();
		$("#rating").hide();
		$("#add-comment").hide();
		$("#share").slideToggle("normal");
	});
	
	$("#poption-foto-vote").hide();
	$('.poption-foto-vote').click( function(e) {
		e.preventDefault();
		$('#rating').show();
		$("#poption-foto-comment").hide();
		$("#poption-foto-comments").hide();
		$('#poption-foto-vote').slideToggle("normal");
	});

	$('.poption-foto-votes').click( function(e) {
		e.preventDefault();
		$('#rating').show();
		$("#poption-foto-comment").hide();
		$("#poption-foto-comments").hide();
		$('#poption-foto-vote').slideToggle("normal");
	});
	
	$('.poption-foto-rates').click( function(e) {
		e.preventDefault();
		$('#rating').show();
		$("#poption-foto-comment").hide();
		$("#poption-foto-comments").hide();
		$('#poption-foto-vote').slideToggle("normal");
	});

	$("#poption-foto-comment").hide();
	$('.poption-foto-comment').click( function(e) {
		e.preventDefault();
		$("#poption-foto-vote").hide();
		$("#poption-foto-comments").hide();
		$('#poption-foto-comment').slideToggle("normal");
	});
	
	$("#poption-foto-comments").hide();
	$('.poption-foto-comments').click( function(e) {
		e.preventDefault();
		$("#poption-foto-vote").hide();
		$("#poption-foto-comment").hide();
		$('#poption-foto-comments').slideToggle("normal");
	});
	
	$('.zoom').preview();
	
	$('.invite-about, .invite-email').focus( function() {
		if (this.value == this.defaultValue){this.value = '';}
		if(this.value != this.defaultValue){this.select();}
	});
		
	$('.invite-about, .invite-email').blur( function() {
		if ($.trim(this.value) == ''){
		this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	/*
	// multiple select without ctrl
       var selected = new Array();

	   $('select').mouseover(function() {
	      if (this.multiple == true) {
	        for (var i=0,a=0;i<this.options.length;i++) {
	          if (this.options[i].selected == true) {
	            selected[a] = this.options[i].value;
	            a++;
	          }
	        }
	      }
	    });
	 
	    // safe them when you click the mouse
	    $('select').click(function() {
	      // make sure it's a multiple select
	      if (this.multiple == true) {
	        for(var i=0;i<selected.length;i++) {
	          for(var a=0;a<this.options.length;a++){
	            if (selected[i] == this.options[a].value && this.options[a].selected == true) {
	              this.options[a].selected = false;
	              selected.splice(i,1);
	            } else if (selected[i] == this.options[a].value) {
	              this.options[a].selected = true;
	            }
	          }
	        }
	      }
	    
	      // load all selected options in array when the mouse pointer hovers the select box
	      if (this.multiple == true) {
	        for (var i=0,a=0;i<this.options.length;i++) {
	          if (this.options[i].selected == true) {
	            selected[a] = this.options[i].value;
	            a++;
	          }
	        }
	      }
	    
	    });
*/
	    
});	

// zoom->
	$(document).ready(function() {
		
	});

	(function($){
		   
	    $.fn.preview = function(options) {
	        var defaults = {
	             speed: 500            // domyslne ustawienie szybkosci pojawienia sie podgladu
	          };
	 
	        var options = $.extend(defaults, options);    //laczy wartosci domyslne z wprowadzonymi
	   
	          return this.each(function() {
	             
	              $(this).hover(function(m){
	            	  var src = this.src;
	            	  var temp = src.substr(0,this.src.length -4);
	                  $("body").append("<p id='prebox'><img src='"+ temp +"s.jpg'/></p>");   
	                $("#prebox")
	                    .css("top",(m.pageY-15) + "px")   
	                    .css("left",(m.pageX+15) + "px")
	                    .fadeIn(defaults.speed);
	                },
	                function(){
	                    $("#prebox").remove();
	                });   
	   
	                $(this).mousemove(function(m){
	                $("#prebox")
	                    .css("top",(m.pageY-15) + "px")
	                    .css("left",(m.pageX+15) + "px");
	                });   
	        });
	        };
	    
	    
	})(jQuery);
	
// <-zoom


// OCENIANIE
var sMax;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;

// onmouseover
function rating(num){
	sMax = 0;	// Maksymalna ocena
	for(n=0; n<num.parentNode.childNodes.length; n++){
		if(num.parentNode.childNodes[n].nodeName == "A"){
			sMax++;	
		}
	}
	
	if(!rated){
		s = num.id.replace("_", ''); // Get the selected star
		a = 0;
		for(i=1; i<=sMax; i++){		
			if(i<=s){
				document.getElementById("_"+i).className = "on";
				document.getElementById("rateStatus").innerHTML = num.title;	
				holder = a+1;
				a++;
			} else {
				document.getElementById("_"+i).className = "";
			}
		}
	}
	
	
}

// onmouseout
function off(me){
	
	if(!rated){
		for(i=1; i<=sMax; i++){		
			document.getElementById("_"+i).className = "";
			document.getElementById("rateStatus").innerHTML = me.parentNode.title;
		}
	}
}

// Akcje po kliknieciu
function rateIt(me, id){
	
	if(!rated){

		var ids = $('#rating').attr('class').split('-');
		var params = {'vote':id}
		
		if (ids[0] == 'photoable') {
			$.post("/firma-zdjecie-ocen/" + ids[1], params,
					  function(data){
					 
						if (data.status = 'success') {
							$('#rating').html('Dziękujemy za oddanie głosu');
							$('#ratingInfo').html(data.msg);
						}
					 
					  }, "json");
		} else if(ids[0] == 'movie') {
			$.post("/firma-film-ocen/" + ids[1], params,
					  function(data){
					 
						if (data.status = 'success') {
							$('#rating').html('Dziękujemy za oddanie głosu');
							$('#ratingInfo').html(data.msg);
						}
					 
					  }, "json");
		
		} else if(ids[0] == 'gallery') {
			$.post("/galeria-zaglosuj/" + ids[1], params,
					  function(data){
					 
						if (data.status = 'success') {
							$('#rating').html('Dziękujemy za oddanie głosu');
							$('#ratingInfo').html(data.msg);
						}
					 
					  }, "json");
		} else if(ids[0] == 'blog') {
			$.post("/blog-zaglosuj/" + ids[1], params,
					  function(data){
					 
						if (data.status = 'success') {
							$('#rating').html('Dziękujemy za oddanie głosu');
							$('#ratingInfo').html(data.msg);
						}
					 
					  }, "json");
		} else if(ids[0] == 'video') {
				$.post("/video-zaglosuj/" + ids[1], params,
						  function(data){
						 
							if (data.status = 'success') {
								$('#rating').html('Dziękujemy za oddanie głosu');
								$('#ratingInfo').html(data.msg);
							}
						 
						  }, "json");
		} else {
			$.post("/artykul-ocen/" + ids[1], params,
				  function(data){
				 
					if (data.status = 'success') {
						$('#rating').html('Dziękujemy za oddanie głosu');
						$('#ratingInfo').html(data.msg);
					}
				 
				  }, "json");
		
		}
		

	}
}

