var mainImageOuter = ".mainImageOuter"
var pagination = ".pagination"
var productmenu = ".product-menu";
var productsubmenu = ".product-submenu";
jQuery(document).ready(function() {

    jQuery("#login").remove();
    jQuery("#pop-content").remove();
    jQuery("#pop-cover1").remove();

    jQuery("#content div.product-submenu").each(function(i) {
        jQuery(this).addClass("product-submenu"+ (i+1));
    });	

    jQuery("div.plans a.select").click(function() {
        jQuery("div.plans a.active").each(function() {
            jQuery(this).parents("tr").toggleClass("selected");
            jQuery(this).toggleClass("active")
            jQuery(this).removeAttr("title")
            jQuery(this).attr("href")
        });
        var title = jQuery(this).parents("tr").children().eq(0).text();
        if (jQuery(this).parents("tr")){
            jQuery(this).parents("tr").toggleClass("selected");
            jQuery(this).toggleClass("active");
            jQuery(this).attr("title", title);
        }
        return false;
        
    });
    
    // Plan selector for vertical plan selector
    jQuery('div.plans-vert a.select').click(function(){
      jQuery("div.plans-vert a.active").each(function() {
          jQuery(this).parents("td").toggleClass("selected");
          jQuery(this).parents("td").toggleClass("active");
          jQuery(this).toggleClass("active")
          jQuery(this).removeAttr("title")
          jQuery(this).attr("href")
      });
      var col = jQuery(this).parent().children().index(jQuery(this));
      var row = jQuery(this).parent().parent().children().index(jQuery(this).parent());
      var plan = jQuery(this).parents("table").find("tr").eq(0).children().eq(0).text();
      var title = plan + " " + jQuery(this).parents("table").find("tr").eq(0).children().eq(row).text();
      if (jQuery(this).parents("tr")){
          jQuery(this).parents("td").toggleClass("selected");
          jQuery(this).toggleClass("active");
          jQuery(this).attr("title", title);
      }
      return false;
    });

    jQuery("ul.phones li a.select").click(function() {
        jQuery("ul.phones li a.active").each(function() {
            jQuery(this).removeClass("active");
            jQuery(this).addClass("select");
            jQuery(this).removeAttr("title")
        });
        var title = jQuery(this).parent().children("span.title").text();
        if (jQuery(this).parents("li")){
            jQuery(this).removeClass("select");
            jQuery(this).addClass("active");
            jQuery(this).attr("title", title);
        }
        return false;

    });
	
    //get/set cookie on sign up form
    jQuery("#product-form .hidden input:eq(0)").val(jQuery.cookie("Prod1"));
    jQuery("#product-form .hidden input:eq(1)").val(jQuery.cookie("Prod2"));
    jQuery("#product-form .hidden input:eq(2)").val(jQuery.cookie("Prod3"));
    
	// cookie for landline/internet //
	// sets cookie on click event //
	$("ul.product-menu a").click(function() {
	  var a = jQuery(this).text(); //gets text from element
      if(jQuery.cookie("Prod1")){ // reset cookie to null
        jQuery.cookie("Prod1", null, { path: '/', domain: 'sctelco.com.au' });
      };
	  if(a){ // write value to cookie if it's defined
	    jQuery.cookie("Prod1", a, { path: '/', domain: 'sctelco.com.au' });
	  } else { // write "Undefined" value for debgging
	    jQuery.cookie("Prod1", "'n/a", { path: '/', domain: 'sctelco.com.au' });
	  };
	});
	
	// cookie for selected phone/internet plan //
	// sets cookie on click event //
	jQuery("a.select").click(function() {
	  var a = jQuery(this).attr("title"); // gets title of element
      if(jQuery.cookie("Prod3")){ // reset cookie to null
        jQuery.cookie("Prod3", null, { path: '/', domain: 'sctelco.com.au' });
      };
	  if(a){ // write value to cookie if it's defined
	    jQuery.cookie("Prod3", a, { path: '/', domain: 'sctelco.com.au' });
	  } else { // write "Undefined" value for debgging
	    jQuery.cookie("Prod3", "undefined", { path: '/', domain: 'sctelco.com.au' });
	  };
	});

    // Below #sign-up click function leave in for integrety, also adds default action for users who don't chose an option //
    // look for XXX to mark sections to leave in //
    jQuery("#sign-up").click(function() {
        /* setting cookies to be passed onto sign up form */

        /* cookie for landline/internet */
        // XXX because onload li.action is defined //
        var a = jQuery("li.active a").text(); //gets text from element
        if(a){ //checks if variable was created
		  if(jQuery.cookie("Prod1")){ // reset cookies to be sure nothing gets left over from a previous session
		    jQuery.cookie("Prod1", null, { path: '/', domain: 'sctelco.com.au' });
		  };
          jQuery.cookie("Prod1", a, { path: '/', domain: 'sctelco.com.au' }); //writes cookie
        };

        /* cookie for plan headings: landline, mobile and internet */
        
        // BELOW FOR MOBILES HEADING || Commented out because not needed, landline heading retrieved from li.active a text //
        /* var b = jQuery("div.product-submenu:visible h2").text(); */
        // XXX for mobile plan with mobile attached //
        var b = jQuery("div.overview-info h2").text(); //this for mobiles
        if(b){
          if(jQuery.cookie("Prod2")){
            jQuery.cookie("Prod2", null, { path: '/', domain: 'sctelco.com.au' });
          };
          jQuery.cookie("Prod2", b, { path: '/', domain: 'sctelco.com.au' });
        };
        
    });

    jQuery(productmenu).find("li:first").toggleClass("active"); // default first element to active
    jQuery(productmenu).find("a").each(function() {
        var selMenu;
        jQuery(this).click(function() {
            selMenu = jQuery(this).text();
            jQuery(this).parent().addClass("active");

            var detailshow = jQuery(this).parent().attr("id");
            jQuery("." + detailshow).show();

            jQuery(".view").hide();

            //select default first
            jQuery("." + detailshow).find("ul").find("li").removeClass("active");


            var detail = jQuery("." + detailshow).find("ul").children(":first").attr("id");

            //condition for handsets
            if (detail == "") {
                detail = jQuery("." + detailshow).show();
            }
            else {
                jQuery("#" + detail).addClass("active");
                jQuery("." + detail).show();
            }

            jQuery(productmenu).find("a").each(function() {
                if (jQuery(this).text() != selMenu) {
                    jQuery(this).parent().removeClass("active");

                    var detailhide = jQuery(this).parent().attr("id");
                    jQuery("." + detailhide).hide();
                }
            });
            return false;
        });
    });

    jQuery(productsubmenu).find("a").each(function() {
        var selMenu;
        jQuery(this).click(function() {
            selMenu = jQuery(this).text();
            jQuery(this).parent().addClass("active");
            var detailshow = jQuery(this).parent().attr("id");
            jQuery("." + detailshow).show();

            jQuery(productsubmenu).find("a").each(function() {
                if (jQuery(this).text() != selMenu) {
                    jQuery(this).parent().removeClass("active");
                    var detailhide = jQuery(this).parent().attr("id");
                    if (jQuery("." + detailhide) != null && detailhide != "") {
                        jQuery("." + detailhide).hide();
                    }
                }
            });
            //respects links with a URL
            return true;
        });
    });


    //call image rotation
    var mainOuter = jQuery(mainImageOuter);
    if (mainOuter != null) {
        bindDefaultEvents();
        setActiveImage();
        //initilize timer
        if (jQuery(mainImageOuter).length > 0) {
            jQuery(mainImageOuter).cycle({
                fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                startingSlide: 0
            });
        }
    }

    //login popup
    addLoginForm();
    jQuery(".my-account").click(function() {
        var offset = jQuery(this).offset();
        var left = offset.left-30;
        var top = offset.top-10;
        jQuery("#login").css("left", left + "px");
        jQuery("#login").css("top", top + "px");
        
        jQuery("#login").toggleClass("open");
        jQuery("#pop-cover1").toggleClass("show");
        return false;
    });


    //webmail popup
    addWebMailForm();
    jQuery(".my-webmail").click(function() {
    
        //Get the screen height and width
        var winH = $(document).height();
        var winW = $(window).width();  

        //Set the popup window to center
        $("#phone-pop").css('top', winH / 2 - ($("#phone-pop").height() / 2)-200);
        $("#phone-pop").css('left', winW / 2 - $("#phone-pop").width() / 2);  

        jQuery("#phone-pop").toggleClass("show");
        jQuery("#pop-cover2").toggleClass("show");
        return false;
    });

})

