jQuery.fn.initDialog = function(options) {
    if (typeof options == 'function')
        options = { success: options };

    options = jQuery.extend({
        autoOpen:  		this.attr('autoOpen') 		|| false,
        draggable: 		this.attr('draggable') 		|| true,
        hide: 			this.attr('hide') 			|| 'fold',
        show: 			this.attr('show') 			|| 'fold',
        modal: 			this.attr('modal') 			|| true,
        resizeable: 	this.attr('resizeable') 	|| true,
        noTitle: 		this.attr('noTitle') 		|| true,
        width: 			this.attr('width') 			|| 200,
        parentID: 		this.attr('parentID') 		|| '',
        closeOnEscape: 	this.attr('closeOnEscape') 	|| true
    }, options || {});
    
    this.dialog(options);
    return this;
};

jQuery.fn.openDialog = function() {
    this.dialog('open');
    return this;
};

jQuery.fn.closeDialog = function() {
    this.dialog('close');
    return this;
};

function loadColorPicker(id) {
	var holder  = $('#' + id + '_holder');
	var select  = $('#' + id + '_selector');
	var selectd = $('#' + id + '_selector div');
	var input   = $('#' + id);
	var widt 	= false;
	var color	= input.attr('value');
	holder.ColorPicker({
		flat: true,
		color: color,
		onSubmit: function(hsb, hex, rgb) {
			selectd.css('backgroundColor', '#' + hex);
			input.attr('value', '#' + hex);
		}		
	});
	$('#' + id + '_selector').bind('click', function() {
		holder.stop().animate({height: widt ? 0 : 173}, 500);
		widt = !widt;

	});
	holder.css('position', 'absolute');
	selectd.css('backgroundColor', color);
}

function changeShipping(chk) {
	if (chk.checked) {
		$('#shipping_info').hide();
	} else {
		$('#shipping_info').show();
	}
}

function show_hide(id) {
	var o = $('#'+id);
	if (o.is(':visible')) {
		o.hide();
	} else {
		o.show();
	}
}

function confirmAction(msg, url) {
	if (msg) {
		msg = 'Are you sure you want to ' + msg + '?';
	} else {
		msg = 'Are you sure?';
	}
	if (confirm(msg)) {
		document.location=url;
	}
}

function main_img(obj, idx, item) {
	if (ser == 1) {
		th = jQuery.parseJSON(base64_decode(th));
		ser = 0;
	}
	$('#img_main').html('<img src="' + th[idx].big + '" onclick="$(\'#dialog_' + item + '_' + idx + '\').openDialog()" class="thumbnail dialog" />');
	$('#img_main_click').html('<a href="#" onclick="$(\'#dialog_' + item + '_' + idx + '\').openDialog()" class="zoom">view larger image</a>');
	$(obj).parent().html('<img src="' + th[sel].small + '" onclick="main_img(this, \'' + sel + '\', \'' + item + '\')" class="thumbnail dialog" />');
	sel = idx;
}

function main_cropimg(obj, idx, id, pid) {
	if (ser == 1) {
		th = jQuery.parseJSON(base64_decode(th));
		ser = 0;
	}
	
	$(obj).parent().html('<img src="' + th[sel].small + '" onclick="main_cropimg(this, \'' + sel + '\', \'' + th[sel].id + '\', \'' + pid + '\')" class="thumbnail dialog" />');
	//crop_zoom = null;
	delete crop_zoom;
	//crop_zoom.restore();	
	initCropZoom('img_main', th[idx].regular, th[idx].width, th[idx].height, id, pid);
	//crop_zoom.restore();
	
	sel = idx;
	$('#crop_id').val(id);
}

function addEditor(obj) {
	var oFCKeditor = new FCKeditor(obj);
	oFCKeditor.BasePath = "/js/fckeditor/";
	oFCKeditor.Config["CustomConfigurationsPath"] = "/js/fckeditor/customconfig.js"  ;
	oFCKeditor.ToolbarSet = 'Custom';
	oFCKeditor.ReplaceTextarea();
	
}

var global_i = 1;
function addNew(i) {
	var o = $('#form-test');
	if (global_i == 1) {
		global_i = i;
	}
	var t = '<dt><label class="required">Name:</label></dt><dd><input type="text" name="foo[name_' + global_i + '][]" value="" size="40" maxlength="100"></dd>';
	$(t).appendTo('#form-test');
	global_i++;
}

