(function($) {
    $(document).ready(function() {
	
		var $body = $('body'),
			$sidebar = $('aside.sidebar'),
			$comments = $('.comment-wrapper');
		

        $('#cycle-featured .inner-wrap div').cycle({
            fx: 'scrollHorz',
            pager: '#cycle-nav',
            timeout: 5000,
            speed: 800
        });
		
		$('#scorller').stickySidebar({
			duration: 200,
			padding: 50
		});

        var cn = $('#cycle-nav');
        cn.css({'float' : 'none', 'width' : cn.width()});

        $('article.item dl dd.image, article.item dl dt, article.item div.image, div.imagecol').hover(
            function() {$(this).parent().addClass('hover');},
            function() {$(this).parent().removeClass('hover');}
        );

        $('a[rel="external"]').attr("target", "_blank");

        $.fn.balanceHeights('.article-wrap', 'dt', 3);
        $.fn.balanceHeights('.article-wrap', 'article.item', 3);

        $('h2.entry-title').setMinHeight(2);
        $('.default_product_display .wpsc_product_price').setMinHeight(2);
        $('.default_product_display form.product_form').setMinHeight(2);
		
		if( $('#single_product_page_container').length !== 0 ) {
			$body.addClass('checkout');
			$sidebar.hide(); $comments.hide();
		}
		
		$.fn.validateFields();
		
		/*$('form.wpsc_checkout_forms').submit( function(e) {
			var errors = $(this).find('.LV_invalid_field');
			
			if ( errors.length == 0 ) {
				var product_list = "",
					data_list = "";
					
				product_list = $('table.checkout_cart').html();
				
				$('table.wpsc_checkout_table td').each( function(index) {
					var tdName = $(this).attr("class");
						//subs = name.substr(0, 19);
						
					data_list = "";
					console.log(tdName.substr(0, 19));
				});
				
				Shadowbox.open({
					content:    '<div id="verify">\n\
									<p>Te rugam sa revizuiesti comanda ta si datele personale pentru a evita orice neplacere.</p>\n\
									<p>Pentru CD-ul si tricoul DOC - "Lucrare de control" procesarea comenzilor (predarea catre Oficiul Postal Bucuresti pentru a pleca) incepe pe data de 18 Octombrie 2011.<br/>\n\
									   <strong>ATENTIE: In cazul celor care nu au ridicat comenzi in trecut (atat de pe deliric1.ro cat si de pe site-urile partenere) comenzile acestora nu vor fi luate in considerare.</strong></p>\n\
									<div class="confirm_products">' + product_list + '</div>\n\
									<div class="confirm_data">' + data_list + '</div>\n\
								 </div>',
					player:     "html",
					title:      "Confirma comanda",
					width:      600,
					height:		700
				});
			}
			
			return false;
			
			
		});*/
		
		
		
    });
	
	$.fn.validateFields = function() {
		var fields = $('form.wpsc_checkout_forms input[type="text"], form.wpsc_checkout_forms textarea');
		
		if ( fields.length !== 0 ) {
			fields.each( function(index) {
				var id = $(this).attr('id'),
					title = $(this).attr('title'),
					value = $(this).attr('value'),
					validation;
				
				validation = new LiveValidation(id, {
									validMessage : " "
								});
				validation.add( Validate.Presence, {
					failureMessage : "Camp obligatoriu!"
				});
				validation.add( Validate.Exclusion, {
					within : [ 'numele', 'prenumele', 'adresa', 'codul postal', 'localitatea', 'judetul', 'telefonul' ],
					failureMessage : "Camp completat gresit!",
					caseSensitive : false
				});
				
				switch(title) {
					case 'billingemail' : {
						validation.add( Validate.Email, {
							failureMessage : "Adresa de e-mail invalida!"
						});
					} break;
					case 'billingphone' : {
						validation.add( Validate.Numericality, {
							notANumberMessage  : "Telefon invalid!"
						});
					} break;
					case 'billingpostcode' : {
						validation.add( Validate.Numericality, {
							notANumberMessage  : "Cod gresit!"
						});
						validation.add( Validate.Exclusion, {
							within : [ '0700', '07000', '070000', '123456' ],
							failureMessage : "Cod gresit!",
						});
					} break;
				}
			});
		}
	}

    $.fn.balanceHeights = function(wrap, balance, n) {
        $(wrap).each( function(index) {
            return $(this).find(balance).setMinHeight(n);
        });
    }

    $.fn.setMinHeight = function(setCount) {
      for(var i = 0; i < this.length; i+=setCount) {
        var curSet = this.slice(i, i+setCount),
            height = 0;
        curSet.each( function() {
            height = Math.max(height, $(this).height());
        });
        curSet.css('height', height);
      }
      return this;
    };

})(jQuery);
