// MAIN - sitewide functionality //

var newwin = null;
function do_payment_Popup(paytype, myform, windowname, subscriber)
{
        if(paytype == 'refill'){
                var c = parseInt(myform.CELLPHONE.value);
                var a = parseInt(myform.amount.value);
                if(c > 39999999 && (a > 99 && a < 100000)){
                        //document.getElementById('fielderror').style.display = 'none';
                        window.name="OCpaymentParent_refill";
                        if (! window.focus) return true;
                        newpaywin = window.open('', windowname, 'scrollbars,status,width=550,height=600');
                        myform.target=windowname;
                        newpaywin.focus();
                        return true;
                } else { //document.getElementById('fielderror').style.display = '';
                        return false;
                }
        }else if(paytype == 'ccreg'){
                document.ccreg_form.subscriber.value=subscriber;
                window.name="OCpaymentParent_ccreg";
                if (! window.focus) return true;
                newpaywin = window.open('', windowname, 'scrollbars,status,width=550,height=600');
                myform.target=windowname;
                newpaywin.focus();
                return true;
        }else if(paytype == 'regccreg'){
                document.regccreg_form.subscriber.value=subscriber;
                window.name="OCpaymentParent_regccreg";
                if (! window.focus) return true;
                newpaywin = window.open('', windowname, 'scrollbars,status,width=550,height=600');
                myform.target=windowname;
                newpaywin.focus();
                return true;
        }else if(paytype == 'advpayment'){
                var checkOK = "0123456789";
                var a = myform.amount.value;
                var allValid = true;
                var allNum = "";
                for (i = 0;  i < a.length;  i++)
                {
                        ch = a.charAt(i);
                        for (j = 0;  j < checkOK.length;  j++)
                        if (ch == checkOK.charAt(j)) break;
                        if (j == checkOK.length)
                        {
                                allValid = false;
                                break;
                        }
                        if (ch != ",") allNum += ch;
                }
                if (!allValid)
                {
                         //document.getElementById('fielderror').style.display = '';
                        return (false);
                }

                if(a >= 10  && a <= 10000){
                        //document.getElementById('fielderror').style.display = 'none';
                        window.name="OCpaymentParent_advpayment";
                        if (! window.focus) return true;
                        window.open('', windowname, 'scrollbars,status,width=550,height=600');
                        myform.target=windowname;
                        windowname.focus();
                        return true;
                        //return false;
                } else {
                        //document.getElementById('fielderror').style.display = '';
                        return false;
                }

        }
}


// Public vars
var isOpera = (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent));
var flashEnabled = FlashDetect.installed;

