/*
 * SimpleModal OSX Style Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: osx.js 238 2010-03-11 05:56:57Z emartin24 $
 */

jQuery(function ($) {
	var OSX = {
		container: null,
		init: function () {
			$("input.osx, a.osx").click(function (e) {
				e.preventDefault();	

				$("#osx-modal-content").modal({
					overlayId: 'osx-overlay',
					containerId: 'osx-container',
					closeHTML: null,
					minHeight: 80,
					opacity: 65, 
					position: ['0',],
					overlayClose: false,
					onOpen: OSX.open,
					onClose: OSX.close
				});
			});
		},
		open: function (d) {
			var self = this;
			save_pressed = false;
			self.container = d.container[0];
			d.overlay.fadeIn('slow', function () {
				$("#osx-modal-content", self.container).show();
				
				if (cur_id!=-1)	AddToCart(cur_id, cur_idx, cur_Name,cur_src,cur_price);
				//alert(cur_id+' '+ cur_idx+'1111');
				
				// удаляем все строки из таблицы - строим повторно из кукисов
				var message='';
				var idx='';
				var Name='';
				var src='';
				var price='';
				$("table#zayavka").find("tr").each(
					function() {
						$("table#zayavka").find("#"+this.id).remove();
				});
				
				$("table#zayavka > tbody").append("<tr style='text-align:center; font-size:11px' id='first_row'><td >&nbsp;</td><td ><strong>Наименование</strong></td><td><strong>Код модели</strong></td><td><strong>Комментарий\\комплектация</strong></td><td style='width:80px' ><strong>Цена</strong></td><td>&nbsp;</td>");
				for (var i=0;i<6;i++) {
					message = getCookie('item_'+i);
					if ((message==null) || (message==''))  {
						continue;			
					}
					else {
						idx = getCookie('item_'+i+'_idx');
						Name = getCookie('item_'+i+'_Name');
						src = getCookie('item_'+i+'_src');
						//alert('item_'+i+' src '+src);
						price = getCookie('item_'+i+'_price');
						Comm = getCookie('item_'+i+'_Comm');
						if (Comm==null) Comm='';
						//alert(' Comm '+Comm);	
						//Comm = '';
						$("table#zayavka > tbody").append("<tr style='text-align:center' id='row_"+message+"'><td><img src='"+src+"' border='0' height='100' width='100' /></td><td><input name='idx["+message+"]' readonly='true' style='background-color: #EEEEEE;width:70px; border:0px; ' value='"+idx+"' ></td><td><input name='Name["+message+"]' readonly='true' style='background-color: #EEEEEE; width:140px;border:0px;  ' value='"+Name+"' ></td><td><textarea cols='22' rows='5' name='comment["+message+"]' id='comm_"+message+"'>"+Comm+"</textarea></td><td nowrap='nowrap'>"+price+"</td><td><a style='color:#FF0000' onclick=\"DelFromCart("+message+","+i+"); return false;\" href=\"#\">удалить</a></td></tr>");
					}
						
				}
				//alert('end');
				chel_email = getCookie('chel_email');
				chel_name = getCookie('chel_name');
				chel_phone = getCookie('chel_phone');
				
				if (chel_email != null) document.getElementById('email_chel').value = chel_email;
				if (chel_name != null) document.getElementById('name_chel').value = chel_name;
				if (chel_phone != null) document.getElementById('phone_chel').value = chel_phone;
										
				var title = $("#osx-modal-title", self.container);
				title.show();
				d.container.slideDown('slow', function () {
					setTimeout(function () {
						var h = $("#osx-modal-data", self.container).height()
							+ title.height()
							+ 20; // padding
						//alert(' h '+h+' d.container.height() '+d.container.height());	
						if (h >	h2) {
							h=h2;
							is_h2 = true;
						}
						else is_h2 = false;
						d.container.animate(
							{height: h}, 
							200,
							function () {
								$("div.close", self.container).show();
								$("#osx-modal-data", self.container).show();
							}
						);
						if (is_h2 == true) $('div.simplemodal-wrap').css({overflow: 'auto'});						
						
					}, 300);
				});
				
				
			})
		},
		close: function (d) {
			var self = this; // this = SimpleModal object
			chel_email = document.getElementById('email_chel').value;
			chel_phone = document.getElementById('phone_chel').value;
			chel_name = document.getElementById('name_chel').value;
			setCookie ('chel_email', chel_email,myDate);	
			setCookie ('chel_phone', chel_phone, myDate);
			setCookie ('chel_name', chel_name, myDate);
			//alert(chel_email);
			
			d.container.animate(
				{top:"-" + (d.container.height() + 20)},
				500,
				function () {
					self.close(); // or $.modal.close();
					
					
				}				
			);
		}
	};

	OSX.init();

});