var ship = '';
var subt  = '';
var wrap = '';
function changeCountry(id, uk, non_uk, sub) {
	if ($('#same_shipping').attr('checked')) {
		var o = $('#country').val();
	} else {
		var o = $('#country_s').val();
	}
	if (wrap == '') {
		wrap = $('#gift_span').html();
	}
	subt  = sub;
	if (o == id) {
		ship = (uk*100/100).toFixed(2);		
		$('#shipping_span').html(ship);
		$('#total_span').html(((parseFloat(uk)+parseFloat(sub)+parseFloat(wrap))*100/100).toFixed(2));
	} else {
		ship = (non_uk*100/100).toFixed(2);
		$('#shipping_span').html(ship);
		$('#total_span').html(((parseFloat(non_uk)+parseFloat(sub)+parseFloat(wrap))*100/100).toFixed(2));
	}
}

function changeGift(obj) {
	if ($(obj).attr('checked')) {
		wrap = $('#defwrap').val();
	} else {
		wrap = '0.00';
	}
	$('#gift_span').html(wrap);
	if (ship == '') {
		ship = $('#shipping_span').html();
	}
	if (subt == '') {
		subt = $('#subtotal').val();
	}
	$('#total_span').html(((parseFloat(ship)+parseFloat(subt)+parseFloat(wrap))*100/100).toFixed(2));
}

var crop_zoom;
function initCropZoom(obj, src, width, height, id, pid, minW, minH, maxW, maxH) {
	if (!minW) minW = 100;
	if (!minH) minH = 70;
	if (!maxW) maxW = 340;
	if (!maxH) maxH = 340;
	
	crop_zoom = $('#' + obj).cropzoom({
            width:540,
            height:400,
            bgColor: '#CCC',
            enableRotation:false,
            enableZoom:true,
            selector:{        
              x:minW,
              y:minH,
              w:minW,
              h:minH,
              centered:true,
              borderColor:'#ec5e71',
              borderColorHover:'#ea455d',
              bgInfoLayer: '#FFF',
              infoFontSize: 10,
              infoFontColor: 'blue',
              showPositionsOnDrag: false,
              showDimetionsOnDrag: false,
              maxHeight: maxH,
              maxWidth: maxW,
              minHeight: minH,
              minWidth: minW
            },
            image:{
                source:src,
                width:width,
                height:height,
                minZoom:0,
                maxZoom:100,
                rotation:0,
                scaleX:1,
                scaleY:1
            },
            onSelectorDrag:clearResponse,
            onSelectorDragStop: clearResponse,
            onSelectorResize: clearResponse,
            onSelectorResizeStop: clearResponse,
            onZoom: clearResponse,
            onRotate:clearResponse,
            onImageDrag:clearResponse
	});
}

function initCropimgZoom2(obj, src, width, height, id, pid) {
	var minW = 100;
	var minH = 70;
	var maxW = width;
	var maxH = height;
	
	crop_zoom = $('#' + obj).cropzoom({
            width:710,
            height:600,
            bgColor: '#CCC',
            enableRotation:false,
            enableZoom:true,
            selector:{        
              x:minW,
              y:minH,
              w:minW,
              h:minH,
              centered:true,
              borderColor:'#ec5e71',
              borderColorHover:'#ea455d',
              bgInfoLayer: '#FFF',
              infoFontSize: 10,
              infoFontColor: 'blue',
              showPositionsOnDrag: false,
              showDimetionsOnDrag: false,
              maxHeight: maxH,
              maxWidth: maxW,
              minHeight: minH,
              minWidth: minW
            },
            image:{
                source:src,
                width:width,
                height:height,
                minZoom:0,
                maxZoom:100,
                rotation:0,
                scaleX:1,
                scaleY:1
            },
            onSelectorDrag:clearResponse,
            onSelectorDragStop: clearResponse,
            onSelectorResize: clearResponse,
            onSelectorResizeStop: clearResponse,
            onZoom: clearResponse,
            onRotate:clearResponse,
            onImageDrag:clearResponse
	});
}