// alert("Browser is Opera: " + /Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent) + " - Flash is installed: " + FlashDetect.installed + " - Flash Disabled: " + isOpera);
$(document).ready(function() {

	console.log("Flash installed: " + FlashDetect.installed);
	// Hide focus borders
	$('a').attr({onfocus: "this.hideFocus=true"});
	
	// Checkboxes
	$('#mypages_leftmenu .mypages_radiobutton').checkBox();
	$('#receipt .mypages_radiobutton').checkBox();
	$('#content_wrapper .content_radiobutton, #content_wrapper .content_checkbox').checkBox();
	
	// Tables
	$('#main_row .standard th:first-child').addClass('left');
	$('#main_row .standard th:last-child').addClass('right');
	$('#main_row .standard td:first-child').addClass('left');
	$('#main_row .standard td:last-child').addClass('right');
	
	$('#main_row .standard tr:even td').addClass('even');
	$('#main_row .standard tr:odd td').addClass('odd');
	$('#main_row .standard tr:last-child td').removeClass('even');
	$('#main_row .standard tr:last-child td').removeClass('odd');
	$('#main_row .standard tr:last-child td').addClass('bottom');
	
	// fullpages Tables
	$('#main_row .fullpage th:first-child').addClass('left');
	$('#main_row .fullpage th:last-child').addClass('right');
	$('#main_row .fullpage td:first-child').addClass('left');
	$('#main_row .fullpage td:last-child').addClass('right');
	
	$('#main_row .fullpage tr:even td').addClass('even');
	$('#main_row .fullpage tr:odd td').addClass('odd');
	$('#main_row .fullpage tr:last-child td').removeClass('even');
	$('#main_row .fullpage tr:last-child td').removeClass('odd');
	$('#main_row .fullpage tr:last-child td').addClass('bottom');
   
	$("#content_wrapper .mizer").click(function () {
  	console.log($(this).hasClass('closed'));
		if ($(this).hasClass('closed')) {
			$("#campaign_container").slideDown("slow");
			$(this).removeClass('closed');
    } else {
			console.log("Is open, closes");
			$("#campaign_container").slideUp("slow");
			$(this).addClass('closed');
		}
  });

	$("#content_wrapper .mizer").hover(
		function () {
			$(this).addClass("over");
		},
		function () {
			$(this).removeClass("over");
		}
	);

	$("#content_wrapper .mizer").hover(
		function () {
			$(this).addClass("over");
		},
		function () {
			$(this).removeClass("over");
		}
	);

	// Accordion functionality
	
	// Reset icons
	$(".accordion dd:not(dd.open)").hide();

		// If user clicks
		$(".accordion a").click(function(){
			
			// Remove all enabled tags
			$(".accordion .toggler").removeClass("enabled");

			// If the item clicked is non-visible
			if ($(".accordion dd:not:visible").get()[0] !== $(this).parent().next().get()[0]) {
			
				// If IE // DO NOT CHANGE THE ORDER
				if($.browser.msie) { 
					$(this).parent().next().show();
					$("dd").hide();
					$(this).parent().find(".toggler").addClass("overEnabled");
					$("dd:visible").slideUp("slow");
					$(this).parent().next().slideDown("def");
					$(this).parent().find(".toggler").addClass("enabled");
					// alert("this is exploder!");

				// If other browser // DO NOT CHANGE THE ORDER
				} else { 
					$("dd").fadeTo("fast", 0);
					$(this).parent().find(".toggler").addClass("overEnabled");
					$("dd:visible").slideUp("slow");
					$(this).parent().next().slideDown("def");
					$(this).parent().next().fadeTo("fast", 1);
					$(this).parent().find(".toggler").addClass("enabled");
				}
			}

			// If the item is the one currently active
			else if ($("dd:visible").get()[0] !== $(this).get()[0]) {
				$("dd:visible").slideUp("fast");
				$(this).parent().next().fadeTo("def", 1);
				$(this).parent().find(".toggler").addClass("overEnabled");
			}

			// If the item clicked is visible
			else if ($("dd:visible").get()[0] !== $(this).parent().next().get()[0]) {
				$(this).parent().find(".toggler").addClass("overEnabled");
			}
		});

	$(".accordion a, .accordion .toggler").hover(
		function() {
			if ($(this).parent().find(".toggler").hasClass("enabled")) {
				$(this).parent().find(".toggler").addClass("overEnabled");
			}
		},
		function() {
			$(this).parent().find(".toggler").removeClass("overEnabled");
		});

	$(".accordion a, .accordion .toggler").hover(
		function() {
			$(this).parent().find(".toggler").addClass("over");
		},
		function () {
			$(this).parent().find(".toggler").removeClass("over");
		}
	);
	
	// Enable clicking on togglers
	$(".accordion .toggler").click(function() {
		$(this).next().click();
	});

	$("#driftsmelding_pluss").click(function(){
			$("#driftsmelding_hidden").css("display","inline");
//      $("#block").animate( { height:"200px" }, { queue:false, duration:300 } );
    });

	$("#driftsmelding_minus").click(function(){
			$("#driftsmelding_hidden").css("display","none");
//      $("#block").animate( { height:"0px" }, { queue:false, duration:300 } );
    });
		
	$("#leftmenu .contact_graphic").click(function(){
		window.location.replace("index.php?content=article");
	})
	
	// Show body
	// alert("Trying to hide preloader")
	$('#preloader').hide();
	// alert("Trying to show wrapper")
	// $('#wrapper').removeClass('invisible');

	// Load flash content
	// Webshop flash (if applicable)
	if ($("#oc_webshop").length > 0) {
		swfobject.embedSWF(siteRoot + "flash/webshop/684x150_webshop.swf", "campaign_container", "684", "150", "9.0", "flash/expressinstall.swf");
	}

	// Webshop flash (if applicable)
	if ($("#oc_webshop").length > 0) {
		swfobject.embedSWF(siteRoot + "flash/webshop/684x150_webshop.swf", "campaign_container", "684", "150", "9.0", "flash/expressinstall.swf");
	} else {
	
//  swfobject.embedSWF(siteRoot + "flash/mypages/684x150_mobilsvar.swf", "campaign_container", "684", "150", "9.0", "flash/expressinstall.swf");

	swfobject.embedSWF("/flash/mypages/684x150_mobilsvar.swf", "campaign_container", "684", "150", "9.0", "flash/expressinstall.swf");


	}
});
