/**
 * author: Dariusz Pobożniak
 * website: http://pobozniak.pl
 */
 
$(function() {
    imgCycle();
    sideNavigation();
    equalHeight();
    curveImgs();
    basketProdCount();
    formLegend('.handiform');
    autoComplete();
    completeAddress();
    slideFieldset();
    transactionsList();
    multimenu();
    newsletterBar();
    loginPanel();
    
    $('.lightbox').fancybox();
    $('.inline').fancybox({
        'hideOnContentClick': false,
        'frameWidth': 540,
        'frameHeight': 315
    });
    //$('#sidebar dl').lessMore();
    $('#multimenu ul')
        .superfish()
        .find('a[href="witamy-strona-glowna-info.html"]').attr('href', 'index.html');
    $('#fsearch, #newsletteremail').placeholder();
});

function newsletterBar() {
    $('#emailoption').hide();
    var value = $('#multimenu #newsletteremail').val();
    $('#multimenu fieldset').not('#emailoption').click(function() {
        $('#emailoption').slideDown(300);
    })
    $('#closeemail').click(function() {
        $('#emailoption').slideUp(300);
    })
    $('#multimenu #newsletteremail').focus(function() {
        $(this).val('');
    }).blur(function() {
        if ($(this).val() == '') {
            $(this).val(value);
        }
    })
}


function imgCycle() {
    $('#prodbox').append('<div id="nav">');
    $('#prodcyc').cycle({fx: 'fade', speed: 300, timeout: 4000, pager: '#nav' });
    $('#slider img').each(function() {
        var src = $(this).attr('src');
        $(this).parent().css({'background': 'url('+src+') 50% 50% no-repeat'});
        $(this).remove();
    })
    /*
$('#slider').append('<div id="controls">');
    $('#slider ul').cycle({fx: 'fade', speed: 300, timeout: 8000, pager: '#controls' });
*/
}

function curveImgs() {
    $('.productview .img, .recomlist a, .gallery-box a').each(function() {
        var $this = $(this),
            $img = $this.find('img');
        $this.css('background', 'url('+$img.attr('src')+') 50% 50% no-repeat');
        $img.remove();
    })
}

function basketProdCount() {
    $('.quantity').keypress(function(e) {
        if (e.which != 8 && e.which != 0 && (e.which<48 || e.which>57)) {
            return false;
        }
    });
    $('.minus').click(function() {
        var $thisinput = $(this).next();
        var $newval = parseInt($thisinput.val()!='' ? $thisinput.val() : 0)-1;
        if ($newval < 0) {
            $thisinput.val(0);
        } else {
            $thisinput.val($newval);
        }
    });
    $('.plus').click(function() {
        var $thisinput = $(this).prev();
        $thisinput.val(parseInt($thisinput.val()!='' ? $thisinput.val() : 0)+1);
    });
}

function formLegend(el) {
    $(el + ' fieldset legend').each(function() {
        var legendtxt = $(this).html();
        $(this).after('<h4>' + legendtxt + '</h4>');
        $(this).hide();
    })
}

function sideNavigation() {
    var $clear = $('<span class="clear" title="Wyczyść filtr">zamknij</span>');
    $('#sidenav ul').addClass('subnav');
	$('#sidenav ul ul').hide();
	$('#sidenav .active').parents('ul').removeClass().show()
	   .end().parent().parent().addClass('subnav').show()
	   .end().parents('ul').not('#sidenav').not('#sidenav > li > ul').before($clear)
	   .end().parents('li').siblings().not('.top').hide();

	$('#sidenav a').click(function() {
		var $par_li = $(this).parent();
		var $clear = $('<span class="clear" title="Wyczyść filtr">zamknij</span>');
		
		if ($par_li.find('ul').length > 0) {
            $(this).addClass('title');
			$par_li.siblings().not('.top').hide();
			$(this).next('.clear').remove().end().after($clear);
			if ($par_li.parent().hasClass('subnav')) {
				$par_li.parent().removeClass('subnav');
			}
		
			$par_li.find('ul:eq(0) > li').show().parent().addClass('subnav').show()
				.find('ul').removeClass('.subnav').hide();
            $('.subnav .clear').remove();
            $('#sidenav > li > .clear').remove();
			return false;
		}
	});
	$('#sidenav .clear').live('click', function() {
	   $(this).next('ul').removeClass('subnav').hide();
	   $(this).parent().siblings().show();
	   $(this).parent().parent().addClass('subnav');
	   $('.subnav .clear').remove();
	});
}

function equalHeight() {
    //$('.column').css({'height':''});
	$('.productview li').setEqualHeight();
}

jQuery.fn.setEqualHeight=function(o) {
    var maxHeight=0;
    var maxElement=null;
    	jQuery(this).each(function(i) {
      		if((jQuery(this).height()+parseInt(jQuery(this).css("padding-bottom"))+parseInt(jQuery(this).css("padding-top")))>maxHeight) {
    		maxHeight=jQuery(this).height()+parseInt(jQuery(this).css("padding-top"))+parseInt(jQuery(this).css("padding-bottom"));
    		maxElement=this;
    	}
    });
    //jQuery(this).not($(maxElement)).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
    jQuery(this).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
}

