var selItems = {shape:'', color:'', texture:''}; // We'll use this for create BFF process
$(document).ready(function(){
	$("#slideCtrl").jFlow({ slides:"#slideCntr",controller:".slideFlwCtrl",slideWrapper:"#slideWrpr",selectedWrapper: "slideFlwSlct",width: "896px",height: "340px",duration: 800,easing: "swing",prev:".slidePrev",next:".slideNext"});
	$("#thinkA,#linkA,#syncA,#blinkA,#prevA,#nextA,#logTrg").click(function(){return false;});
	$("#shopa,#shopa_on,#soutsa,#soutsa_on,#gtalka,#gtalka_on,#wnewa,#wnewa_on,#thinkA,#linkA,#syncA,#blinkA,#prevA,#nextA").click(function(){this.blur();});
	$("#gemz").bubble({
		'trigger' : '.trigger',
		'popup': '.popup',
		'distance': 10,
		'hideDelay': 250,
		'effectTime': 250,
		'offsetX': -50,
		'offsetY': 150
	});
	$('#logTrg,#gemz').click(function(e) {$.modal($('#logBox'));});

	// Alert and Confirm Fields
	function alert_msg(message) {
		$('#alertBox').modal({
			containerId: ('modalAlertContainer'),
			onShow: function (dialog) {
				dialog.data.find('.message_alert').append(message);
			}
		});
	}
	function confirm_msg(message, callback){
		$('#confirmBox').modal({
			containerId: ('modalConfirmContainer'),
			onShow: function (dialog) {
				dialog.data.find('.message_confirm').append(message);

				// if the user clicks "yes"
				dialog.data.find('.modalConfirmYesImg').click(function () {
					// call the callback
					if ($.isFunction(callback)) {
						callback.apply();
					}
					// close the dialog
					$.modal.close();
				});
			}

		});

	}
	
	$('#cart').click(function(e){window.location = $(this).attr('href');return false;});

	$('#casingsCatCnt, #chainsCatCnt, #braceletsCatCnt, #decorativeCatCnt, #neckCatCnt').hover(
		function() {
			$("#" + $(this).attr("id") + " img").fadeTo(400, 0.35);
			$("#" + $(this).attr("id") + " div").fadeIn(500);
		}, function() {
			$("#" + $(this).attr("id") + " img").fadeTo(400, 1);
			$("#" + $(this).attr("id") + " div").fadeOut(250);
		}
	);
	$('#casingsCatCnt, #chainsCatCnt, #braceletsCatCnt, #decorativeCatCnt, #neckCatCnt').click(function() {
			window.location = $("#" + $(this).attr("id") + " a").attr("href");
	});
	
	$('#sbclrsel').change(function() {
		$(this).removeClass("bgorange bgblue bggreen bgpink bgbrown");
		var tcl = 'bgorange'; // Default
		if($(this).val() != '') tcl = 'bg' + $(this).val();
		$(this).addClass(tcl);
	});
		
});
	
