/***********************************************
* jQuery functions execute
***********************************************/

/*preloading images*/
jQuery.preloadImages = function()
	{
	  for(var i = 0; i<arguments.length; i++)
	  {
		 jQuery("<img>").attr("src", arguments[i]);
	  }
	}
	$.preloadImages("images/core/bg.jpg", "images/core/bg-base1.jpg", "images/core/bg-base-repeat.jpg", "images/core/bg-header.jpg",
						 "images/core/bg-footer.gif", "images/core/bg-title-category.gif", "images/core/bg-title-mailing.gif", "images/core/bg-title-shopping.gif",
						 "images/core/logo.jpg");

function isInteger(s) { 
	var i;

	for (i = 0; i < s.length; i++)
	{
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

$(document).ready(function() {                
               /*cmx form */
					if(jQuery.browser.mozilla) {
                                $('form.cmxform').hide().find('label:not(.nocmx)').each(function(){
                                                var $labelContent = $(this).html();
                                                var $labelWidth = $(this).css('width');
                                                $(this).empty();
                                                $(this).append('<span style="display: block; width: '+$labelWidth+';">');
                                                $(this).prepend('</span>');
                                                $(this).css('display', '-moz-inline-box');
                                                $(this).find('span').html($labelContent);
                                                $('form.cmxform').show();
                                });
                };             


					/*external link*/
					$('a[@rel$="external"]').click(function(){this.target = "_blank";});


					/*on focus for input boxes*/
					$.fn.search = function() {
							return this.focus(function() {
								if( this.value == this.defaultValue ) {
									this.value = "";
								}
							}).blur(function() {
								if( !this.value.length ) {
									this.value = this.defaultValue;
								}
							});
						};

					$.fn.searchNumber = function() {
							return this.focus(function() {
								if( this.value == this.defaultValue ) {
									this.value = "";
								}
							}).blur(function() {
								if( !this.value.length ) {
									this.value = this.defaultValue;
								} else
									if (!isInteger(this.value)) {
										this.value = 0
									}
							});
						};

					  $("input.mail-list").search();
					  $("input.searchbox").search();
					  $("input.user").search();
					  $("input.recovery").search();
					  $("input.amount").search();
					  $("input.box").search();
					  $("input.starter-text").search();
					  $("input.starter-number").searchNumber();
						
						
						/*accordion navigation - accordion.js + easing.js. + dimension.js*/
						$('ul#catalogue-nav').Accordion({
						active: '.current',
						selectedClass: 'active',
						header: '.firstlevel',
						navigation: true,
						event: 'click',
						autoheight: false,
						alwaysOpen: false
					});
					$('ul#catalogue-nav ul').Accordion({
						active:  '.current',
						selectedClass: 'active',
						header: '.secondlevel',
						navigation: true,
						event: 'click',
						autoheight: false,
						alwaysOpen: true
					});

					
					/*set hover class for anything*/
					  $('a.send').hover(function() {
						 $(this).addClass('hover');
					  }, function() {
						 $(this).removeClass('hover');
					  });
					  
					  $('form.form input.send').hover(function() {
						 $(this).addClass('formhover');
					  }, function() {
						 $(this).removeClass('formhover');
					  });
					  
					  $('input.send').hover(function() {
						 $(this).addClass('sendhover');
					  }, function() {
						 $(this).removeClass('sendhover');
					  });
					  
					   $('input.go').hover(function() {
						 $(this).addClass('gohover');
					  }, function() {
						 $(this).removeClass('gohover');
					  });
												
						$('input.voucher').hover(function() {
						 $(this).addClass('voucherhover');
					  }, function() {
						 $(this).removeClass('voucherhover');
					  });
						
						$('input.jqmbutton').hover(function() {
						 $(this).addClass('alerthover');
					  }, function() {
						 $(this).removeClass('alerthover');
					  });
						
						$('#mini-cart').hover(function() {
						 $(this).addClass('carthover');
					  }, function() {
						 $(this).removeClass('carthover');
					  });
						
						$("#mini-cart").css('cursor', 'pointer').click( function() { window.location = $("#mini-cart").children("a").attr("href");});

					
});


/* Overriding Javascript's Confirm Dialog using simpleModal.js */

$(document).ready(function () {
	$('.confirm').click(function (e) {
		e.preventDefault();
			$('#wishlist-alert').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    			onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
				});
			});
			},
				onShow: function (dialog) {
					//dialog.data.find('.message').append(message);
			},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
				});
			 });
			}
		});
	});

	$('.confirm1').click(function (e) {
		e.preventDefault();
			$('#voucher-alert').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    			onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
				});
			});
			},
				onShow: function (dialog) {
					//dialog.data.find('.message').append(message);
			},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
				});
			 });
			}
		});
	});

	$('.confirm2').click(function (e) {
		e.preventDefault();
			$('#checkout-alert').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    			onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
				});
			});
			},
				onShow: function (dialog) {
					//dialog.data.find('.message').append(message);
			},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
				});
			 });
			}
		});
	});
	
	$('.confirm3').click(function (e) {
		e.preventDefault();
			$('#account-alert').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    			onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
				});
			});
			},
				onShow: function (dialog) {
					//dialog.data.find('.message').append(message);
			},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
				});
			 });
			}
		});
	});
	
	$('.confirm4').click(function (e) {
		e.preventDefault();
			$('#mailing-alert').modal({
				close: false,
				overlayId: 'confirmModalOverlay',
				containerId: 'confirmModalContainer',
    			onOpen: function (dialog) {
					dialog.overlay.fadeIn(300, function () {
					dialog.container.fadeIn(300, function () {
					dialog.data.fadeIn(200);
				});
			});
			},
				onShow: function (dialog) {
					//dialog.data.find('.message').append(message);
			},
				onClose: function (dialog) {
					dialog.data.fadeOut(200, function () {
					dialog.container.fadeOut(300, function () {
					dialog.overlay.fadeIn(300, function () {
					$.modal.close();
					});
				});
			 });
			}
		});
	});
	
});


