/*
 * Exp:Store module for ExpressionEngine 2.x by Crescendo (support@crescendo.net.nz)
 * Copyright (c) 2010-2012 Crescendo Multimedia Ltd
 * All rights reserved.
 */

window.ExpressoStore = window.ExpressoStore || {};

ExpressoStore.dashboardGraph = function(plotLabel, plotData) {
	var options = {
		legend: { show: true, margin: 10, backgroundOpacity: 0.5 },
		points: { show: true, radius: 3 },
		lines: { show: true },
		grid: { borderWidth: 0, hoverable: true },
		xaxis: { },
		yaxis: { min: 0 }
	};

	var placeholder = $('#store_graph');
	$.plot(placeholder, [{ label: plotLabel, data: plotData }], options);

	var previousPoint = null;
	placeholder.bind('plothover', function (event, pos, item) {
		if (item) {
			if (previousPoint != item.datapoint[0]) {
				previousPoint = item.datapoint[0];
				$(".store_dashboard_tooltip").remove();
				$('<div class="store_dashboard_tooltip">$'+item.datapoint[1].toFixed(2)+'</div>')
					.css({ position: "absolute", display: "none", top: item.pageY+15, left: item.pageX+10 })
					.appendTo("body").fadeIn("fast");
			}
		}
		else {
			previousPoint = null;
			$(".store_dashboard_tooltip").fadeOut("fast", function() { $(this).remove(); });
		}
	});

	placeholder.bind('mouseout', function() {
		previousPoint = null;
		$(".store_dashboard_tooltip").fadeOut("fast", function() { $(this).remove(); });
	});
}

