var swoff = base+'assets/js/widgets/iphone_switch_container_off.png';
var swon = base+'assets/js/widgets/iphone_switch_container_on.png';
var sw_bg = base+'assets/js/widgets/iphone_switch.png';
$(document).ready(function(){

	$('#printpage').bind('click',function(){
		
		window.print();
		return false;
		
	});
	
	$('#emailtocolleague').bind('click',function(){
	
  		var u = site+'ecomms/senddetails/';
	  	//window.alert('My id url '+u);
	  	tb_show('Send this page',u+"?width=300&height=260&TB_iframe=true",false)
		return false;
	});
	
	/*	$('#notifications').bind('click',function(){

	  		var u = site+'myspace/getnotificationspopup/';
		  	//window.alert('My id url '+u);
		  	//tb_show('Receive product update notifications',u+"?width=600&height=340&TB_iframe=true",false)

			return false;
		});*/
	$("#dialog").dialog("destroy");
		
	$('#notifications').bind('click',function() {
		$('#dialog-notifications').dialog('open');
	});
	
	$("#dialog-notifications").dialog({
		autoOpen: false,
		height: 250,
		width: 570,
		resizable: false,
		zIndex: 15000,
		modal: true,
		
		close: function() {
			//allFields.val('').removeClass('ui-state-error');
			$('.btnimg').die('click');
		},
		
		open: function() {
			$('#nsubmit').bind('click',function(){
				
				//submit the form
				var un = document.nform.username.value;
				var pw = document.nform.password.value;
				//alert('username is: '+un+", password is: "+pw);
				
				var jo = new Object();
				jo.un = un;
				jo.pw = pw;
				
				jsonStr = JSON.stringify(jo);
				
				//send it in
	        	$.ajax({
					url:'/myspace/ajax_login/'+jsonStr,
					type:'POST',
					dataType:"json",
					success:function(result){
						if(result instanceof Object){
							//alert(result['message']);
							$('nloggedin').css('display','block');
							var nc = getncontent(result['services']);
							$('#nloggedin').append(nc);
							$('#nloggedin').css('display','block');
							$('#nloginform').css('display','none');
						}else{
							//alert(result);
							var txt = '<p>Could not login, please <a href="myspace" try logging in first</p>'
							$('#nmsg').append(result);
						}
					}
				});
				
				return false;
			});
			$('.btnimg').live('mouseover',function(){
				$(this).css("cursor",'pointer');
			});
			$('.btnimg').live('mouseout',function(){
				$(this).css("cursor",'default');
			});
			$('.btnimg').live('click',function(){

				if($(this).attr('rel') == 1) {
					$(this).animate({backgroundPosition: -53}, "fast", function() {
						$(this).attr('src', swon);
						switch_off_callback($(this).attr('rev'));
					});
					$(this).attr('rel', 0);
					
				}
				else {
					$(this).animate({backgroundPosition: 0}, "fast", function() {
						switch_on_callback($(this).attr('rev'));
					});
					$(this).find('.iphone_switch').attr('src', swoff);
					$(this).attr('rel', 1);
				}	
			});
		}
	});

	
	$('#morelink').bind('click',function(){
	
  		var u = site+'ecomms/getmoreinfo/';
	  	//window.alert('My id url '+u);
	  	tb_show('Contact me about this',u+"?width=500&height=420&TB_iframe=true",false)
		return false;
	});
	
	$('#printpage, #emailtocolleague, #morelink, #notifications').bind('mouseover',function(){
		
		$(this).addClass('imgro');
		
	});
	
	$('#printpage, #emailtocolleague, #morelink, #notifications').bind('mouseout',function(){
		
		$(this).removeClass('imgro');
		
	});

})

function switch_off_callback(v){
	//make a call to the server to update prefs
	//alert('switching off service '+v);
	var sid = v;
	var val = false;
	
	var jo = new Object();
	jo.sid = sid;
	jo.val = val;
	
	jsonStr = JSON.stringify(jo);
	
	//send it in
	$.ajax({
		url:'/myspace/ajax_setservice/'+jsonStr,
		type:'POST',
		dataType:"json",
		success:function(result){
			alert(result)
		}
	});
	
	var txt = 'Your preferences have been updated';
	$('#nstatus').hide().addClass('flash-notify').html(txt).fadeIn();
	setTimeout(function(){
         $('#nstatus').fadeOut();
     }, 1800);
}

function switch_on_callback(v){
	//make a call to the server to update prefs
	//alert('switching on service '+v);
	var sid = v;
	var val = true;
	
	var jo = new Object();
	jo.sid = sid;
	jo.val = val;
	
	jsonStr = JSON.stringify(jo);
	
	//send it in
	$.ajax({
		url:'/myspace/ajax_setservice/'+jsonStr,
		type:'POST',
		dataType:"json",
		success:function(result){
			alert(result)
		}
	});
	
	var txt = 'Your preferences have been updated';
	$('#nstatus').hide().addClass('flash-notify').html(txt).fadeIn();
	setTimeout(function(){
         $('#nstatus').fadeOut();
     }, 1800);
}

function getncontent(s){
	var pst = "margin:6px 0;";
	var t = "<p style="+pst+">To add this notification to your subscriptions, just use the switch below. You can turn this off at any time by logging into your account area and clicking 'subscriptions'. To go to the login are now <a href=\"/myspace\" target=\"_top\"click here.</a></p>";
	var swoff = base+'assets/js/widgets/iphone_switch_container_off.png';
	var swoffpos = '-53px';
	var swon = base+'assets/js/widgets/iphone_switch_container_on.png';
	var swonpos = '0px';
	var sw_bg = base+'assets/js/widgets/iphone_switch.png';
	if(s.length < 1){
		var state = swoff;
		var spos = swoffpos;
		var mr="0";
	}
	else if(s['2']!=null){
		var state = swon;
		var spos = swonpos;
		var mr="1";
	}else{
		var state = swoff;
		var spos = swoffpos;
		var mr="0";
	}
	
	t += "<ul style='list-style:none;padding:0;margin:0;'>";
	t += "<li style=\"padding:5px;background-color:#f3ede3;display:inline-block;width:530px;margin-top:3px;\">";

	t += "<div style=\"float:left;width:280px;margin-top:6px;\">LCD Controller Notifications</div>";
	t += "<div style=\"float:right;width:120px;\">";
	t += "<div class='swcontainer' style=\"height:27px; width:94px; position: relative; overflow: hidden\">";
	var st = 'width:94px;height:27px;background-image:url('+sw_bg+');background-repeat:none; background-position:'+spos+';)';
	t += "<img src='"+state+"' class='btnimg' rel = '"+mr+"' rev= '2' style='"+st+"' />";
	t += "</div>";
	t += "</div>";

	t += "</li>";

	t += "<p style="+pst+"><span style='font-style:italic'>* Please Note: </span> This subscription will only allow you to receive updates for LCD Controllers. You will not receive any other communications through subscribing to this event.</p>";
	t += "</ul>";
	t += "<div style='clear:both;font-size:11px;padding:10px;border:1px solid gray;background-color:#f3ede3;display:none;' id=\"nstatus\"></div>";	
	return t;
}