var pendingRequest;

jQuery.noConflict();
jQuery(document).ready(function(){

    jQuery(".ul_container_products").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    jQuery("ul.topnav li .to_expand").hover(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
	if (jQuery.browser.safari){
		jQuery(this).parent().find(".ul_container_products").show(10);
	}else{
	        jQuery(this).parent().find(".ul_container_products").slideDown().show(10); //Drop down the subnav on click
	}

        jQuery(this).parent().hover(function() {
            }, function(){
                jQuery(this).parent().find(".ul_container_products").slideUp(10); //When the mouse hovers out of the subnav, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){  //On Hover Out
        jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

});
jQuery(document).ready(function(){

    jQuery(".ul_container_support").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    jQuery("ul.topnav li .to_expand").hover(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
	if (jQuery.browser.safari){
	        jQuery(this).parent().find(".ul_container_support").show(10); //Drop down the subnav on click
	}else{
		jQuery(this).parent().find(".ul_container_support").slideDown().show(10); //Drop down the subnav on click
	}

        jQuery(this).parent().hover(function() {
            }, function(){
                jQuery(this).parent().find(".ul_container_support").slideUp(10); //When the mouse hovers out of the subnav, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){  //On Hover Out
        jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

});
jQuery(document).ready(function(){

    jQuery(".ul_container_wheretobuy").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    jQuery("ul.topnav li .to_expand").hover(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
	if (jQuery.browser.safari){
		jQuery(this).parent().find(".ul_container_wheretobuy").show(10);
	}else{
	        jQuery(this).parent().find(".ul_container_wheretobuy").slideDown().show(10); //Drop down the subnav on click
	}

        jQuery(this).parent().hover(function() {
            }, function(){
                jQuery(this).parent().find(".ul_container_wheretobuy").slideUp(10); //When the mouse hovers out of the subnav, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){  //On Hover Out
        jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

});
jQuery(document).ready(function(){

    jQuery(".ul_container_becomeadealer").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    jQuery(".ul_container_news").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
    
    jQuery("ul.topnav li .to_expand").hover(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        jQuery(this).parent().find(".ul_container_becomeadealer").slideDown().show(10); //Drop down the subnav on click
		jQuery(this).parent().find(".ul_container_news").slideDown().show(10); //Drop down the subnav on click
        
        jQuery(this).parent().hover(function() {
            }, function(){
                jQuery(this).parent().find(".ul_container_becomeadealer").slideUp(10); //When the mouse hovers out of the subnav, move it back up
                jQuery(this).parent().find(".ul_container_news").slideUp(10); //When the mouse hovers out of the subnav, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){  //On Hover Out
        jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

});
jQuery(document).ready(function(){

    jQuery(".ul_container_appstore").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    jQuery("ul.topnav li .to_expand").hover(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
	if (jQuery.browser.safari){
	        jQuery(this).parent().find(".ul_container_appstore").show(); //Drop down the subnav on click
	}else{
		jQuery(this).parent().find(".ul_container_appstore").slideDown(10).show();
	}

        jQuery(this).parent().hover(function() {
            }, function(){
                jQuery(this).parent().find(".ul_container_appstore").slideUp(10); //When the mouse hovers out of the subnav, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){  //On Hover Out
        jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

});

function smartSwitchShowTab(TabClass){
    if(TabClass == "tab-features"){
        jQuery("#corner").removeClass().addClass("top-left-nocorner");
    }  else {
        jQuery("#corner").removeClass().addClass("top-left");
    }
    jQuery(".tab-features-selected").hide();
    jQuery(".tab-afeatures-selected").hide();
    jQuery(".tab-specs-selected").hide();
    jQuery(".tab-images-selected").hide();
    jQuery(".tab-faqs-selected").hide();

    jQuery(".tab-features-normal").show();
    jQuery(".tab-afeatures-normal").show();
    jQuery(".tab-specs-normal").show();
    jQuery(".tab-images-normal").show();
    jQuery(".tab-faqs-normal").show();

    jQuery(".tab-features-content").hide();
    jQuery(".tab-afeatures-content").hide();
    jQuery(".tab-specs-content").hide();
    jQuery(".tab-images-content").hide();
    jQuery(".tab-faqs-content").hide();

    jQuery("."+TabClass+"-normal").hide();
    jQuery("."+TabClass+"-selected").show();
    jQuery("."+TabClass+"-content").show();
}
function productsShowTab(TabID){
    jQuery("#tab_family_selected").hide();
    jQuery("#tab_money_selected").hide();
    jQuery("#tab_control_selected").hide();
    jQuery("#tab_theather_selected").hide();

    jQuery("#tab_family_normal").show();
    jQuery("#tab_money_normal").show();
    jQuery("#tab_control_normal").show();
    jQuery("#tab_theather_normal").show();

    jQuery("#"+TabID+"_selected").show();
    jQuery("#"+TabID+"_normal").hide();

    jQuery("#tab_family_content").hide();
    jQuery("#tab_money_content").hide();
    jQuery("#tab_control_content").hide();
    jQuery("#tab_theather_content").hide();
    jQuery("#"+TabID+"_content").show();
}
jQuery(document).ready(function(){

    jQuery("ul.contact-subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    jQuery("ul.contact-nav li .country-list-expand").hover(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        jQuery(this).parent().find("ul.contact-subnav").slideDown(10).show(); //Drop down the subnav on click

        jQuery(this).parent().hover(function() {
        }, function(){
            jQuery(this).parent().find("ul.contact-subnav").slideUp(10); //When the mouse hovers out of the subnav, move it back up
        });

        //Following events are applied to the trigger (Hover events for the trigger)
        }).hover(function() {
            jQuery(this).addClass("subhover"); //On hover over, add class "subhover"
        }, function(){  //On Hover Out
            jQuery(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });
    jQuery("ul.contact-nav ul.contact-subnav li").click(function() {
        jQuery("#country-name").val(jQuery(this).html());
        jQuery("ul.contact-nav .country-list-expand").html(jQuery(this).html());
        jQuery("ul.contact-nav .contact-subnav").hide();
    });
});

jQuery(document).ready(function(){
//jQuery(function(){
    overflowFunction()
});

 jQuery.noConflict()(window).resize(function(){overflowFunction()})

function overflowFunction(){

    if(jQuery('#ref_point_div').position() && jQuery('#ref_point_div').position().top < jQuery(window).height()){
        jQuery("html").css({overflow:"hidden"});
        jQuery(".smartbox-middle-center").css({height:jQuery(window).height()});
    }else{
        jQuery("html").css({overflow:"visible"});
        jQuery(".smartbox-middle-center").css({height:""});
    }
}

function detect_new_vera(){
	pendingRequest = new Ajax.Request("detect_vera.php",
	{
		method:"get",
		onSuccess: function(transport){
			try{
				var json = transport.responseText.evalJSON(); 
				if(json.length>0){
					clearTimeout(redirectTimer);
					if(json.length==1){
						check_ip(json[0].FK_AccessPoint,json[0].InternalIP);
					}else{
						show_ap_picker(json);
					}
				}else{
					clearTimeout(redirectTimer);
					force_redirect(3);
				}
			}catch(e){
				_console('error: '+e);
				clearTimeout(redirectTimer);
				force_redirect(4);
			}
			/*
			if(transport.responseText.substr(0,2)=="OK"){
				var data=transport.responseText.split(",");
				if(data.length>=3){
					clearTimeout(redirectTimer);
					if(data.length==3){
						check_ip(data[1],data[2]);
					}else{
						show_ap_picker(data);
					}
				}else{
					setTimeout("detect_new_vera()",2000);
				}
			}else if(transport.responseText=='NO_RECORDS'){
				clearTimeout(redirectTimer);
				force_redirect(3);
			}else{
				setTimeout("detect_new_vera()",2000);
			}
			*/
		},
		onFailure: function(transport){
			setTimeout("detect_new_vera()",2000);
		}
	});
}

function check_ip(PK_AccessPoint,ipAddress){
	DCE_Router_AP=0;
	_console("DCE_Router_AP=0")
	document.getElementById("scan_info").innerHTML="<p class=\"subtitlu\">Please wait ...</p>Attempting to connect with Vera #"+PK_AccessPoint+".<br><br><input type=\"button\" value=\"Cancel\" onClick=\"self.location=\'scanning_vera2.php\'\"><br>";
	document.getElementById("ip").value=ipAddress;
	connectTimer=setTimeout("force_redirect(2)", 5000);

	try{
		//var obj=new JSONscriptRequest("http://"+ipAddress+"/cgi-bin/cmh/online_check.sh?tmp=1");
		var obj = new JSONScriptRequest();
  		obj.open('GET', "http://"+ipAddress+"/cgi-bin/cmh/online_check.sh?tmp=1");
		obj.onreadystatechange = function() {
			if (obj.readyState == 4) {
   				obj.buildScriptTag(); // Build the script tag
				obj.addScriptTag(); // Execute (add) the script tag
		   }else{
				DCE_Router_AP=PK_AccessPoint;
				var DCEobj = new JSONScriptRequest();
		  		DCEobj.open('GET', "http://"+ipAddress+"/cmh/online.html?tmp=1");
				DCEobj.onreadystatechange = function() {
					if (DCEobj.readyState == 4) {
		   				DCEobj.buildScriptTag(); // Build the script tag
						DCEobj.addScriptTag(); // Execute (add) the script tag
				   }				
				}
				DCEobj.send(null);	
		   }
		};
  		obj.send(null);		
		

	}catch(e){
		_console(e);
	}
}

function test(data){
	var PK_AccessPoint=(DCE_Router_AP==0)?parseInt(data.PK_AccessPoint):DCE_Router_AP;

	if(PK_AccessPoint!=0 && PK_AccessPoint!=NaN){
		var script = (typeof(redirectScript)!='undefined')?redirectScript:'mios_vera2.php';
		self.location = script+"?PK_AccessPoint="+PK_AccessPoint+"&ip="+document.getElementById("ip").value;
	}else{
		self.location="cant_connect.php?error=2";
	}
}

function force_redirect(flag){
	_console('redirect flag: '+flag);
	try{
		pendingRequest.transport.abort();
	}catch(e){
		_console('force_redirect error: '+e);
	}

	self.location="cant_connect.php?error="+flag;
}

function show_ap_picker(json){
	_console(Object.toJSON(json))
	//multiple veras, show picker
	var html='<p class="subtitlu">I found more than 1 Vera on your network. Which one do you want to setup?</p><br>';
	html+='<table class="regular" cellpadding="3" cellspacing="1">';
	for(var i=0;i<json.length;i++){
		html+='<tr>'+
		'	<td>#'+json[i].FK_AccessPoint+'</td>'+
		'	<td>'+json[i].InternalIP+'</td>'+
		'	<td><input type="button" class="button" onClick="check_ip('+json[i].FK_AccessPoint+',\''+json[i].InternalIP+'\')" value="Setup"></td>'+
		'</tr>';
	}
	html+='</table>';
	
	document.getElementById("scan_info").innerHTML=html;
}

function go_to_upgrade(PK_AccessPoint,oldVersion,ip,upgrade_file,upgradeParm,siteFrom){
	self.location='upgrade.php?ap='+PK_AccessPoint+'&version='+oldVersion+'&redir='+encodeURIComponent('http://'+ip+'/cgi-bin/cmh/upgrade_step2.sh?'+upgradeParm+'='+upgrade_file+'&AutomaticReturn='+siteFrom);
}

function _console(str){
	try{
		console.log(str);
	}catch(e){
		
	}	
}

function newsShow(TabClass){
    if(TabClass == "tab-news"){
        jQuery("#corner").removeClass().addClass("top-left-nocorner");
    }  else {
        jQuery("#corner").removeClass().addClass("top-left");
    }
    jQuery(".tab-news-selected").hide();
    jQuery(".tab-reviews-selected").hide();
	jQuery(".tab-videos-selected").hide();
    
    jQuery(".tab-news-normal").show();
    jQuery(".tab-reviews-normal").show();
    jQuery(".tab-videos-normal").show();

    jQuery(".tab-news-content").hide();
    jQuery(".tab-reviews-content").hide();
    jQuery(".tab-videos-content").hide();

    jQuery("."+TabClass+"-normal").hide();
    jQuery("."+TabClass+"-selected").show();
    jQuery("."+TabClass+"-content").show();
}

