var sugarsnap = 
{
	tempVar: "",
	
	addFileUploader: function(fileType)
	{
		new Ajax.Request("/template/pages/getiframe.php", {
			method: 'get',
			parameters: {file_type:fileType},
			onComplete: function(req) {
				try {
						switch(fileType){
							case 'A':
										new Insertion.Bottom($$('div.uploaderAttachment').first(), req.responseText);
										break;
							case 'I':
										new Insertion.Bottom($$('div.uploaderImage').first(), req.responseText);
										break;
							case 'V':
										new Insertion.Bottom($$('div.uploaderVideo').first(), req.responseText);
										break;
						}
				}catch(e){ 
						alert(e);
						return; 
				}
			}
		});
	},


	removeElement: function()
	{
		var doc = window.frames;
		for(i=0; i<doc.length; i++)
		{
			if(window.frames[i].document.getElementById('addButton'))
				Element.remove(window.frames[i].document.getElementById('addButton'));
		}
	},


	submitAJAXData: function(url, params, callback)
	{
		generic_id = 0;
		if (arguments.length > 3)
		{
			generic_id = arguments[3];
			
		}

		new Ajax.Updater('',
			url,
			{
				parameters: params,
				evalScript: true,
				onComplete: function(request){
					eval("sugarsnap." + callback + "_callback")(request);
				}
			}
		)
	},


	remove_product_files: function(id, type){
		if(confirm('Are you sure you want to delete this file?'))
			sugarsnap.submitAJAXData('/delete_product_file', 'id=' + id, 'remove_product_files');
		else
			return false;
	},


	remove_product_files_callback: function(request){
		if(request.responseText == 'error'){
			alert('<?=$language->line("error_deletimg_file")?>');
		}else{
			var my_var = 'div.' + request.responseText.gsub('\n', '').strip();
			

			$$(my_var).first().style.background = '#FFE2E2';
			new Effect.Opacity(
				$$(my_var).first(), 
					{ 
						from: 1.0, to: 0.0,
						duration: 0.9 ,
						afterFinish: function(){
							$$(my_var).first().remove();
						}
					}
			);
		}
	},

	
	remove_product_files_iframe: function(id, tempid){
		if(confirm('Are you sure you want to delete this file?')){
			sugarsnap.tempVar = tempid;
			sugarsnap.submitAJAXData('/delete_product_file', 'id=' + id, 'remove_product_files_iframe');
		}else
			return false;
	},

	
	remove_product_files_iframe_callback: function(request){
		if(request.responseText == 'error'){
			alert('<?=$language->line("error_deletimg_file")?>');
		}else{
			var d = parent.document.getElementById('divframe' + sugarsnap.tempVar);
			var olddiv = parent.document.getElementById('theframe' + sugarsnap.tempVar);
			d.removeChild(olddiv);

			//parent.document.getElementById('divframe' + sugarsnap.tempVar).removeElement();
			//parent.document.getElementById('divframe' + sugarsnap.tempVar).style.display = 'none';
			/*parent.document.getElementById('divframe' + sugarsnap.tempVar).style.background = '#FFE2E2';
			new Effect.Opacity(
				parent.document.getElementById('divframe' + sugarsnap.tempVar), 
					{ 
						from: 1.0, to: 0.0,
						duration: 0.9 ,
						afterFinish: function(){
							parent.document.getElementById('divframe' + sugarsnap.tempVar).remove();
						}
					}
			);*/
		}
	},


	review_comment_callback: function(request){
		new Insertion.Bottom('comment_listing', request.responseText);
		$('comment_text').value = "";
		$('addCommentForm').hide();
		$('add_comment_link').show();
	},
	

	show_hide_dom_element: function(element_id){
			$(element_id).show();
			Effect.ScrollTo(element_id);
	},


	recommend_review_callback: function(request){
		if(generic_id){
			new Effect.Opacity($("review_container_" + generic_id), { from: 0.3, to: 1.0, duration: 0.5 });
			$('recommendReview' + generic_id).update(request.responseText);
		}
		else{
			new Effect.Opacity($("productreview"), { from: 0.3, to: 1.0, duration: 0.5});
			$('recommendReview').update(request.responseText);
		}
	},


	recommend_QA_callback: function(request){
		if(generic_id)
			$('recommendQA' + generic_id).update(request.responseText);
		else
			$('recommendQA').update(request.responseText);
	},


	update_video_detail_callback: function(request){
		new Effect.Opacity($('file_' + generic_id), { from: 0.3, to: 1.0, duration: 0.5 });
	},


	autosave: function (url, params, function_name) { 
		var t = setTimeout("autosave()", 20000); 
		sugarsnap.submitAJAXData(url, params, function_name) ;
	},


	review_autosave_callback: function(request)
	{
		$('autosave_review_time').update(request.responseText);
	},


	popup_div: function(top_adjustment,width)
	{
		var brow_width = document.body.scrollWidth;
		divele = document.createElement("div");
		divele.id=  "popup";
		divele.style.position="absolute";
		if(typeof top_adjustment == 'undefined')
			divele.style.top = (this.browser_top_position() + 60) + 'px';
		else
			divele.style.top = (this.browser_top_position() + parseInt(top_adjustment)) + 'px';

		if(typeof width != 'undefined')
		{
			divele.style.width = width + 'px';
			half_width = parseInt(width)/2;
			half_brower = parseInt(brow_width)/2;
			new_left = half_brower - half_width -10;
			divele.style.left = new_left+'px';
		}
		else
		{
			divele.style.left = '250px';
		}
		document.body.appendChild(divele);	
		divele.style.zIndex = 1000;
	},


	open_question_popup: function(product_id)
	{
		this.fadeBG(true);
		this.popup_div(150,400);
		new Ajax.Updater("popup","/add_question/"+product_id,{evalScripts:true});
	},


	close_popup: function()
	{
		$('popup').remove();
		this.fadeBG(false);
	},


	add_product_question: function()
	{
		if($('description').value=='')
		{
			alert('Enter description');
			return false;
		}
		var qstring = $('add_question').serialize(true);
		var url = '/add_question/';
		var opt = { method: 'post',
					parameters :qstring,
					onSuccess: function(transport) {
						Cookie.erase("__CJ_refine");
						sugarsnap.close_popup();
						sugarsnap.page_reload();},
					evalScripts: true
					};	
		var ajaxObj = new Ajax.Request(url, opt); 

	},	


	scrollHeightAccess: function()
	{
		if(!Prototype.Browser.WebKit) 
			return Math.max(window.document.documentElement.offsetHeight, window.document.documentElement.scrollHeight);
		else 
			return window.document.body.scrollHeight;	
	},


	browser_top_position: function()
	{
		var ie=document.all && !window.opera
		standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
		scroll_top=(ie)? standardbody.scrollTop : window.pageYOffset
		return scroll_top;
	},


	fadeBG: function(cond)
	{
		if(cond)
		{
			$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
			var divele = document.createElement("div");
			divele.style.position="absolute";
			divele.id="faded_div";
			divele.style.top="0px";
			divele.style.left="0px";
			divele.style.height= this.scrollHeightAccess()+'px';
			divele.style.width=document.body.scrollWidth+"px";
			divele.style.zIndex = 555;
			divele.style.backgroundColor="#cccdc9"; // lighty
			document.body.appendChild(divele);
			Element.setOpacity("faded_div", 0.85);
		}
		else
		{
			$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
			if(document.getElementById('faded_div')) document.body.removeChild(document.getElementById('faded_div'));
		}
	},


	 open_example_affiliation: function()
	{
		this.fadeBG(true);
		this.popup_div(150,400);
		new Ajax.Updater("popup","/example_affiliation");
	},	


	open_search_tips: function()
	{
		this.fadeBG(true);
		this.popup_div(150,400);
		new Ajax.Updater("popup","/searchtips");
	},	


	open_share_form: function(element_id, element_type)
	{
		this.fadeBG(true);  
		this.popup_div(120,600);
		opt= {
					method:"post",
					parameters: "element_id="+ element_id +"&element_type="+ element_type,
					evalScripts: true
				};
			new Ajax.Updater("popup", "/share",opt);
	},
	
	
	open_ask_the_reviewer: function(user_id, review_id, product_id)
	{
		this.fadeBG(true);  
		this.popup_div(120,600);
		opt= {
					method:"post",
					parameters: "user_id="+ user_id +"&review_id="+ review_id+"&product_id="+ product_id,
					evalScripts: true
				};
			new Ajax.Updater("popup", "/ask",opt);
	},


	track_product: function(product_id){
			$('fav_track_product_id').value = product_id;
			$('fav_track_action_type').value = "track_product";
			$('fav_track_product_form').submit();
	},
	
	favorite_product: function(product_id){

			$('fav_track_product_id').value = product_id;
			$('fav_track_action_type').value  = "favorite_product";
			$('fav_track_product_form').submit();
	},
	
	page_reload: function(){
			window.location.reload(true);
	},
	
	
	pulsate_effect_on_id: function(id){
			new Effect.Pulsate(id);	
	},

	
	show_hide_with_effect: function(divId, show_hide){
		if(show_hide == 1)
		{
			new Effect.BlindUp(divId);
			$(divId + '_image').innerHTML = '<a href="javascript:void(0)" onclick="sugarsnap.show_hide_with_effect(\'' + divId + '\', 0);"><img src="/img/leftcol/arrowside.gif" alt="Arrow" width="5" height="6" align="absmiddle"><a>';
		}else{
			new Effect.BlindDown(divId);
			$(divId + '_image').innerHTML = '<a href="javascript:void(0)" onclick="sugarsnap.show_hide_with_effect(\'' + divId + '\', 1);"><img src="/img/leftcol/arrow.gif" alt="Arrow" width="6" height="5" align="absmiddle"><a>';
		}
	},
	
	show_hide_with_effect_history: function(divId, show_hide){
		if(show_hide == 1)
		{
			new Effect.BlindUp(divId);
			$(divId + '_image').innerHTML = '<a href="javascript:void(0)" onclick="sugarsnap.show_hide_with_effect_history(\'' + divId + '\', 0);"><img src="/img/global/arrow1.gif" alt="Arrow" width="6" height="7" align="absmiddle" class="imgmargin_rating1"><a>';
			$(divId + '_link').innerHTML = '<a href="javascript:void(0)" onclick="sugarsnap.show_hide_with_effect_history(\'' + divId + '\', 0);"><strong>See earlier Shared Activity</strong><a>';
 			Effect.ScrollTo('shared_history_link');
		}else{
			new Effect.BlindDown(divId);
			$(divId + '_image').innerHTML = '<a href="javascript:void(0)" onclick="sugarsnap.show_hide_with_effect_history(\'' + divId + '\', 1);"><img src="/img/global/arrow2.gif" alt="Arrow" width="6" height="5" align="absmiddle" class="imgmargin_rating1"><a>';
			$(divId + '_link').innerHTML = '<a href="javascript:void(0)" onclick="sugarsnap.show_hide_with_effect_history(\'' + divId + '\', 1);"><strong>See earlier Shared Activity</strong><a>';
			Effect.ScrollTo('shared_history_link');
		}
	},

	update_left_panel_tracked_product: function()
	{
		opt ={parameters: "action_type=left_panel_track_update"};
		new Ajax.Updater('left_panel_track_updates',"/product_ajax_actions",opt);
	},
	update_left_panel_colleague_shared: function()
	{
		opt ={parameters: "action_type=left_panel_colleague_shared"};
		new Ajax.Updater('left_panel_colleague_shared',"/product_ajax_actions",opt);
	},

	update_left_panel_your_stuff: function()
	{
		opt ={parameters: "action_type=left_panel_your_stuff"};
		new Ajax.Updater('left_panel_your_stuff',"/product_ajax_actions",opt);
	},

	your_stuff_question_read_all: function()
	{
		if(confirm("Are you sure you want to mark all unread items as read?"))
		{
			opt ={	
					parameters: "action_type=your_stuff_question_update",
					onComplete: function()
								{
									$('question_mark_all_read_btn').hide();
									sugarsnap.page_reload();
									//sugarsnap.update_left_panel_your_stuff();
								}
				};	
			new Ajax.Request("/mark_updates_read",opt);		
		}
	},

	your_stuff_review_read_all: function()
	{
		if(confirm("Are you sure you want to mark all unread items as read?"))
		{
			opt ={	
					parameters: "action_type=your_stuff_review_update",
					onComplete: function()
								{
									$('review_mark_all_read_btn').hide();
									sugarsnap.page_reload();
									//sugarsnap.update_left_panel_your_stuff();
								}
				};	
			new Ajax.Request("/mark_updates_read",opt);	
		}
	},
	
	colleagues_shared_items_read_all: function()
	{
		if(confirm("Are you sure you want to mark all unread items as read?"))
		{
			opt ={	
				parameters: "action_type=colleagues_shared_items",
				onComplete: function()
							{
								$('colleagues_shared_mark_all_read_btn').hide();
								sugarsnap.page_reload();
								//sugarsnap.update_left_panel_colleague_shared();
							}
				};	
			new Ajax.Request("/mark_updates_read",opt);			
		}
	},
	
	open_add_description_page: function(element_id)
	{
		this.fadeBG(true);  
		this.popup_div(120,600);
		opt= {
					method:"post",
					parameters: "product_id=" + element_id,
					evalScripts: true
				};
			new Ajax.Updater("popup", "/add-product-description",opt);
	},

	allow_memeber_ask_ques_on_review: function(review_id,ele)
	{
		mark = "0";
		if(ele.checked){
			mark = "1";
		}

		opt = {parameters:"action_type=allow_memeber_ask_ques_on_review&review_id=" + review_id + "&mark=" + mark};
		
		new Ajax.Request("/product_ajax_actions",opt);

	},
	show_user_profile: function(e, userId, uniqId)
	{
		/*try{
									
				var browser_type = navigator.appName;
				var browser_version = parseInt(navigator.appVersion);
				
				//if IE 6
				if (browser_type=="Microsoft Internet Explorer" && (browser_version==6 || browser_version==4))
				{
					pos = $(uniqId).cumulativeOffset();
					$('profileinfo').setStyle({'top':(pos[1] - 55) +'px', 'left': (pos[0] + 40) + 'px'});
				}
				//if IE 7
				else if (browser_type=="Microsoft Internet Explorer" && browser_version==7)
				{
					pos = $(uniqId).cumulativeOffset();
					$('profileinfo').setStyle({'top':(pos[1] - 55) +'px', 'left': (pos[0] + 40) + 'px'});
				}
				//Default goto page (NOT IE 6 and NOT IE 7)
				else{*/
					pos = $(uniqId).cumulativeOffset();
					
					$('profileinfo').setStyle({'top':(pos[1] - 55) +'px', 'left': (pos[0] + 40) + 'px'});
				/*}
			}catch(e){
				alert(e);
			}*/
		
		opt ={	
				method:"post",
				parameters: "user_id=" + userId,
				onComplete: function(req)
							{
								$('profileinfo').innerHTML = req.responseText;
								$('profileinfo').show();								
							}
			};	
		new Ajax.Request("/user",opt);		
	}
	
};
