jQuery.fn.rdy = function(func){
	this.length && func.apply(this);
	return this;
};
jQuery(document).ready(function($){
	$('html').removeClass('no-js');

	$('body').filter(function(){ return $('#ropeSteps').length ? this : '' }).addClass('ropeStepsIn');

	$('.pageShadow').rdy(function(){
		var content = $('#content'),
				height = parseInt($(window).height() - content.position().top - content.outerHeight(true),10);

		$(this).css('height',height + 100);

	});

	function element_margin(el,index){
		var child = el.children();
		
		child.each(function(){
			var i = $(this).index()+1;
			if (i%index==0) {
				$(this).css('margin-right',0);
			};
		});
		
	}
	
	element_margin($('.listingNews'),3);
	element_margin($('.recipsesList'),4);
	
	//Full background ie
	
	$('body').rdy(function(){
		var ie = $.browser,
				curBg = $(this).attr('style');
				
			curBg = curBg.split('(');
			curBg = curBg[1].split(')'); 

		if (ie.msie) {
			$(this).css({
				"filter" : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+curBg[0]+"', sizingMethod='scale')",
				"-ms-filter" : "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+curBg[0]+"', sizingMethod='scale')"			
			});			
		};		
	});
	
	
	//Ntz Infinite Carousel
	
	(function( $ ){
		$.fn.ntz_infiniteCarousel = function() {
			return this.each(function(){
				var carousel = $(this),
						etalon = $('li:first', carousel);
	 
				carousel.wrap('<div class="ntz_infiniteCarouselWrap"></div>');
	 
				var w = carousel.closest('.ntz_infiniteCarouselWrap'), //the wrap
						n = $('<nav class="pageArrows"><a href="#" class="sprite block prev" /><a href="#" class="sprite block next" /></nav>'), // the navigation
						inc = etalon.outerWidth(true), // increment - the width of an element ( is the same amount of px to be scrolled )
						ul = $('ul', carousel),
						perPage = Math.ceil( ul.parent().width()/inc ), // how many elements are displayed per page
						fX = $('li:lt(' + perPage + ')', ul).clone(), // first and last X elements ( x = perPage)
						lX = $('li:gt('+ ( $('li', ul).length - perPage-1 ) +')', ul).clone(),
						s = $('li.s', carousel); // selected
				w.addClass( carousel.attr('id') );
		

				ul.append(fX).prepend(lX); // adding elemnts on begining and the end for infinite loop

				var ulW = $('li', ul).length * inc, // width for ul
						maxPos = -1*(ulW - inc*perPage - perPage); // max position

				ul.width( ulW ).css({marginLeft:-inc*perPage});

				n.appendTo(w);

				if ($('li',ul).length - fX.length - lX.length <= perPage ) {

					fX.add(lX).add(n).remove();

					ulW = $('li', ul).length * inc;
					ul.width(ulW).css({marginLeft : 'auto', marginRight : 'auto'});

				}


				$('a',n).click(function(){
					var t = $(this),
							curPos = parseInt(ul.css('marginLeft'), 10);
	 
					if( ul.is(':animated') ) {return false;} // we don't need multiple scrolls
	 
					if( t.hasClass('prev') ){ // scroll left
						if( curPos >= 0 ){
							ul.css({ marginLeft: -( ulW - inc * perPage*2 ) }); // infinite loop
						}
						ul.animate({ marginLeft:'+='+inc })
					}
	 
					if ( t.hasClass('next') ) { //scroll right
						if( maxPos >= curPos ){
							ul.css({ marginLeft: - inc * perPage }); // infinite loop
						}
						ul.animate({ marginLeft:'-='+inc });
					}
					return false;
				});
				
	 
			});
		}; // ntz_infiniteCarousel
	})( jQuery );
	
});

$(document).ready(function () {

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect     
        $('#mask').fadeTo(500, 0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        //transition effect
        $(id).fadeIn(2000);

    });

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });

});
