var detectMagnetUIntervalID = '';
	var installationprocess = false;
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	var isff = false;
	var issafari = false;
    var isPluginInstalled = false;                   
    function searchPlugin() {
      isPluginInstalled = false;
      if ((typeof(navigator) == 'object') && (typeof(navigator.plugins) == 'object')) {
        $.each(navigator.plugins, function (index, item) {
          if (item.name == "ActiveX hosting plugin for magnetU V1.0") {
            isPluginInstalled = true;
          }
        });
      }
    }
    if (is_chrome) {
      searchPlugin();
    }	
				   if (navigator.userAgent.indexOf("Firefox")!=-1) {
					   isff=true;
					   searchPlugin();
				   }
				   if (navigator.userAgent.indexOf("Safari")!=-1) {
					   issafari=true;
					   searchPlugin();
				   }
	//detectMagnetUafterinstall();
    function registerMagnetUControlEventHandler(eventName, handlerFunction)
    {
		if (document.MagnetUControl.addEventListener)
        {
			if ($.browser.msie && parseInt($.browser.version, 10) == 9){
				document.MagnetUControl.attachEvent(eventName, handlerFunction);
			} else {
				document.MagnetUControl.addEventListener(eventName, handlerFunction, false);
			}
        }
        else if (document.MagnetUControl.attachEvent)
        {
            document.MagnetUControl.attachEvent(eventName, handlerFunction);
        }
		//document.MagnetUControl.attachEvent(eventName, handlerFunction);
    }

    var detectMagnetUTimesCalled = 0;
	
	function detectMagnetUafterinstall() {
		if ((!is_chrome)||(isff)||(issafari)) {
			$(".objmagnetU").html('<iframe src="/refreshplugin.php" frameborder="0" width="0" height="0"></iframe>');
		} else {
			if ((typeof(navigator) == 'object') && (typeof(navigator.plugins) == 'object')) {
              $.each(navigator.plugins, function (index, item) {
              if (item.name == "ActiveX hosting plugin for magnetU V1.0") {
                isPluginInstalled = true;
                foundMagnetU();
              }
            });
          }
		}
	}	
	function movetonextstep() {
		alert('installation done');
	}
	function retrydetection() {
      $('.alert').css('display','none');
      $('.magnetU').html('<div class="found" style="display: none;text-align: left; line-height:25px !important; padding-top: 18px;">You are now ready to create your magnetU profile!<div class="setsocialdesires" style="padding-top: 25px;"><a href="javascript:manageprofile();" target="_top"><img src="/images/putonyoursocialdesires.jpg" border="0" /></a></div></div><div class="loader"><img src="/images/loader.gif" /></div><div class="mimg"><img src="/images/magnetUsearch.jpg" /></div></div><div class="alert" style="display: none;"><img src="/images/alerticon.jpg" />&nbsp;Disconnect magnetU from computer, connect it again and confirm the magnetU driver installation wizard screens.</div>');
      $("#activexvalidation").attr('src','/include/libs/spcheckactivex.php');
      Cufon(".magnetU");
    }
	function installation() {
		installationprocess = true;
		$("#magnetUsearch").css("display","none");
		$("#shadow").toggle();
		$("#installationpackage").html('<div align="center"><div class="installation_panel"><div class="close"><img src="/images/installationclose.png" /></div><div class="steps"><ul><li class="step1"></li><li class="step2"></li><li class="step3"></li></ul></div><div class="instructions"><div class="in"><span class="title">Install magnetU on computer</span><br /><br />To allow your computer to access your magnetU,<br />it requires magnetU app to be installed on your<br />computer  (Windows XP/Vista/7).<br /><br /><input type="checkbox" class="installbutton_confirm" />&nbsp;I have read and accept magnetU <a href="/inner/terms.htm" target="_blank">Terms and Conditions</a></div></div><div class="installbutton"><div style="display: none;" class="ps"><img src="/images/installbutton_mouseover.png" /></div><a href="#"><img src="/images/mu_install_button.png" border="0" /></a></div><div class="alert" style="display: none;"><img src="/images/alerticon.jpg" />&nbsp;Please check the box.</div></div></div>');
		$(".step1").html('<div class="in"><span class="num">01.</span><br />Install magnetU app</div>');
		$(".step2").html('<div class="in"><span class="num">02.</span><br />Define your social desires profiles</div>');
		$(".step3").html('<div class="in"><span class="num">03.</span><br />Link your magnetU<br />with your mobile phone</div>');			
		Cufon("#installationpackage .installation_panel .steps ul li");
		Cufon("#installationpackage .installation_panel .instructions");
		Cufon("#installationpackage .installation_panel .installbutton");
		$(".step1").css("background-image","url(../../images/mu_install_step_current.png)");
		$(".installbutton").mouseover(function(){
			$(".ps").css("display","block");
		});
		$(".installbutton").mouseout(function(){
			$(".ps").css("display","none");
		});
		$(".installbutton").click(function(){
			if ($(".installbutton_confirm:checked").val()!='on') {
				$(".alert").css("display","block");
			};
		});
		$("#installationpackage").css("display","block");
		$(".installbutton_confirm").click(function(){
			if ($(".installbutton_confirm:checked").val()=='on') {
				/*$(".installbutton").css("background-color","#67368b");*/
				$(".installbutton a").attr("href","/bin/magnetUsetup.exe");					
				$(".alert").css("display","none");
				//$(".installbutton a").click(function(){
					detectMagnetUIntervalID = setInterval("detectMagnetUafterinstall()", 5000);
				//});
			} else {
				clearInterval(detectMagnetUIntervalID);
				$(".installbutton a").attr("href","#");
				$(".installbutton a").unbind('click');
			}
		});
		$(".close").click(function(){
			try {
	  		  if (pagename!=null) {
				if (pagename=='hp') {
                  	$(".detectdevice").html('Access your Social desires');
                  	Cufon('.detectdevice');
				}
			  }
			} catch(e) {
				$(".detectdevice").html('<span class="aysd">Access your Social desires</span><div id="magnetUsearch" class="sn_popup" style="display: none;"><div align="center"><div class="message"><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td align="left" class="messagetitle">Access your Social desires</td><td align="right" id="hide4"><a href="javascript:removepopup();"><img src="/images/spacer.gif" width="55" height="55" border="0" /></a></td></tr><tr><td colspan="2" height="132" class="messagecontent" align="left" style="color: white; padding-top: 15px;">Searching your magnetU</td></tr></table></div></div></div></div>');
			}			
			Cufon('.detectdevice');
			Cufon('.loginbutton');
			$("#shadow").toggle();
			$("#installationpackage").append('');
			$("#installationpackage").css("display","none");
		});
	}
	function recheck() {
		$('.messagecontent').html('Searching your magnetU');
		Cufon('.messagecontent');
		if (detectMagnetUTimesCalled==1) {
		} else {
			detectMagnetUTimesCalled = 0;
			detectMagnetU();
			detectMagnetUIntervalID = setInterval("detectMagnetU()", 1000);
		}
			
		registerMagnetUControlEventHandler('OnGetMEPIdentifierDone', foundMagnetUIdentifier);
	}
    function detectMagnetU()
    {        
		detectMagnetUTimesCalled++;
        if (detectMagnetUTimesCalled == 6)
        {
                clearInterval(detectMagnetUIntervalID);
                if (installationprocess) {
	 				foundMagnetU();
                } else {
                    $(".clickhere").html('<font style="color: red;">connect your magnetU and click here</font>');
					try {
			  		  if (pagename!=null) {
						if (pagename=='hp') {
                    	$(".detectdevice").html('Access your Social desires');
                    	Cufon('.detectdevice');
						}
					  }
					} catch(e) {
					}
					Cufon('.clickhere');
                    Cufon('.loginbutton');
					$('.messagecontent').html('connect your magnetU and <a href="javascript:recheck();" style="font-size: 22px; color: white;text-decoration: underline;border-bottom: solid 1px white;">click here</a>');
					Cufon('.messagecontent');
                }
        }
        else
        {
			document.MagnetUControl.GetMEPIdentifier();
        }
    }
	function foundMagnetUIdentifier(strMagnetUIdentifier) {
        clearInterval(detectMagnetUIntervalID);
		$("#MagnetUIdentifier").val(strMagnetUIdentifier);
		$("#detect_device_form").attr('action','/manage_profile.php');
		$("#detect_device_form").submit();
	}
        function manageprofile() {
            $("#detect_device_form").submit();
        }
    function foundMagnetU(strMagnetUIdentifier)
    {
        clearInterval(detectMagnetUIntervalID);
		$("#detect_device_form").attr('action','/manage_profile.php');
		$("#installationpackage").html('<div align="center"><div class="installation_panel"><div class="close"><img src="/images/installationclose.png" /></div><div class="steps"><ul><li class="step1 done"></li><li class="step2"></li><li class="step3"></li></ul></div><div class="instructions"><div class="in"><span class="title">Stick magnetU into computer</span><br /><br />This is the time to stick your magnetU into any USB port.</a></div></div><div class="magnetU"><div class="found" style="display: none; text-align: left; line-height:25px !important; padding-top: 18px;">You are now ready to create your magnetU profile!<div class="setsocialdesires" style="padding-top: 25px;"><a href="javascript:manageprofile();" target="_top"><img src="/images/putonyoursocialdesires.jpg" border="0" /></a></div></div><div class="loader"><img src="/images/loader.gif" /></div><div class="mimg"><img src="/images/magnetUsearch.jpg" /></div></div><div class="alert" style="display: none;"><img src="/images/alerticon.jpg" />&nbsp;Disconnect magnetU from computer, connect it again and confirm the magnetU driver installation wizard screens.</div></div></div><iframe width="1" height="1" frameborder="0" id="activexvalidation" src="/include/libs/spcheckactivex.php"></iframe>');
		$(".step1").html('<div class="in"><span class="num">01.</span><br />Install magnetU app</div>');
		$(".step2").html('<div class="in"><span class="num">02.</span><br />Define your social desires profiles</div>');
		$(".step3").html('<div class="in"><span class="num">03.</span><br />Link your magnetU<br />with your mobile phone</div>');			
		Cufon("#installationpackage .installation_panel .steps ul li");
		Cufon("#installationpackage .installation_panel .instructions");
		Cufon("#installationpackage .installation_panel .found");
		Cufon("#installationpackage .installation_panel .alert");
		$(".step1").css("background-image","url(../../images/mu_install_step_done.png)");
		$(".step2").css("background-image","url(../../images/mu_install_step_current.png)");
		$("#installationpackage").css("display","block");		
		$(".close").click(function(){
			$(".detectdevice").html('Access your Social desires');
			Cufon('.detectdevice');
			Cufon('.loginbutton');
			$("#shadow").toggle();
			$("#installationpackage").append('');
			$("#installationpackage").css("display","none");
		});
		
    }
    $(function() {
		$('#fb').hover(
		  function() { $(this).attr('src', 'images/icons/facebookho.png'); },
		  function() { $(this).attr('src', 'images/icons/facebook.png'); }
		)
		$('#t').hover(
		  function() { $(this).attr('src', 'images/icons/twitterho.png'); },
		  function() { $(this).attr('src', 'images/icons/twitter.png'); }
		)
		$('#rss').hover(
		  function() { $(this).attr('src', 'images/icons/rssho.png'); },
		  function() { $(this).attr('src', 'images/icons/rss.png'); }
		)
	});
	function noPlugin() {
		isPluginInstalled=false;
	}
    function removepopup() {
		detectMagnetUTimesCalled=0;
		clearInterval(detectMagnetUIntervalID);
		$("#magnetUsearch").css("display","none");
	}
	$(document).ready(function(){
		try {
			  if (pagename!=null) {
				  if (pagename=='wig') {
				  }
			  }
		} catch(e) {
			$(".detectdevice").html('<span class="aysd">Access your Social desires</span>');
			Cufon('.aysd');
		}		
		Cufon('#magnetUsearch .messagetitle');
		Cufon('#magnetUsearch .messagecontent');
		$(".detectdevice").click(function(){
			try {
			  if (pagename!=null) {
				if (pagename=='wig') {
					$("#magnetUsearch").css("display","block");
					
				} else {
					$(this).html('Searching magnetU');
				}
			  }
			} catch(e) {
				$("#magnetUsearch").css("display","block");
				if (document.MagnetUControl!=null) {
					recheck();				
				} else {
					$("#shadow").toggle();
					installation();
				}
			}
			Cufon('.loginbutton');
            $(".clickhere").html('Sign up / Login');
            Cufon('.clickhere');
            var browser = (navigator.appVersion.indexOf("MSIE")  != -1 );
            if (browser) {
            if (document.MagnetUControl != null) {
				isPluginInstalled=true;
			}
		}
		if (isPluginInstalled==false) {
			installation();
		} else {
			try {
			  if (pagename!=null) {
				if ((pagename=='hp')||(pagename=='wig')) {
				detectMagnetUTimesCalled = 0;
				detectMagnetU();
				detectMagnetUIntervalID = setInterval("detectMagnetU()", 1000);
				
				registerMagnetUControlEventHandler('OnGetMEPIdentifierDone', foundMagnetUIdentifier);
				}
			  }
			} catch(e) {
			}
		}
		});
	    $(".container").css('visibility', 'visible');
		$("#shadow").css("height", $(document).height()).hide();
    });