/*floating popup - jqModal.js*/
$().ready(function() {
					$('#privacy')
					 .jqDrag('.jqDrag')
					 .jqResize('.jqResize')
					 .jqm({
						trigger:'#privacyTrigger',
						overlay: 0,
						onShow: function(h) {
						  /* callback executed when a trigger click. Show notice */
						  h.w.css('opacity',0.95).slideDown(); 
						  },
						onHide: function(h) {
						  /* callback executed on window hide. Hide notice, overlay. */
						  h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); } 
						});
					 
					 $('#privacy2')
					 .jqDrag('.jqDrag')
					 .jqResize('.jqResize')
					 .jqm({
						trigger:'#privacy2Trigger',
						overlay: 0,
						onShow: function(h) {
						  /* callback executed when a trigger click. Show notice */
						  h.w.css('opacity',1).slideDown(); 
						  },
						onHide: function(h) {
						  /* callback executed on window hide. Hide notice, overlay. */
						  h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); } 
						});
					 
					 $('#terms')
					 .jqDrag('.jqDrag')
					 .jqResize('.jqResize')
					 .jqm({
						trigger:'#termsTrigger',
						overlay: 0,
						onShow: function(h) {
						  /* callback executed when a trigger click. Show notice */
						  h.w.css('opacity',0.95).slideDown(); 
						  },
						onHide: function(h) {
						  /* callback executed on window hide. Hide notice, overlay. */
						  h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); } 
						});
					 
					 $('#terms2')
					 .jqDrag('.jqDrag')
					 .jqResize('.jqResize')
					 .jqm({
						trigger:'#terms2Trigger',
						overlay: 0,
						onShow: function(h) {
						  /* callback executed when a trigger click. Show notice */
						  h.w.css('opacity',0.95).slideDown(); 
						  },
						onHide: function(h) {
						  /* callback executed on window hide. Hide notice, overlay. */
						  h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); } 
						});
					 
					 $('#shipping-rates')
					 .jqDrag('.jqDrag')
					 .jqResize('.jqResize')
					 .jqm({
						trigger:'#ratesTrigger',
						overlay: 0,
						onShow: function(h) {
						  /* callback executed when a trigger click. Show notice */
						  h.w.css('opacity',0.95).slideDown(); 
						  },
						onHide: function(h) {
						  /* callback executed on window hide. Hide notice, overlay. */
						  h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); } 
						});
					 
					 $('#help')
					 .jqDrag('.jqDrag')
					 .jqResize('.jqResize')
					 .jqm({
						trigger:'#helpTrigger',
						overlay: 0,
						onShow: function(h) {
						  /* callback executed when a trigger click. Show notice */
						  h.w.css('opacity',0.95).slideDown(); 
						  },
						onHide: function(h) {
						  /* callback executed on window hide. Hide notice, overlay. */
						  h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); } 
						});
					 
					 $('#customer-service')
					 .jqDrag('.jqDrag')
					 .jqResize('.jqResize')
					 .jqm({
						trigger:'#customerTrigger',
						overlay: 0,
						onShow: function(h) {
						  /* callback executed when a trigger click. Show notice */
						  h.w.css('opacity',0.95).slideDown(); 
						  },
						onHide: function(h) {
						  /* callback executed on window hide. Hide notice, overlay. */
						  h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); } 
						});

});


/*news ticker*/
var newsitems;
var curritem=0;

$(document).ready(function(){
    newsitems = $("#ticker li").hide().size();
    $("#ticker li:eq("+curritem+")").show();
    setInterval(ticknews,4000); //time in milliseconds

});

function ticknews() {
    $("#ticker li:eq("+curritem+")").hide();
    curritem = ++curritem%newsitems;
    $("#ticker li:eq("+curritem+")").show();

}

/*simple show + hide*/

      $(document).ready(function() {
			  $('div.rates').hide();
		
        $('a.open-rates').click(function() {
          $('div.rates').toggle(400);
          return false;
        });
      });