function bindDefaultEvents() {
    jQuery(pagination).find("a").each(function() {
        //mouseover
        jQuery(this).mouseover(function(e) {
            activeTab = jQuery(this).parent().removeClass("inactive").addClass("active").attr("id");

            jQuery(pagination).find("a").each(function() {
                var tabId = jQuery(this).parent().attr("id");
                if (activeTab != tabId) {
                    jQuery(this).parent().removeClass("active").addClass("inactive");
                }
            });

            //get active image index
            var index = activeTab.replace('page', '');
            setActiveImage();

            //initilize timer with current selected slide
            jQuery(mainImageOuter).cycle({
                fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                startingSlide: index - 1
            });
            //pause timer
            jQuery(mainImageOuter).cycle('pause');
        });
    });
}


function setActivePagination(index, totalCount) {
    totalCount = totalCount + 1;
    index = index + 2;
    if (index == totalCount) {
        index = 1;
    }
    var $activeCommand = jQuery(pagination).find("div[id$='page" + index + "']");

    if ($activeCommand != null) {
        var activeTab = $activeCommand.removeClass("inactive").addClass("active").attr("id");
        jQuery(pagination).find("a").each(function() {
            var tabId = jQuery(this).parent().attr("id");
            if (activeTab != tabId) {
                jQuery(this).parent().removeClass("active").addClass("inactive");
            }
        });
        setActiveImage();
    }
}