$(document).ready(function(){
        // --== Alert / Confirm Box

        function alert_msg(message) {
                $('#alertBox').modal({
                        containerId: ('modalAlertContainer'),
                        onShow: function (dialog) {
                                dialog.data.find('.message_alert').append(message);
                        }
                });
        }

        function confirm_msg(message, callback){
                $('#confirmBox').modal({
                        containerId: ('modalConfirmContainer'),
                        onShow: function (dialog) {
                                dialog.data.find('.message_confirm').append(message);

                                // if the user clicks "yes"
                                dialog.data.find('.modalConfirmYesImg').click(function () {
                                        // call the callback
                                        if ($.isFunction(callback)) {
                                                callback.apply();
                                        }
                                        // close the dialog
                                        $.modal.close();
                                });
                        }
                });
        }


	$('a.buybtn, a.wishbtn').hover(
		function() {
			var offs = $(this).offset();
			offs.top -= $('#moBalloon').height();
			//offs.left -= Math.round($(this).width() / 2);
			$('#moBalloon').css(offs);
			if($(this).attr('class') == 'buybtn') {
				$('#moBalloon span').html('Add it to your shoppping cart!');
			} else {
				$('#moBalloon span').html('Add it to your wishlist!');
			}
			$('#moBalloon').css(offs);
			$('#moBalloon').show();
		}, function() {
			$('#moBalloon').hide();
		});
	
	$('#modalOverlay').click(function(e){ $('#modalOverlay').hide(); });
	
	if($.browser.msie && $.browser.version.toString() < "7.0") {
		$.ifixpng('/images/px.gif');
		$('#loginpop, a.createbtn, #moBalloon img, div.colorpicker_color > div').ifixpng();
	}

	var cItems = {shapeT:0, colorT:0, textureT:0,previewT:0};

	function createBFF() {
		$('.createH').hover(
			function() {
				var pID = $(this).parent().attr('id');
				$( '#' + pID + ' ' + ($('#' + pID + ' .createC').css('display') == 'none' ? ' .closed' : ' .open')).show();
			},
			function() {
				var pID = $(this).parent().attr('id');
				$( '#' + pID + '  .open').hide();
				$( '#' + pID + '  .closed').hide();
			}
		);

		$('#colorT .createC, #textureT .createC, #previewT .createC').each(function() {$(this).hide();});
		
		$('#shapeT .createH,  #colorT .createH, #textureT .createH, #previewT .createH').click(function() {
			var pid = $(this).parent().attr('id')
			if( cItems[pid] )
				$('#' + pid + ' .createC').slideToggle(500);
				carousel();
		});
			
	}

	//-- (carousel) scrolling with arrows for register and create bff page
	jQuery('.createTbl').jcarousel();
	jQuery('.ravatar').jcarousel({
		vertical:true,
		scroll: 1,
		size:3 // How many rows to show on avatar resgister
	});

	//-- (carousel) scrolling with arrows for register and create bff page
	var carousel = function() {
		jQuery('.createTbl').jcarousel();
		return;
	}
	var changeItem = function() {
			var eid = $(this).attr('id');
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: rpath + 'ajax_handler.php',
				data: {id: 'createbff', value: $(this).attr('id') + '-' + selItems.shape + '-' + selItems.color + '-' + selItems.texture},
				success: function(data) {
					if(data.success == '1') {
						cItems[data.type + 'T'] = 1;
						
						/*
						$('#textureT  ul.cImgs').html(data.textureHtml);
						$('#colorT  ul.cImgs').html(data.colorHtml);
						$('#previewImg').html(data.previewHtml);
						*/
						$('#textureT  .createC').html(data.textureHtml);
						$('#colorT .createC').html(data.colorHtml);
						$('#previewImg').html(data.previewHtml);

						$('#colorT .creator').click(changeItem);
						$('#textureT .creator').click(changeItem);

						$('#' + data.type + 'T .creator').each(function() { $(this).removeClass('createMaskSel'); $(this).addClass('createMask') });
						$('#' + eid).removeClass('createMask');
						$('#' + eid).addClass('createMaskSel');
						$('#' + data.open + 'T .createC').slideDown(500);
						if(data.type == 'shape')// && selItems.shape == '')
							$('#shapeT .createC').slideUp(500);
							carousel();
						if(data.type == 'color')// && selItems.color == '')
							$('#colorT .createC').slideUp(500);
							carousel();
						if(data.type == 'texture')// && selItems.texture == '')
							$('#textureT .createC').slideUp(500);
							carousel();
					
						selItems[data.type] = data.item;
						$('#' + data.type + 'T .createF .open, #' + data.type +'T .createF .closed').html('&rarr;&nbsp;Update the ' + data.type + ' of your creation!');
						
					} else {
						alert_msg('There was an error while saving your creation!');
					}
				},
				error: function() {
					alert_msg('There was an error while saving your creation!');
				}
			});
	}
	$('#shapeT .creator, #colorT .creator, #textureT .creator').click(changeItem);

	createBFF();
	var cnt = 0;
	$('.shoutBox').each(function() {
		var t = $(this); 
		$(t).animate( { width:"0px", height:"0px", opacity:0.1} , 10, "easein", function() {$(t).show();}).animate( { width:"242px", height:"240px", opacity:1.0 } , 1000 + (cnt++)*500 );});
});

