// JavaScript Document
var interface = {
	flag : false,
	root : '/', //'http://ortega.com/',
	d : function(id){
		return document.getElementById(id);
	},
	automatic : true,
	showRecipesMenu : function(show){
		if(!show){
			document.getElementById('recipes_menu').style.visibility = 'hidden';
			document.getElementById('recipies_link2').className = 'lrecipes_menu_over';
			return;
		}else{
			document.getElementById('recipies_link2').className = 'lrecipes_menu_overHover';
			position = interface.getPosition('recipies_link');
			document.getElementById('recipes_menu').style.marginLeft = (position.x - 1) + 'px';
			
			
			if($.browser.msie && $.browser.version=="6.0"){
				document.getElementById('recipes_menu').style.marginTop = '-7px';
			} else {
				document.getElementById('recipes_menu').style.marginTop = '32px';	
			}
			
			
			document.getElementById('recipes_menu').style.visibility = 'visible';
			if($.browser.msie && /8.0/.test(navigator.userAgent)){
				$('#recipes_menu').css('margin-top','-6px');
			}
			if($.browser.msie && /7.0/.test(navigator.userAgent)){
				$('#recipes_menu').css('margin-top','-6px');
				$('#recipes_menu').css('margin-left','-901px');
			}
		}
	},
	showFamilyMenu : function(show){
		if(!show){
			document.getElementById('family_menu').style.visibility = 'hidden';
			document.getElementById('family_link2').className = 'lfamily_menu_over';
			return;
		}else{
			document.getElementById('family_link2').className = 'lfamily_menu_overHover';
			position = interface.getPosition('family_link');
			document.getElementById('family_menu').style.marginLeft = (position.x - 1) + 'px';
			//document.getElementById('family_menu').style.marginTop = '32px';
			
			if($.browser.msie && $.browser.version=="6.0"){
				document.getElementById('family_menu').style.marginTop = '-7px';
			} else {
				document.getElementById('family_menu').style.marginTop = '32px';	
			}
			
			document.getElementById('family_menu').style.visibility = 'visible';
			
			if($.browser.msie && /8.0/.test(navigator.userAgent)){
				$('#family_menu').css('margin-top','-6px');
			}
			
			if($.browser.msie && /7.0/.test(navigator.userAgent)){
				$('#family_menu').css('margin-top','-6px');
				$('#family_menu').css('margin-left','-224px');
			}
		}
	},
	showProductsMenu : function(show){
		if(!show){
			document.getElementById('products_menu').style.visibility = 'hidden';
			document.getElementById('products_link2').className = 'lproducts_menu_over';
			return;
		}else{
			document.getElementById('products_link2').className = 'lproducts_menu_overHover';
			position = interface.getPosition('products_link');
			document.getElementById('products_menu').style.marginLeft = (position.x - 1) + 'px';
			//document.getElementById('products_menu').style.marginTop = '32px';
			
			if($.browser.msie && $.browser.version=="6.0"){
				document.getElementById('products_menu').style.marginTop = '-7px';
			} else {
				document.getElementById('products_menu').style.marginTop = '32px';	
			}
			
			document.getElementById('products_menu').style.visibility = 'visible';
			
			if($.browser.msie && /8.0/.test(navigator.userAgent)){
				$('#products_menu').css('margin-top','-6px');
			}
			
			if($.browser.msie && /7.0/.test(navigator.userAgent)){
				$('#products_menu').css('margin-top','-6px');
				$('#products_menu').css('margin-left','-838px');
			}
		}
	},
	showTalkMenu : function(show){
		if(!show){
			document.getElementById('talk_menu').style.visibility = 'hidden';
			document.getElementById('talk_link2').className = 'ltalk_menu_over';
			return;
		}else{
			document.getElementById('talk_link2').className = 'ltalk_menu_overHover';
			position = interface.getPosition('talk_link');
			document.getElementById('talk_menu').style.marginLeft = (position.x - 2) + 'px';
			//document.getElementById('talk_menu').style.marginTop = '32px';
			
			if($.browser.msie && $.browser.version=="6.0"){
				document.getElementById('talk_menu').style.marginTop = '-7px';
			} else {
				document.getElementById('talk_menu').style.marginTop = '32px';	
			}
			
			document.getElementById('talk_menu').style.visibility = 'visible';
			
			if($.browser.msie && /8.0/.test(navigator.userAgent)){
				$('#talk_menu').css('margin-top','-6px');
			}
			
			if($.browser.msie && /7.0/.test(navigator.userAgent)){
				$('#talk_menu').css('margin-top','-6px');
				$('#talk_menu').css('margin-left','-582px');
			}
		}
	},
	
	getPosition : function (obj) {
		
		vx = document.getElementById(obj).offsetLeft;
		vy = document.getElementById(obj).offsetTop;
		return {x:vx,y:vy}
	},
	showSubMenu : function(obj,ord,source){
		obj = interface.getPosition(obj);
		//submenu = interface.d('products_submenu');
		document.getElementById('submenuContent').innerHTML = document.getElementById(source).innerHTML;
		submenu.style.marginLeft = (obj.x + 300) + 'px';
		submenu.style.marginTop = (43*ord) + (14*(ord-2)) + 'px';
		
		
			if($.browser.msie && /7.0/.test(navigator.userAgent)){
				$('#products_submenu').css('margin-left', obj.x - 657 + 'px');
			}
		
		submenu.style.display = '';
	},
	visibleSubmenu : function(show){
		/*if(!show){
			document.getElementById('products_submenu').style.display = 'none';
		}else{
			document.getElementById('products_menu').style.display = '';
		}*/
	},
	persistSubmenu : function(show){
		
			/*if(!show){
				document.getElementById('products_submenu').style.display = 'none';
				document.getElementById('products_menu').style.display = 'none';
				document.getElementById('products_link2').className = 'lproducts_menu_over';	
			}else{
				document.getElementById('products_menu').style.display = '';
				document.getElementById('products_link2').className = 'lproducts_menu_overHover';
			}*/
		
	},
	recipiesHome : function(id,type){
		$.post('/ajax/recipes_home.php',{id:id,type:type},function(data){						  
			if(type == 'recipes'){
				$('#home_foodTitle').html(data.title);
				$('#home_foodDescription').html(data.description);
				$('#home_foodIngredients1').html(data.ingredients);
				$('#home_foodIngredients2').html(data.ingredients2);
				$('#home_foodStars').html(data.stars);
				$('#home_foodVideo').html(data.video);
				$('#home_link_recipe').html(data.link);
				$('#home_link_recipe').show();
				$('#home_foodIngredients1').css('display','');
				$('#home_foodIngredients2').css('display','');
				$('#home_foodStars').css('visibility','');
				$('.home_foodRated').css('visibility','');
				$('#home_foodVideo').css('visibility','');
				$('#home_itjpg').css('display','');
				interface.recipesHomeImage(data.image);
			}else{
				$('#home_foodTitle').html(data.title);
				$('#home_foodDescription').html(data.description);
				$('#home_link_recipe').hide();
				$('#home_foodIngredients1').css('display','none');
				$('#home_foodIngredients2').css('display','none');
				$('#home_foodStars').css('visibility','hidden');
				$('.home_foodRated').css('visibility','hidden');
				$('#home_foodVideo').css('visibility','hidden');
				$('#home_itjpg').css('display','none');
				interface.recipesHomeImage(data.image);
			}
		},'json');
	},
	recipesHomeImage : function(image){
		var is = {1:'#rh_div', 2:'#rh_div2'};
		if($(is[1]).css('display') == 'none'){
			adi = is[2];
			idi = is[1];
		}else{
			adi = is[1];
			idi = is[2];
		}
		
		if(image == ''){
			$(adi).fadeOut(500);
			return;
		}
		$(idi).html(image);
		$(adi).fadeOut(500,function(){  $(idi).fadeIn(500)  });
		
		
	},
	cookBook : function(){

		var links = $('.home_foodNext');
		$('#' + links[0].id).css('text-decoration','underline');
		links.click(function(e){ 
			interface.automatic = false;
			$('.home_foodNext').css('text-decoration','none');
			$('#' + e.target.id).css('text-decoration','underline');
			
			
							 });
		var json = {
			1 : function(){
				interface.recipiesHome(links[0].id, 'recipes');
			},
			2 : function(){
				interface.recipiesHome(links[1].id, 'promotion');
			},
			3 : function(){
				interface.recipiesHome(links[2].id, 'recipes');
			},
			4 : function(){
				interface.recipiesHome(links[3].id, 'promotion');
			},
			5 : function(){
				interface.recipiesHome(links[4].id, 'recipes');
			}
		};
		
		var i=1;
		var inter = function(){
			if(!interface.automatic) clearInterval(interval);
			i++;
			if ( i == 6 ) i=1;
			json[i]();
			
			$('.home_foodNext').css('text-decoration','none');
			$('#' + links[i-1].id).css('text-decoration','underline');
			
			
		}
		var interval = setInterval(inter, 5000);
	},
	addToFavorite : function(id){
            $.ajax({
                type: 'POST',
                url: interface.root+'ajax/addtofavorite.php',
                dataType: 'html',
                data: 'id='+id,
                success: function(r){
                    if(r.match('<!--OK-->')){
                        window.location = interface.root+'recipebox/';
                    } else  {
                        window.location = interface.root+'tacoclub/';
                    }
                }
            })
        },
        deleteFavorte : function(id){
            $.ajax({
                type: 'POST',
                url: interface.root+'ajax/deletefavorite.php',
                dataType: 'html',
                data: 'id='+id,
                success: function(r){
                    if(r.match('<!--OK-->')){
                        window.location = interface.root+'recipebox/';
                    }	
                }
            })
        },
        sendtoFriend: function(){
            var data = $('#recibesfriends').serialize();
            $.fn.colorbox({href:interface.root+'ajax/sendtofriend.php?data=data&' + data, open:true});
        },
        printRecipes: function(id){
            var URLStr = this.root+'recipes/print_recipes.php?id='+id;
            var width = 500;
            var height = 500;
            window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=1,copyhistory=no,width='+width+',height='+height+',left=300, top=300,screenX=300,screenY=300');
            //$.fn.colorbox({href:'/recipes/print_recipes.php?id='+id, open:true});
        },
        printTips: function(id){
            var URLStr = this.root+'tips/print_tips.php?id='+id;
            var width = 500;
            var height = 500;
            window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable="no",copyhistory=no,width='+width+',height='+height+',left=300, top=300,screenX=300,screenY=300');
        },
        printProducts: function(id){
            var URLStr = this.root+'products/print.php?id='+id;
            var width = 700;
            var height = 700;
            window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=1,copyhistory=no,width='+width+',height='+height+',left=300, top=300,screenX=300,screenY=200');
        },
		printFun: function(id){
            var URLStr = this.root+'familyactivities/print.php?id='+id;
            var width = 700;
            var height = 700;
            window.open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=1,copyhistory=no,width='+width+',height='+height+',left=300, top=300,screenX=300,screenY=200');
        },
        mailRecipes: function(id){
            $.fn.colorbox({href:interface.root+'recipes/mail_recipes.php?id='+ id, open:true, scrolling:false});
        },
        mailTips: function(id){
            $.fn.colorbox({href:interface.root+'tips/mail_tips.php?id='+ id, open:true});
        },
		
		
		showNutrition: function(id) {
			$.fn.colorbox({href:interface.root+'products/nutrition.php?id='+ id, open:true});
		},
	
        changeViewRecipe: function(){
            if ( document.getElementById('videoDIV').style.display == 'none' ) {
                document.getElementById('imageDIV').style.display = 'none';
                document.getElementById('videoDIV').style.display = '';
            } else {
                document.getElementById('videoDIV').style.display = 'none';
                document.getElementById('imageDIV').style.display = '';
            }
        },
		preloadAndPNGFix: function(){
			image_url = new Array();
			image_url[0] = "/images/menu/family_over.png";
			image_url[1] = "/images/menu/family_top.png";
			image_url[2] = "/images/menu/history_over.png";
			image_url[3] = "/images/menu/home_over.png";
			image_url[4] = "/images/menu/products_over.png";
			image_url[5] = "/images/menu/promotions_over.png";
			image_url[6] = "/images/menu/recipe_bottom.png";
			image_url[7] = "/images/menu/recipe_half.png";
			image_url[8] = "/images/menu/recipe_top.png";
			image_url[9] = "/images/menu/recipes_over.png";
			image_url[10] = "/images/menu/rewards_over.png";
			image_url[11] = "/images/menu/submenu_bottom.png";
			image_url[12] = "/images/menu/submenu_half.png";
			image_url[13] = "/images/menu/submenu_separator.png";
			image_url[14] = "/images/menu/submenu_top.png";
			image_url[15] = "/images/menu/talk_over.png";
			image_url[16] = "/images/menu/whats_over.png";
			image_url[17] = "/images/menu/where_over.png";

			/*
			var preload_image_object = new Array();
			for(i=0; i<image_url.length; i++){
				preload_image_object[i] = new Image();
				preload_image_object[i].src = image_url[i];
			}
			*/
		  var cache = [];
			for (var i = image_url.length; i--;) {
				var cacheImage = document.createElement('img');
				cacheImage.src = image_url[i];
  			cache.push(cacheImage);
			}

			
			//$('#image_testing_msk').attr('src', preload_image_object[3]);
			//document.getElementById('image_testing_msk').src = preload_image_object[3]
			
			if ( $.browser.msie && $.browser.version=="6.0" ) {
				//pngfix();
			}
		},
		rotatorSearch : function(){
			var now = 0;
			var rot = function(){
				rs = $('.rotators');
				total = rs.length;
				/*for(i = 0; i < total ; i++){
					if($('#rotator_' + i).css('display') == ''){
						alert(i);
						now = i;
						break;
					}
				}*/
				//alert(now);
				if(now == (total - 1)){
					next = 0;	
				}else{
					next = parseInt(now) + 1;	
				}
				$('#rotator_' + now).fadeOut(500,function(){
					$('#rotator_' + next).fadeIn(500);
				});
				
				//alert(now);
				now = next;
			}
			
			setInterval(rot,4000);
			
		},
		login: function(url, msj){
			var data = 'url='+url+'&msj='+msj;
            $.fn.colorbox({href:interface.root+'ajax/login2.php?data=data&' + data, open:true});	
		},
		forgotYourPassword: function(){
            //var data = $('#recibesfriends').serialize();
            $.fn.colorbox({href:interface.root+'ajax/forgot_password.php?data=data&', open:true, scroll:false});
    }
}
function contactForm(){
    $('#formdiv').hide();
    $('#formloading').show();
    $('#formans').hide();
    var data = $('#formcontact').serialize();
    $.ajax({
        type: 'POST',
        url: interface.root+'ajax/contact.php',
        dataType: 'html',
        data: data,
        success: function(r){
            if(r.match('<!--ERROR-->')){
                var hideLoad = function(){
                    $('#formdiv').show();
                    $('#formans').show();
                    $('#formloading').hide();
                    $('#formans').html(r);
                }
                setTimeout(hideLoad, 1000);
            } else {
				/*
                var hideLoad = function(){
                    $('#formloading').hide();
                    $('#formans').show();
                    $('#formans').html(r);
                }
                setTimeout(hideLoad, 1000);
				*/
				window.location = interface.root+'contact/thankyou.php';
            }
        }
    })
}