function setActiveImage() {
    //mousehover and out event on command
    jQuery(pagination).find("a").each(function() {
        jQuery(this).parent().parent().find(".active").mouseover(function() {
            jQuery(mainImageOuter).cycle('pause');
        });
        jQuery(this).parent().parent().find(".active").mouseout(function() {
            var currentCmdId = jQuery(this).parent().parent().find(".active").attr("id");
            jQuery(mainImageOuter).cycle('resume', false);
        });
    });
}

function addLoginForm() {
    var url = "popup/login.html";
    $.get(url, function(data) {
    if (data != null) {
            $("body").append(data);
        }
        else {
            alert("Some problem occurred please try again.");
        }
    });
}

function addWebMailForm() {
    var url = "popup/webmail.html";
    $.get(url, function(data) {
        if (data != null) {
            $("body").append(data);
        }
        else {
            alert("Some problem occurred please try again.");
        }
    });
}

    jQuery(document).ready(function() {

        jQuery(".close").click(function() {
        jQuery("#login").toggleClass("open");
        jQuery("#pop-cover1").toggleClass("show");
            return false;
       });
    });

/* replaced by pie.htc
function initCorners() {
   if (jQuery.browser.msie) {
       jQuery("#bread").corner("bottom 5px");
       jQuery("#test").corner("bottom");
       jQuery("#bar").corner("top");
       jQuery(".tags a").corner("round");
       jQuery(".phones .select").corner("round 5px");
       jQuery(".phones .more-info").corner("round 5px");
       jQuery(".overview-info .show-plan").corner("round 5px");
       jQuery("#quad h3").corner("round");
   }   
}
*/