jQuery(function($) {
	var STORE_CP = EE.BASE+"&C=addons_modules&M=show_module_cp&module=store";

	/*
	 * Publish Page
	 */

	if ($('#store_product_field').size() > 0)
	{
		// toggle field panes
		$('#store_product_field label.store_hide_field').click(function() {
			img = $(this).children('img');
			if (img.attr('src').indexOf('field_collapse') > 0)
			{
				img.attr('src', img.attr('src').replace('field_collapse', 'field_expand'));
				$(this).next('.store_field_pane').slideDown();
			} else {
				img.attr('src', img.attr('src').replace('field_expand', 'field_collapse'));
				$(this).next('.store_field_pane').slideUp();
			}
		});

		// store new option template and handle add option events
		var opt_template = $('#store_product_modifier_template tr.store_product_option').clone();
		opt_template.attr('style', 'display: none');

		$('#store_product_modifiers_table').delegate('a.store_product_option_add', 'click', function() {
			var mod_key = $(this).attr('data-mod-key');
			var new_opt_key = parseInt($(this).attr('data-new-opt-key'));
			var opt_prefix = '[modifiers]['+mod_key+'][options]['+new_opt_key+']';

			var template = opt_template.clone();
			template.find('[name]').each(function() {
				$(this).attr('name', $(this).attr('name').replace('[modifiers][new][options][1]', opt_prefix));
			});
			$(this).closest('tbody').append(template);

			var opt_count = $(this).closest('tbody').find('tr.store_product_option').size();
			$(this).closest('td').find('span.store_product_options_count').text(opt_count);
			template.fadeIn('fast');

			$(this).attr('data-new-opt-key', new_opt_key+1);
			return false;
		});

		// store product modifier template and handle add modifier events
		var mod_template = $('#store_product_modifier_template').removeAttr('id');
		mod_template.remove();

		$('#store_product_modifiers_add').click(function() {
			var new_mod_key = parseInt($(this).attr('data-new-mod-key'));
			var mod_prefix = '[modifiers]['+new_mod_key+']';
			var template = mod_template.clone();

			template.find('[name]').each(function() {
				$(this).attr('name', $(this).attr('name').replace('[modifiers][new]', mod_prefix));
			});
			template.find('a.store_product_option_add').attr('data-mod-key', new_mod_key);
			$('#store_product_modifier_empty').hide();
			$('#store_product_modifiers_table').append(template);
			template.fadeIn('fast');

			$(this).attr('data-new-mod-key', new_mod_key+1);
			return false;
		});

		var reloadStockTable = function() {
			$('#store_product_stock_loading').show();
			// don't serialize ACT attribute, otherwise it overrides our URL action
			var act = $('#publishForm [name="ACT"]').attr('disabled', true);
			var data = $('#publishForm').serialize();
			act.attr('disabled', false);
			$('#store_product_stock input').attr('disabled', true);

			$.post(ExpressoStore.fieldStockUrl, data).done(function(content) {
				$('#store_product_stock').empty();
				$('#store_product_stock').html(content);
				$('#store_product_stock_loading').hide();
			});
		}

		$('#store_product_modifiers_table').delegate('.store_select_mod_type', 'change', function() {
			modifier_tbody = $(this).closest('tbody');
			if ($(this).val() == 'var' || $(this).val() == 'var_single_sku')
			{
				modifier_tbody.children('tr.store_product_option').show();
				modifier_tbody.find('.store_product_options_info').show();
			}
			else
			{
				modifier_tbody.children('tr.store_product_option').hide();
				modifier_tbody.find('.store_product_options_info').hide();
			}

			reloadStockTable();
		});

		$('#store_product_modifiers_table').delegate('.store_input_mod_name, .store_input_opt_name', 'change', function() {
			reloadStockTable();
		});

		$('#store_product_modifiers_table').delegate('a.store_product_modifier_remove', 'click', function() {
			$(this).closest('tbody').fadeOut('slow', function() {
				$(this).remove();
				reloadStockTable();

				if ($('#store_product_modifiers_table tbody.store_product_modifier').size() == 0)
				{
					$('#store_product_modifier_empty').show();
				}
			});
			return false;
		});

		$('#store_product_modifiers_table').delegate('a.store_product_option_remove', 'click', function() {
			$(this).closest('tr').fadeOut('slow', function() {
				var opt_count = $(this).closest('tbody').find('tr.store_product_option').size() - 1;
				$(this).closest('tbody').find('span.store_product_options_count').text(opt_count);
				$(this).remove();
				reloadStockTable();
			});
			return false;
		});

		//Toggle stock level enable/disable based on stock tracking option
		$('#store_product_stock .store_track_stock input:checkbox').live('change', function() {
		    $(this).closest('td').find('.store_stock_level input')
		        .attr('disabled', !this.checked).toggleClass('disabled', !this.checked);
		});

		$('#store_product_stock .checkall_stock_publish').live('change', function() {
		    $(this).closest('table').find('.store_track_stock input:checkbox')
			    .attr('checked', this.checked).trigger('change');
		});
	}

	/*
	 * Orders Page / Tables
	 */

	$('#filterform select, #filterform input:checkbox').change(function() {
		$('.store_datatable').dataTable().fnDraw();
	});


	$('#filterform input:text').bind('keyup blur paste', function() {
		$('.store_datatable').dataTable().fnDraw();
	});

	$('#checkall').change(function() {
		$('.mainTable input:checkbox[name="selected[]"]').attr('checked', this.checked)
			.closest('tr').toggleClass('store_selected', this.checked);
	});

	$('.mainTable input:checkbox[name="selected[]"]').live('change', function() {
		$(this).closest('tr').toggleClass('store_selected', this.checked);
	});

	/*
	 * Datepicker used on various pages
	 */

	// check jQuery Datepicker plugin has been loaded
	if ($('#mainContent input.store_datetimepicker').size() > 0) {
		date_obj = new Date();
		date_obj_hours = date_obj.getHours();
		date_obj_mins = date_obj.getMinutes();

		if (date_obj_mins < 10) { date_obj_mins = "0" + date_obj_mins; }

		if (date_obj_hours > 11) {
			date_obj_hours = date_obj_hours - 12;
			date_obj_am_pm = " PM";
		} else {
			date_obj_am_pm = " AM";
		}

		date_obj_time = " '"+date_obj_hours+":"+date_obj_mins+date_obj_am_pm+"'";

		$('#mainContent input.store_datetimepicker').datepicker({dateFormat: $.datepicker.W3C + date_obj_time});
	}

	if ($('#mainContent input.store_datepicker').size() > 0) {
		$('#mainContent input.store_datepicker').datepicker({dateFormat: $.datepicker.W3C});
	}

	/*
	 * Country/Region multiple select boxes used on various pages
	 */

	if ($('#mainContent select.store_country_select').size() > 0)
	{
		$('#mainContent select.store_country_select').change(function() {
			var region_select_name = $(this).attr('name').replace('[country_code]', '[region_code]');
			var region_select = $('#mainContent select[name="'+region_select_name+'"]');

			if ($(this).data('oldVal') !== $(this).val()) {
				// remove all but first entry
				region_select.html(region_select.find('option[value="*"]'));

				// populate new regions
				var region_list = ExpressoStore.countries[$(this).val()];
				if (typeof(region_list) !== 'undefined') {
					for (var property in region_list.regions) {
						region_select.append('<option value="'+property+'">'+region_list.regions[property]+'</option>');
					}
				}

				// save current value
				$(this).data('oldVal', $(this).val());
			}
		});
	}

	/*
	 * Datatables functions
	 */

	if ($('#mainContent .store_datatable').size() > 0)
	{
		var fnToggleHiddenRow = function() {
				var tr = $(this).parent();
				var img = tr.children('td').children('a').children('img');

				if (tr.attr('class').indexOf('expand') >= 0)
				{
					/* This row is already open - close it */
					img.attr('src', EE.PATH_CP_GBL_IMG + 'expand.gif');
					tr.removeClass('expand');
					tr.addClass('collapse');
					tr.next('tr').find('.store_datatables_details_row').slideUp('fast', function() {
						oTable.fnClose(tr.get(0));
					});
				}
				else
				{
					/* Open this row */
					img.attr('src', EE.PATH_CP_GBL_IMG + 'collapse.gif');
					tr.removeClass('collapse');
					tr.addClass('expand');
					newTr = oTable.fnOpen(this.parentNode, fnFormatDetails(oTable, this.parentNode), 'details');
					newTrCols = $(newTr).children('td').attr('colspan');
					$(newTr).children('td').attr('colspan', newTrCols - 2);
					$(newTr).prepend('<td colspan="2"></td>');
					$(newTr).find('.store_datatables_details_row').slideDown('fast');
				}
				return false;
			}

		$('.store_datatable tbody tr td.clickable').live('click', fnToggleHiddenRow);

		var fnToggleAll = function() {
				var img = $(this).children('img');

				if (img.attr('src').indexOf('expand') >= 0)
				{
					img.attr('src', EE.PATH_CP_GBL_IMG + 'collapse.gif');
					$('.store_datatable tbody tr.collapse td.clickable:first-child').click();
				}
				else
				{
					img.attr('src', EE.PATH_CP_GBL_IMG + 'expand.gif');
					$('.store_datatable tbody tr.expand td.clickable:first-child').click();
				}
				return false;
		}

		$('.store_datatable thead tr th a[id=all]').live('click', fnToggleAll);

		/*
		 * Insert a 'details' column to the table
		 */
		var nCloneTd = document.createElement('td');
		$('.store_datatable tbody tr').each( function () {
			this.insertBefore(nCloneTd.cloneNode(true), this.childNodes[0] );
		} );

		/* Formatting function for row details */
		function fnFormatDetails (oTable, nTr )
		{
			var aData = oTable.fnGetData( nTr );
			var sOut = aData[aData.length-1];
			return sOut;
		}
	}

	/*
	 * Status changing
	 */
	$('.cancel_edit_status').click(function() {
		$(this).closest('td').find('.edit_status_info').slideUp('fast');
		$(this).hide();
		$(this).prev('.edit_status').show();
		return false;
	});
	$('.edit_status').click(function() {
		$(this).closest('td').find('.edit_status_info').slideDown('fast');
		$(this).next('.cancel_edit_status').show();
		$(this).hide();
		return false;
	});

	$('input[data-store-confirm]').click(function() {
	    return confirm($(this).attr('data-store-confirm'));
	});

	/*
	 * Reports page
	 */
	$('#store_report_list .store_date_range_select').change(function() {
		if ($(this).val() == 'custom_range') {
			$(this).siblings('.custom_date_range').show();
		} else {
			$(this).siblings('.custom_date_range').hide();
		}
	});

	/*
	 * Payment Methods settings
	 */
	$('#payment_method_class').change(function() {
	    var driver_class = $(this).val();
	    // hide and disable all driver settings
	    $('.payment_driver_settings').hide().find(':input').attr('disabled', true);
	    if (driver_class) {
    	    // show and enable new driver
    	    $('#'+driver_class+'_settings').show().find(':input').attr('disabled', false);
    	    // update name and title fields
            $('#payment_method_title').val(ExpressoStore.payment_drivers[driver_class]['title']);
            $('#payment_method_name').val(ExpressoStore.payment_drivers[driver_class]['name']);
        }
	}).change();

	/*
	 * Sortable Tables
	 */
	if ($('.store_sortable_table').size() > 0)
	{
		$('.store_sortable_table').sortable({ handle: '.store_handle' });

		function initialiseTable(){
					$(".store_sortable_table").sortable({
                		update: function(event, ui) { reorderDisplayNums(); }
        			});
				}

		function reorderDisplayNums (){
			var rows = $('.store_sortable_table').children();
			//Iterate through the table rows to reset each row's hidden new display value
			var i = 0;
			for (i = 0, ordering_data= ""; i < rows.length; i++){
		 		var d = $(rows[i]);
				d.attr("data-order", i);

				if (i != 0) { ordering_data += ", "; }
				ordering_data += d.attr("id")+" => "+d.attr("data-order");
	 		}

			var URL = STORE_CP+"&method=ajax_reorder&table="+$('.store_sortable_table').attr('id');
			$.ajax({
		 		url: URL,
		 		data: {order: ordering_data},
	 	 		dataType: "json"
	 		});
		}
		initialiseTable();
	}
});