$(document).ready(function(){

	
	// ** ADMIN ** //

	
    /**
     * add styles to first column of form fields
     */
	$("td.formCol1:odd").css({
		"background-color" : "#e8dfc5",
		"padding" : "5px",
		"margin" : "0 0 10px 10px"
	});
	
	$("td.formCol1:even").css({
		"background-color" : "#e8dfc5",
		"padding" : "5px",
		"margin" : "0 0 10px 10px"
	});
	
	$("td.formCol1 + td").css({
		"padding" : "5px",
		"margin" : "0 0 10px 10px",
		"font-size" : "12px",
		"font-family" : "Trebuchet MS, sans-serif"
	});

	$(".assetInfo").css({
		"font-size" : "12px",
		"font-family" : "Trebuchet MS, sans-serif"
	});
	
	
	/** generic cancel btn that carries its desitnation as a property; edit on view page
	 * 
	 */
	
	$("input#cancel_Redirect").click(function () {
		var id = $(this).attr("object_id");
		var loc = $(this).attr("loc");
		location.href = loc+id;     
    });
	
	/**
	 * 
	 */

	$("input#cancel_RedirectRoom").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		var room_id= $(this).attr("room_id");
		var loc = $(this).attr("loc");
		location.href = loc+restaurant_id+'/room_id:'+room_id;    
    });
	
	$("input#cancel_RedirectExperience").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		var experience_id= $(this).attr("experience_id");
		var loc = $(this).attr("loc");
		location.href = loc+restaurant_id+'/experience_id:'+experience_id;    
    });
	
	
	// redirects //
	
	// restaurant
	
	// de-activate -> de-activate confirm
	$("input#deactivate_RestaurantEdit").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		location.href = '/admin/restaurants/deactivate/'+restaurant_id;       
    });

	// activate -> activate confirm
	$("input#activate_RestaurantEdit").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		location.href = '/admin/restaurants/activate/'+restaurant_id;       
    });
	
	// view restaurant admins -> add restaurant admin
	$("input#add_RestaurantUsersView").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		location.href = '/admin/users/add/'+restaurant_id;     
    });

	// restaurant add photo -> photo upload
	$("input#add_RestaurantPhoto").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_photo/'+restaurant_id;     
    });
	
	// restaurant add photo -> homepage photo upload
	$("input#add_RestaurantHomepagephoto").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_homepagephoto/'+restaurant_id;     
    });	

	// restaurant add video -> video upload
	$("input#add_RestaurantVideo").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_video/'+restaurant_id;     
    });
	
	// restaurant add virtual tour -> upload
	$("input#add_RestaurantVirtualtour").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_virtualtour/'+restaurant_id;     
    });
		
	// restaurant add floorplans _> floorplan upload
	$("input#add_RestaurantFloorplan").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_floorplan/'+restaurant_id;     
    });
	
	// restaurant add static menu -> static menu upload
	$("input#add_RestaurantStaticmenu").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_staticmenu/'+restaurant_id;     
    });	
	
	// restaurant add brochure -> brochure upload
	$("input#add_RestaurantBrochure").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_brochure/'+restaurant_id;     
    });
	
	// restaurant add terms
	$("input#add_RestaurantTerms").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		location.href = '/admin/restaurants/add_restaurantterms/'+restaurant_id;     
    });	
	
	
	// room
	
	// room add photo -> room upload photo
	$("input#add_RoomPhoto").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var room_id = $(this).attr("room_id");	
		location.href = '/admin/restaurants/add_photo_room/'+restaurant_id+'/room_id:'+room_id;    
    });	
	
	// room add video -> room upload video
	$("input#add_RoomVideo").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var room_id = $(this).attr("room_id");	
		location.href = '/admin/restaurants/add_video_room/'+restaurant_id+'/room_id:'+room_id;    
    });
	
	// room add virtual tour -> room upload virtual tour
	$("input#add_RoomVirtualtour").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var room_id = $(this).attr("room_id");	
		location.href = '/admin/restaurants/add_virtualtour_room/'+restaurant_id+'/room_id:'+room_id;    
    });		
	
	// room add floorplan -> room upload floorplan
	$("input#add_RoomFloorplan").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var room_id = $(this).attr("room_id");	
		location.href = '/admin/restaurants/add_floorplan_room/'+restaurant_id+'/room_id:'+room_id;     
    });	
	
	// room add static menu -> room upload static menu
	$("input#add_RoomStaticmenu").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var room_id = $(this).attr("room_id");	
		location.href = '/admin/restaurants/add_staticmenu_room/'+restaurant_id+'/room_id:'+room_id;     
    });	
	
	
	// activate room -> activate room page
	$(".activate_RoomEdit").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var room_id = $(this).attr("room_id");
		location.href = '/admin/restaurants/activate_room/'+restaurant_id+'/room_id:'+room_id;        
    });
	
	// de-activate room -> de-activate room page
	$(".deactivate_RoomEdit").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		var room_id = $(this).attr("room_id");
		location.href = '/admin/restaurants/deactivate_room/'+restaurant_id+'/room_id:'+room_id;       
    });
	
	
	// experience //
	
	// experience add photo -> experience upload photo
	$("input#add_ExperiencePhoto").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var experience_id = $(this).attr("experience_id");	
		location.href = '/admin/restaurants/add_photo_experience/'+restaurant_id+'/experience_id:'+experience_id;    
    });	
	
	// experience add floorplan -> experience upload floorplan
	$("input#add_ExperienceFloorplan").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var experience_id = $(this).attr("experience_id");	
		location.href = '/admin/restaurants/add_floorplan_experience/'+restaurant_id+'/experience_id:'+experience_id;     
    });
	
	// experience add video -> experience upload video
	$("input#add_ExperienceVideo").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var experience_id = $(this).attr("experience_id");	
		location.href = '/admin/restaurants/add_video_experience/'+restaurant_id+'/experience_id:'+experience_id;     
    });
	
	// experience add virtual tour -> experience upload virtual tour
	$("input#add_ExperienceVirtualtour").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var experience_id = $(this).attr("experience_id"); 
		location.href = '/admin/restaurants/add_virtualtour_experience/'+restaurant_id+'/experience_id:'+experience_id;     
    });
		
	// experience add static menu -> experience upload static menu
	$("input#add_ExperienceStaticmenu").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");	
		var experience_id = $(this).attr("experience_id");	
		location.href = '/admin/restaurants/add_staticmenu_experience/'+restaurant_id+'/experience_id:'+experience_id;     
    });		
	
	// de-activate -> de-activate confirm
	$(".deactivate_ExperienceEdit").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		var experience_id = $(this).attr("experience_id");
		location.href = '/admin/restaurants/deactivate_experience/'+restaurant_id+'/experience_id:'+experience_id;       
    });

	// activate -> activate confirm
	$(".activate_ExperienceEdit").click(function () {
		var restaurant_id = $(this).attr("restaurant_id");
		var experience_id = $(this).attr("experience_id");
		location.href = '/admin/restaurants/activate_experience/'+restaurant_id+'/experience_id:'+experience_id;       
    });
	
	
	// configure //

	// cuisines delete -> cuisines delete page	
	$("input#delete_CuisineEdit").click(function () {
		var cuisine_id = $(this).attr("cuisine_id");
		location.href = '/admin/cuisines/delete/'+cuisine_id;       
    });
		
	// restaurant features delete -> restaurant features delete page
	$("input#delete_RestaurantFeatureEdit").click(function () {
		var feature_id = $(this).attr("feature_id");
		location.href = '/admin/restaurant_features/delete/'+feature_id;       
    });
	
	// room features delete -> room features delete page
	$("input#delete_RoomFeatureEdit").click(function () {
		var feature_id = $(this).attr("feature_id");
		location.href = '/admin/room_features/delete/'+feature_id;       
    });	

	// room features delete -> room features delete page
	$("input#delete_ExptypeEdit").click(function () {
		var exptype_id = $(this).attr("exptype_id");
		location.href = '/admin/exptypes/delete/'+exptype_id;       
    });	

	// general photos -> add
	$("input#add_RestaurantGeneralphoto").click(function () {
		location.href = '/admin/generalphotos/add_generalphoto'       
    });	
	
	

	/**
	 * 		USER
	 */
		
		
	// for DP Admins adding new users; manages Restaurant select field toggle
	// if coming from Restaurant mgmt page
	if ($("select#selectRole_UserAdd option:selected").val() == 3) {
		// if new user is NOT Restaurant Admin, remove select field name
		$("div.selectRestaurant_UserAdd").hide().fadeIn();
	}
	
	$("select#selectRole_UserAdd").change(function () {
		if ($("select#selectRole_UserAdd option:selected").val() == 3) {
			// if new user is Restaurant Admin force the select field name
			$("div.selectRestaurant_UserAdd").hide().fadeIn();
		} else {
			// if new user is NOT Restaurant Admin, remove select field name
			$("div.selectRestaurant_UserAdd").hide();
			var el = 'data[User][restaurant_id]';
			$("div.selectRestaurant_UserAdd").children(el).val('');
			//alert($("div.selectRestaurant_UserAdd").children(el).val());
		}
    });
	
	
	 // for DP Admins editing new users; manages Restaurant select field toggle	
	// if pages loads and this user's role != 3
	if ($("select#selectRole_UserEdit option:selected").val() != 3) {
		// if new user is NOT Restaurant Admin, remove select field name
		$("div.selectRestaurant_UserEdit").hide();
	}
	
	$("select#selectRole_UserEdit").change(function () {
		if ($("select#selectRole_UserEdit option:selected").val() == 3) {
			// if new user is Restaurant Admin force the select field name
			$("div.selectRestaurant_UserEdit").hide().fadeIn();
		} else {
			// if new user is NOT Restaurant Admin, remove select field name
			$("div.selectRestaurant_UserEdit").hide();
		}
    });


	// edit user -> delete user
	$("input#delete_UserEdit").click(function () {
		var user_id = $(this).attr("user_id");
		location.href = '/admin/users/delete/'+user_id;       
    });	
	
	
	/**
	 * 		RESTAURANT
	 */
	
	
	
	///
	
	// save and redirect
	$("a.add_RestaurantRedirect").click(function () {
		// append a hidden field to tell the controller where to send the redirect
		var redirect_id = $(this).attr('id');
		
		$("form#RestaurantAddForm").append("<input type='hidden' name='data[Restaurant][redirect_target]' value='"+redirect_id+"'>");
		$("form#RestaurantAddForm").submit();
		return false;
	});
	
	$("a.edit_RestaurantRedirect").click(function () {
		// append a hidden field to tell the controller where to send the redirect
		var redirect_id = $(this).attr('id');		
		$("form#RestaurantEditForm").append("<input type='hidden' name='data[Restaurant][redirect_target]' value='"+redirect_id+"'>");
		$("form#RestaurantEditForm").submit();
		return false;
	});	
	
	
	
	// ROOMS
	
	
	// save and redirect
	
	$("a.add_RoomRedirect").click(function () {
		// append a hidden field to tell the controller where to send the redirect
		var redirect_id = $(this).attr('id');
		$("form#RestaurantAddRoomForm").append("<input type='hidden' name='data[Room][redirect_target]' value='"+redirect_id+"'>");
		$("form#RestaurantAddRoomForm").submit();
		return false;
	});
	
	$("a.edit_RoomRedirect").click(function () {
		// append a hidden field to tell the controller where to send the redirect
		var redirect_id = $(this).attr('id');
		$("form#RestaurantEditRoomForm").append("<input type='hidden' name='data[Room][redirect_target]' value='"+redirect_id+"'>");
		$("form#RestaurantEditRoomForm").submit();
		return false;
	});
	
	//
	
	
	// view link for photo checkboxes
	// pulls paths from hidden divs on the page
	var x = $(".roomAdd_photoCheckbox").find(".checkbox").length;
	for (var y=0; y<x; y++) {
		$(".roomAdd_photoCheckbox").find(".checkbox:eq("+y+")").append("&nbsp;&nbsp;&nbsp;<img class='photoThumb_room' src='"+$(".roomAdd_photoPath").find(".photoPath:eq("+y+")").text()+"'>");
	}
	
	// view link for floorplan checkboxes
	// pulls paths from hidden divs on the page
	var x = $(".roomAdd_floorplanCheckbox").find(".checkbox").length;
	for (var y=0; y<x; y++) {
		$(".roomAdd_floorplanCheckbox").find(".checkbox:eq("+y+")").append(" | <a href='"+$(".roomAdd_floorplanPath").find(".floorplanPath:eq("+y+")").text()+"' class='nav'>view</a>");
	}
	
	// view link for static menu checkboxes
	// pulls paths from hidden divs on the page
	var x = $(".roomAdd_staticmenuCheckbox").find(".checkbox").length;
	for (var y=0; y<x; y++) {
		$(".roomAdd_staticmenuCheckbox").find(".checkbox:eq("+y+")").append(" | <a href='"+$(".roomAdd_staticmenuPath").find(".staticmenuPath:eq("+y+")").text()+"' class='nav'>view</a>");
	}
		
	// EXPERIENCES
	
	
	$("a.add_ExperienceRedirect").click(function () {
		// append a hidden field to tell the controller where to send the redirect
		var redirect_id = $(this).attr('id');
		$("form#RestaurantAddExperienceForm").append("<input type='hidden' name='data[Experience][redirect_target]' value='"+redirect_id+"'>");
		$("form#RestaurantAddExperienceForm").submit();
		return false;
	});
	
	$("a.edit_ExperienceRedirect").click(function () {
		
		// append a hidden field to tell the controller where to send the redirect
		var redirect_id = $(this).attr('id');
		$("form#RestaurantEditExperienceForm").append("<input type='hidden' name='data[Experience][redirect_target]' value='"+redirect_id+"'>");
		$("form#RestaurantEditExperienceForm").submit();
		return false;
	});


	// Delete confirmation alerts
	$('a.del-link').click(function(){
		if(confirm('Are you sure you want to '+$(this).attr('title')+'?')){
			return true;
		}
		return false;
	});
});