function autoComplete() {
    $('.autocomplete').each(function(){
        $(this).autocomplete($(this).attr('rel'), {
            selectFirst: false
        });
    });
}

function completeAddress() {
    $('#deaddress').change(function() {
        var arr = $(this).val().split(':');
        var arrlength = arr.length;
        $(this).parent().nextAll().find('input').each(function(index) {
            $(this).val(arr[index]);
        })
    })
    $('#fvaddress').change(function() {
        var arr = $(this).val().split(':');
        var arrlength = arr.length;
        $(this).parent().nextAll().find('input').each(function(index) {
            $(this).val(arr[index]);
        })
    })
    showHideFv($('#fv'));
    $('#fv').click(function() {
        showHideFv($(this));
    })

}

function showHideFv(el) {
    if ($(el).is(':checked')) {
        $(el).parent().siblings('p').show();
    } else {
        $(el).parent().siblings('p').hide();
    }
}

function slideFieldset() {
    $('fieldset.slide p').hide();
    $('fieldset.slide h4').click(function() {
        if ($(this).siblings('p').is(':visible')) {
            $(this).siblings('p').slideUp(400);
        } else {
            $(this).siblings('p').slideDown(400);
        }
        return false;
    });
    $('fieldset.slide .add').click(function() {
        if ($(this).siblings('p').is(':visible')) {
            $(this).siblings('p').slideUp(400);
        } else {
            $(this).siblings('p').slideDown(400);
        }
        return false;
    })
}

function transactionsList() {
	if ($('table.transaction').length > 0) {
		$('table.transaction:gt(2)').hide();
		$('#content h4').each(function() {
			$(this).css({'cursor': 'pointer'});
			$(this).click(function() {
				$(this).next('.transaction').toggle();
			})
		})

	}
}

function multimenu() {
    $('#multimenu > ul > li > a').each(function() {
        var $this = $(this);
        var $li = $this.siblings('ul').children('li');
        if ($li.length > 1) {
            $value = $(this).text();
            $span = '<span>' + $value + '</span>';
            $(this).after($span);
            $(this).remove();
        } else {
            $this.attr('href', $li.find('a').attr('href'));
            $li.parent().remove();
        }
    });
    $('#multimenu a[href="agro-sur-info.html"]').attr('href', 'http://agrosur.com.pl').attr('target', '_blank');

} 

function loginPanel() {
	var cookie_name = 'handiTopPanel';
	var panel = $.cookies.get(cookie_name);
	if (panel == 'collapsed') {
        $('#toppanel').css({top:"-138px"});
		$('#wrapper').css({top:"2px"});
		$('#logintab').toggle(function() {
			// slide panel down
			$('#toppanel').animate({top:"0px"},500);
			$('#wrapper').animate({top:"140px"},500);
			$.cookies.set(cookie_name, 'expanded');
		}, function() {
			// slide panel up
			$('#toppanel').animate({top:"-148px"},500);
			$('#wrapper').animate({top:"2px"},500);
			$.cookies.set(cookie_name, 'collapsed');
		});
	} else {
		$('#toppanel').css({top:"0px"});
		$('#wrapper').css({top:"140px"});
        $('#logintab').toggle(function() {
        	// slide panel up
			$('#toppanel').animate({top:"-138px"},500);
			$('#wrapper').animate({top:"2px"},500);
			$.cookies.set(cookie_name, 'collapsed');
		}, function() {
			// slide panel down
			$('#toppanel').animate({top:"0px"},500);
			$('#wrapper').animate({top:"140px"},500);
			$.cookies.set(cookie_name, 'expanded');
		});
	}
    $('#tp-close').click(function() {
		// slide panel up
		$('#toppanel').animate({top:"-138px"},500);
		$('#wrapper').animate({top:"2px"},500);
		$.cookies.set(cookie_name, 'collapsed');
    });
}

(function() {
	$.fn.placeholder = function(customOptions) {
		var options = {
			pclass: 'placeholder' 
		},
		options = $.extend({}, options, customOptions);
		
		var fakeInput = document.createElement('input'),
			phsupport = ('placeholder' in fakeInput);
			
		return this.each(function() {
			if (!phsupport) {
				var field = $(this),
					orgText = field.attr('placeholder');
				field
					.val(orgText)
					.addClass('placeholder')
					.bind({
						focus: function() {
							field.removeClass('placeholder');
							clearValue();
						},
						blur: function() {
							if ($(this).val().length === 0) {
								$(this).val(orgText);
								$(this).addClass('placeholder');
							}
						}
					})
					.parents('form').submit(function() {
						clearValue();
					});
				
				function clearValue() {
					if (field.val() === orgText) {
						field.val('');
					}
				};
			}
		});
	};
})(jQuery);