$(document).ready(function(){
	$("#content").charCounter(400, {container:"#charcnt", format: "%1 characters remaining"});
	$('#recaptcha').click(function() {
		$('#captchaimg').attr('src', rpath + 'captcha.php?t=' + (new Date()).getTime());
		return false;
	});
	
	var sbval = $("#sbform").validate({
		rules: {
			sender: "required",
			recipient: "required",
			email: {
				required: false,
				email: true
			},
			content: {
				required: true,
				minlength: 3
			},
			code: "required"
		},
		messages: {
			sender: "Enter your Name / Nickname",
			recipient: "Enter a friend's BFF Name / Nickname",
			email: "Please enter a valid email address",
			content: "Please enter a notable content {at least 3 characters}",
			code: "&larr; Enter the code&darr;"
		},
		errorPlacement: function(error, element) {
			element.parent().children("span.sberror").text( error.text() );
			element.parent().children("span.status").hide();
		},
		highlight: function(element) {
			$(element).parent().children("span.status").css({ display:"none" });
			$(element).parent().children("span.sberror").css({ display:"inline" });
	  	},
	  	unhighlight: function(element) {
			$(element).parent().children("span.status").css({ display:"inline" });
			$(element).parent().children("span.sberror").css({ display:"none" });
	  	}
	});
});