var pageForms = {
    newTopic : function(){
        $('#formdiv').hide();
        $('#formloading').show();
        $('#formans').hide();
        var data = $('#formnewtopic').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/new_topic.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#formdiv').show();
                        $('#formans').show();
                        $('#formloading').hide();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#formloading').hide();
                        $('#formans').show();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                }
            }
        })
    },
    newReply : function(){
        $('#formdiv').hide();
        $('#formloading').show();
        $('#formans').hide();
        var data = $('#fromreply').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/new_reply.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#formdiv').show();
                        $('#formans').show();
                        $('#formloading').hide();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#formloading').hide();
                        $('#formans').show();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                }
            }
        })
    },
    newCommentVideo : function(){
        $('#formdiv').hide();
        $('#formloading').show();
        $('#formans').hide();
        var data = $('#vb_comment_form').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/new_comment_video.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#formdiv').show();
                        $('#formans').show();
                        $('#formloading').hide();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#formloading').hide();
                        $('#formans').show();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                }
            }
        })
    },
    newRecipesReview : function(){
        $('#formdiv').hide();
        $('#formloading').show();
        $('#formans').hide();
        var data = $('#frm_reviews').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/new_recipes_review.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#formdiv').show();
                        $('#formans').show();
                        $('#formloading').hide();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#formloading').hide();
                        $('#formans').show();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                }
            }
        })
    },
	
	login: function() {
		//$('#signin-form').fadeIn(400).trigger('show');
		$('#signin-form').show();
		$.fn.colorbox({inline:true, href:$('#signin-form'), scrolling:false, open:true, onCleanup:function() { $('#signin-form').hide().trigger('hide'); } });	
	},
	
    loginForm : function(e){
		
        $('#loginForm').hide();
        $('#loginLoad').show();
        $('#loginAns').hide();
		$.colorbox.resize();
        var data = $('#frm_login').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/login.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#loginForm').show();
                        $('#loginAns').show();
                        $('#loginLoad').hide();
                        $('#loginAns').html(r);
						$.colorbox.resize();
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(rr){
                        $('#loginLoad').hide();
                        $('#loginAns').show();
                       	$('#loginAns').html(rr);
						$.colorbox.resize();
						setTimeout(function() { location.reload(); }, 2000);
                    }
                    setTimeout(function() { hideLoad(r) }, 1000);
                }
            }
        })
    },
	loginFormRecipes : function(){
        $('#r_login_form').hide();
        $('#r_login_load').show();
        $('#r_login_ans').hide();
        var data = $('#r_login').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/login.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#r_login_form').show();
                        $('#r_login_ans').show();
                        $('#r_login_load').hide();
                        $('#r_login_ans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#r_login_load').hide();
                        $('#r_login_ans').show();
                        $('#r_login_ans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                }
            }
        })
    },
    logout : function(){
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/logout.php',
            dataType: 'html',
            success: function(){
                window.location = './';
            }
        })
    },
    sendfriend: function(){
        $('#form').hide();
        $('#formloading').show();
        $('#formans').hide();
		$.colorbox.resize();
        var data = $('#frmsendfriend').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/sendtofriend_send.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#form').show();
                        $('#formans').show();
                        $('#formloading').hide();
                        $('#formans').html(r);
						$.colorbox.resize();
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#formloading').hide();
                        $('#formans').show();
                        $('#formans').html(r);
						$.colorbox.resize();
                    }
                    setTimeout(hideLoad, 1000);
                }
				
            }
        })
    },
    sendfriendTips: function(){
        $('#form').hide();
        $('#formloading').show();
        $('#formans').hide();
        var data = $('#frmsendfriend').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/sendtofriend_send_tips.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#form').show();
                        $('#formans').show();
                        $('#formloading').hide();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#formloading').hide();
                        $('#formans').show();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                }
            }
        })
    },
	recoveryPassword: function(){
        $('#form').hide();
        $('#formloading').show();
        $('#formans').hide();
        var data = $('#frmrecoverypassword').serialize();
        $.ajax({
            type: 'POST',
            url: interface.root+'ajax/send_password.php',
            dataType: 'html',
            data: data,
            success: function(r){
                if(r.match('<!--ERROR-->')){
                    var hideLoad = function(){
                        $('#form').show();
                        $('#formans').show();
                        $('#formloading').hide();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                } else {
                    var hideLoad = function(){
                        $('#formloading').hide();
                        $('#formans').show();
                        $('#formans').html(r);
                    }
                    setTimeout(hideLoad, 1000);
                }
            }
        })
    }
};