function initCropimgZoom(obj, src, width, height, id, pid) {
	var minW = 100;
	var minH = 70;
	var maxW = width;
	var maxH = height;
	
	crop_zoom = $('#' + obj).cropzoom({
            width:540,
            height:400,
            bgColor: '#CCC',
            enableRotation:false,
            enableZoom:true,
            selector:{        
              x:minW,
              y:minH,
              w:minW,
              h:minH,
              centered:true,
              borderColor:'#ec5e71',
              borderColorHover:'#ea455d',
              bgInfoLayer: '#FFF',
              infoFontSize: 10,
              infoFontColor: 'blue',
              showPositionsOnDrag: false,
              showDimetionsOnDrag: false,
              maxHeight: maxH,
              maxWidth: maxW,
              minHeight: minH,
              minWidth: minW
            },
            image:{
                source:src,
                width:width,
                height:height,
                minZoom:0,
                maxZoom:100,
                rotation:0,
                scaleX:1,
                scaleY:1
            },
            onSelectorDrag:clearResponse,
            onSelectorDragStop: clearResponse,
            onSelectorResize: clearResponse,
            onSelectorResizeStop: clearResponse,
            onZoom: clearResponse,
            onRotate:clearResponse,
            onImageDrag:clearResponse
	});
}

function sendCrop(pid)
{
	var id = $('#crop_id').val();
	crop_zoom.send('/product/docrop/id/'+id+'/pid/'+pid, 'POST', {}, doneCrop);
}

function sendCropimg(pid)
{
	var id = $('#crop_id').val();
	crop_zoom.send('/product/docropimg/id/'+id+'/pid/'+pid, 'POST', {}, doneCrop);
}

function sendCropimgHome(pid)
{
	var id = $('#crop_id').val();
	crop_zoom.send('/homepage/docropimg/id/'+id+'/pid/'+pid, 'POST', {}, doneCrop);
}

function sendCropCateg(id)
{
	crop_zoom.send('/category/docrop/id/'+id, 'POST', {}, doneCrop);
}

function sendCropSubCateg(id)
{
	crop_zoom.send('/subcategory/docrop/id/'+id, 'POST', {}, doneCrop);
}

function doneCrop(r) {
	$('div_img_response').html('');
	r = jQuery.parseJSON(r);
	if (r.response==0) {
		$('#crop_response').html(r.error);
	} else {
		$('#crop_response').html(r.error);
		$('#div_img_response').html('<img src="' + r.file + '?' + Math.random() + '" />');
		$("#dialog_response").initDialog({width:300});
	}
}

function clearResponse() {
	$('#crop_response').html('');
}

function log(obj) {
	var ss = '';
	try {
		for (var att in obj) {
			var t = typeof(obj[att]);
			if (t != 'function') {
				if (t == 'object') {
					ss += log(obj[att]);
				} else
				if (obj[att]) {
					ss += att + ' => ' + obj[att] + "; ";
				}
			}
		}
	}catch (e) {}
	return ss;
}

function PrintThisPage(id) 
{
   var sOption="toolbar=no,location=no,directories=no,menubar=yes,scrollbars=yes,width=600,height=600,left=100,top=25"; 
   
   var winprint=window.open("","",sOption); 
       winprint.document.open(); 
       winprint.document.write('<html><link href="/skins/blues/css/style.css" rel="stylesheet" type="text/css" /><style type="text/css">.dialog-table2{font-size:18px;}.medium{font: bold 20px/24px \'Arial Black\';}</style><body style="background:#ffffff;font:normal 14px/16px Arial, Helvetica, sans-serif;"><div style="width:100%; text-align:left; vertical-align:top; padding-left:30px; padding-top:30px;font:normal 12px/16px Arial, Helvetica, sans-serif;"><div class="big red" style="padding-left:5px;padding-bottom:10px;">Spring baby</div>'); 
       winprint.document.write($('#' + id).html());          
       winprint.document.write('</div></body></html>'); 
       winprint.document.close(); 
       winprint.focus(); 
}

function categoryChange()
{
	var obj = $('#category');
	$.ajax({
		url: '/subcategory/ajaxget/category/' + obj.val(),
		success: function(data) {
			var r = '';
			r = jQuery.parseJSON(data);
			if (r != '-') {
				$('#subcategory').html(r);
			}
		}
	});
}

function changeCCType(obj) {
	var v = $(obj).val();
	if (v == 'Solo') {
		$('#form-payment-ss').show();
	} else {
		$('#form-payment-ss').hide();
	}
}