// Shopping Cart:
$(document).ready(function(){
        // --== Alert / Confirm Box

        function alert_msg(message) {
                $('#alertBox').modal({
                        containerId: ('modalAlertContainer'),
                        onShow: function (dialog) {
                                dialog.data.find('.message_alert').append(message);
                        }
                });
        }

        function confirm_msg(message, callback){
                $('#confirmBox').modal({
                        containerId: ('modalConfirmContainer'),
                        onShow: function (dialog) {
                                dialog.data.find('.message_confirm').append(message);

                                // if the user clicks "yes"
                                dialog.data.find('.modalConfirmYesImg').click(function () {
                                        // call the callback
                                        if ($.isFunction(callback)) {
                                                callback.apply();
                                        }
                                        // close the dialog
                                        $.modal.close();
                                });
                        }
                });
        }

	// Add to cart/wishlist
	$('.buybtn, .wishbtn').click(function() {
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: rpath + 'ajax_handler.php',
			data: {id: $(this).attr('id')},
			success: function(data) {
				if(data.success == '1') {
					$('#cart').text(data.update_cart);
				} 
				alert_msg(data.msg);
			},
			error: function() {
				alert_msg('There was an error while adding the selected item into the cart!');
			}
		});
		return false;
	});
	
	// Add custom creation(casing) to cart/wishlist
	$('#add_to_cart_custom, #add_to_wishlist_custom').click(function() {
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: rpath + 'ajax_handler.php',
			data: {id: $(this).attr('id') + '-' + 'shape=' + selItems.shape + '|color=' + selItems.color + '|pattern=' + selItems.texture},
			success: function(data) {
				if(data.success == '1') {
					$('#cart').text(data.update_cart);
				} 
				alert_msg(data.msg);
			},
			error: function() {
				alert_msg('There was an error while adding the selected item into the cart!');
			}
		});
		return false;
	});
	
	$('.remove_from_cart').click(function() {
		var dataid = $(this).attr('id');
		confirm_msg ("Are you sure to remove the item?", function(){
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: rpath + 'ajax_handler.php',
				data: {id: dataid},
				success: function(data) {
					if(data.success == '1') {
						$('#cart').text(data.update_cart);
						$('#row-' + data.row_id).remove();
						$('#total').text(data.total);
					} 
					alert_msg(data.msg);
				},
				error: function() {
					alert_msg('There was an error while removing the selected item from the cart!');
				}
			});
		});
		return false;
	});

	$('.update_cart_row').click(function() {
		var cart_row_id = $(this).attr('id').toString().replace(/update_cart_row-/, '');
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: rpath + 'ajax_handler.php',
			data: {id: $(this).attr('id'), value: $('#cart_row_quantity-' + cart_row_id).attr('value')},
			success: function(data) {
				if(data.success == '1') {
					$('#cart_row_total_price-' + cart_row_id).text(data.cart_row_total_price);
					$('#total').text(data.total);
					$('#cart_row_quantity-' + cart_row_id).attr('value', data.quantity);
				} 
				alert_msg(data.msg);
			},
			error: function() {
				alert_msg('There was an error while updating the cart item!');
			}
		});
		return false;
	});
	
	$('#checkout').click(function() {
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: rpath + 'ajax_handler.php',
			data: {action: 'checkout'},
			success: function(data) {
				if(data.success == '1') {
					$('#checkout_container').html(data.html);
					$('#' + data.frmid).submit();
				} else {
					alert_msg(data.msg);
				}
			},
			error: function() {
				alert_msg('There was an error in the checkout process! Please try again later!');
			}
		});
		return false;
		
	});
	
	// Register:
	$('.ravatari img').click(function() {
		$('#avatar_id').get(0).value = $(this).attr('id').replace(/avatar-/, '');
		$('.ravatari').each(function(i, s) {
			$(s).removeClass('sel');
		});
		$(this).parent().addClass('sel');
	});
	var regval = $("#registerfrm").validate({
		rules: {
			nickname: {
				required: true,
				minlength: 3
			},
			email: {
				required: false,
				email: true
			},
			parent_email: {
				required: true,
				email: true
			},
			password: {
				required:true,
				minlength: 3
			},
			confirm_password: {
				required:true,
				equalTo: "#password"
			},
			accept_terms: "required"
		},
		messages: {
			nickname: "enter your nickname {at least 3 characters}:",
			email: "enter a valid email address:",
			parent_email: "enter a valid email address:",
			password: "enter your password {at least 3 characters}:",
			confirm_password: "confirm your password:",
			accept_terms: "must accept terms of use to register: "
		},
		errorPlacement: function(error, element) {
			element.parent().parent().children("div.ril").children("span.regerror").text( error.text() );
			element.parent().parent().children("div ril").children("span.status").hide();
		},
		highlight: function(element) {
			$(element).parent().parent().children("div.ril").children("span.regerror").css({ display:"inline" });
			$(element).parent().parent().children("div ril").children("span.status").css( { display:"none"} );
	  	},
	  	unhighlight: function(element) {
			$(element).parent().parent().children("div.ril").children("span.regerror").css({ display:"none" });
			$(element).parent().parent().children("div ril").children("span.status").css( { display:"inline"} );
	  	}
	});
	$('#available_check').click(function() {
		if($.trim($('#nickname').get(0).value) == '') {
			alert_msg('Enter your desired nickname!');
		} else {
			$.ajax({
				type: 'POST',
				dataType: 'json',
				url: rpath + 'ajax_handler.php',
				data: {id: 'check_nickname', value: $('#nickname').get(0).value},
				success: function(data) {
					$('#available_status').html('');
					$('#available_status').show();
					if(data.success == 1) {
						$('#available_status').html(' - nickname available');
					} else {
						$('#available_status').html(' - nickname taken');
					}
					$('#available_status').fadeOut(5000);
				},
				error: function() {
					alert_msg('Server error!');
				}
			});
		}
		return false;
	});

        // Clear Input Fields
        $('#user_input').focus(function(){
		if ($(this).val() == "username"){
			$(this).val("");
		}
        });
        $('#user_input').blur(function(){
		if ( $(this).val() == ""){
			$(this).val("username");
		}
        });


        $('#user_pass').focus(function(){
		if ($(this).val() == "password"){
			$(this).val("");
		}
        });
        $('#user_pass').blur(function(){
		if ( $(this).val() == ""){
			$(this).val("password");
		}
        });
});
