$(function(){
	init_page();
})

function init_page()
{
	// for menu top
		$('td.menu_top_class').css('cursor','pointer').click(function(){
            var temp = $(this).contents();																	  
			linkto = $(temp[1]).attr('href');
			link_to(linkto);
		})
	
	// for scroll
		
	balance();	
	
	// for add cart
	$('a.add_product_detail').click(function(){
		var this_id = $(this).attr('id');											 	
		var pro_id = $(this).attr('rel');
		$.get( index()+'callback/man.add_cart/'+pro_id,function(data){
			if(data)
			{
				$('#show_menu_shopping').html(data);
				balance();
				$('.active_class'+pro_id).removeClass('cls_products').addClass('cls_products_active');
				
				if(this_id=='a_click_on_list_view') {
					$('.cls_right_viewed_item_for_remove').each(function(){																	 						var tr_rel = $(this).attr('rel');																																													  						var tr_obj = $(this);
						if(pro_id==tr_rel) {							
							$.post( index()+'callback/man.ajax', {'type':'remove_list_view_item', 'product_id':tr_rel},function(s) {
								$(tr_obj).remove();			
							});
						}
					});
				}
			} 
		})
	})
	
	// for delete_all_viewed
	$('.delete_all_viewed').click(function(){
		$.post( index()+'callback/man.ajax', {type:'delete_all_viewed'},function(){
			$('.cls_right_viewed_item_for_remove').each(function(){
				$(this).remove();
			});
			balance();			
		});
	})
	
	$('#email_friend_link').click(function() {
		$('#email_to_friend').dialog('open');
        $('.man_product_detail button').addClass('custom_button').removeClass('ui-corner-all').removeClass('ui-state-default').css('width', '64px').css('height', '29px').css('font-family', 'Arial');
	})
	
	// dialog	
	$("#email_to_friend").dialog({
		bgiframe: true,
		width: 600,
		height: 390,
		modal: true,
		autoOpen: false,
        dialogClass : 'man_product_detail',
		buttons: {
			'Send': function() {
				var error = false;
				var string_content = '';

				$('.required').each(function(){
					value = $.trim($(this).val());
					if(value)
					{
						$(this).next().html('');
						string_content+= $(this).attr('name')+'='+value+'&';
					}else{
						$(this).next().html(' <font color="red">required</font>');
						error = true;
					}						
				});
				
				if(error)	return false;
				
				$('.email').each(function(){
					value = $.trim($(this).val());
					if(check_email(value))
					{
						$(this).next().html('');							
					}else{
						$(this).next().html(' <font color="red">*invalid</font>');
						error = true;
					}						
				});
				
				if(error)	return false;
				
				element = $(this);
				
				$.post( index()+'callback/man.send_email', {data:string_content},function(html){					
					alert(html);
					element.dialog('close');
				});
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('.required').removeClass('ui-state-error');
		}
	});

}

function balance()
{
	// balance 2 site
	
	/*height = 950;
	
	//height1 = $('td.main_left').height();	
	height1 = $('td.main_left li.parent').length;
	
	//more_ie = ($.browser.msie)?20:25;
	more_ie = ($.browser.msie)?25:30;
	height = height1*more_ie+253; 
	
	if(height<630)		height=630;
	
	more = ($.browser.msie)?40:14;
	more2 = ($.browser.msie)?46:20;
			
	$('td.main_right').height(height+more);
	value = $('td.main_right').height();
	
	height1 = height+more2;
	
	if(value>height1)
	{
		height1 = value+6;
	}
			
	$('td.main_left').height(height1);	
	$('div#page_content_main').height(height1+10).width(572).css('overflow','auto')//.jScrollPane({showArrows:true});*/
}

function check_email( email )
{	
	if(!/^([a-zA-Z0-9_\.])+\@([a-zA-Z0-9\-])+\.([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?$/.test(email))
	{
		return false;
	}
	return true;
}

function link_to(link)
{
	window.location = index()+link; 
}


