var GLOBAL_SITE_URL = 'http://paycheckr/';
//var GLOBAL_SITE_URL = 'http://paycheckr.visual-craft.com/';
function UpdateIcons() {
        var Dmain = jQuery('div#prev-buttons');
	var ParamStr = '';
	var FullString = '';
	var Incer = 0;
	Dmain.html('Loading...');
	
	jQuery('tr#ButtonCHeckBoxes').find('input[type=checkbox]').each(function(i) {
		if(jQuery(this).attr('checked')) {
			ParamStr += '[button]' + (i+1) + '$#';
			Incer++;
		}
	});
	FullString = GLOBAL_SITE_URL + 'pc-insert.php?design{d}1{a}count{d}' + Incer + '{a}params{d}' + ParamStr;
	jQuery.ajax({
		url: GLOBAL_SITE_URL + "preview.php",
		type: "POST",
		data: "a_params=" + FullString,
		success: function(data) {
			Dmain.html(data);
			
			if(GLOBAL_STEP == 'get-paycheckr-now-step-1') {
				var a_top = 12;var a_left = 2;
				if(jQuery.browser.webkit) {a_left = 2;a_top = 12;}
			}
			if(GLOBAL_STEP == 'get-paycheckr-now-step-2') {
				var a_top = 12;var a_left = 2;
				if(jQuery.browser.webkit) {a_left = 200;a_top = 12;}
			}
			
			jQuery('div#paycheckr-button-menu-999').css({'position':'absolute', 'top': a_top+'px', 'left': a_left+'px'});
			jQuery('span#paycheckr-button-999').mouseover(function() {
				jQuery('div#paycheckr-button-menu-999').css('display', 'block');
			});
			jQuery('span#paycheckr-button-999').mouseout(function() {
				jQuery('div#paycheckr-button-menu-999').css('display', 'none');
			});
		}
	});
}

function UpdateButtonSkin(radio) {
	var Skin = radio.attr('value');
	var bSkinImg = jQuery('div#button-skin-change span img:first');
	if(Skin == '1') {bSkinImg.attr('src', GLOBAL_SITE_URL + 'images/design/white1.gif');return;}
	if(Skin == '2') {bSkinImg.attr('src', GLOBAL_SITE_URL + 'images/design/white2.gif');return;}
	if(Skin == '4') {bSkinImg.attr('src', GLOBAL_SITE_URL + 'images/design/black1.gif');return;}
	if(Skin == '5') {bSkinImg.attr('src', GLOBAL_SITE_URL + 'images/design/black2.gif');return;}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function CheckIfAnyChecked() {
	for(i=1; i<=8; i++) {
		if(jQuery('input[name=create_buttons_check_' + i + ']').attr('checked')) {
			return true;
		}
	}
	return false;
}

function getCheckedCount() {
	var count = 0;
	for(i=1; i<=8; i++) {
		if(jQuery('input[name=create_buttons_check_' + i + ']').attr('checked')) {
			count++;
		}
	}
	return count;
}

jQuery(document).ready(function() {
	jQuery('#ButtonCHeckBoxes').find('input[type=checkbox]').each(function() {
		if(jQuery(this).attr('checked')) {
			jQuery(this).parent().parent().find('div.input-field').css('display', 'block').find('input').focus();
		}
	});

//	jQuery('div.one-button').each(function() {
//		var Div = jQuery(this);
//
//		Div.find('label').css('cursor', 'pointer');
//
//		Div.find('input[type=checkbox]').each(function() {
//			jQuery(this).click(function() {
//				if(jQuery(this).attr('checked')) {
//					//Show
//					Div.find('div.input-field').css('display', 'block').find('input').focus();
//				} else {
//					//Hide
//					Div.find('div.input-field').css('display', 'none');
//				}
//			});
//		});
//	});
	

	//jQuery('input[name=create_buttons_design]').click(function() {UpdateButtonSkin(jQuery(this));});

    jQuery(".create_button").click(function(){
      if(jQuery(this).is(":checked")){
        var param = "add";
        var id_value = jQuery(this).attr("id").split("_");
        var menu_btn_id = parseInt(id_value[2]);
        var simple_info = jQuery("#url_info_"+menu_btn_id).attr("value");
        var menu_url = jQuery("#menu_url_"+menu_btn_id).attr("value");
        var num = parseInt(jQuery("#submenu_amount_"+menu_btn_id).attr("value")) + 1;
        jQuery("#submenu_amount_"+menu_btn_id).attr("value", num);
        var html = "";
        html += "<div id='submenu_"+menu_btn_id+"_"+num+"' class='submenu_item'>";
        html += "<span class='simple_info simple_info_1'><img src='/images/url_link.jpg' width='12' border='0' alt='' title=''></span>";
        html += "<div class='simple_info_hint simple_info_hint_1 hint_div'><div style='padding: 4px;'>"+simple_info+"</div></div>";
        html += "<input type='text' name='buttons_"+menu_btn_id+"_text["+num+"]' class='menu_url_1' id='buttons_"+menu_btn_id+"_text_"+num+"' value='"+menu_url+"' />&nbsp;&nbsp;&nbsp;";
        html += "</div>";

        jQuery("#input_area_" + menu_btn_id).html(html);
        jQuery("#input_area_"+menu_btn_id).show();

    } else {
        var param = "del";

        var id_value = jQuery(this).attr("id").split("_");
        var menu_btn_id = parseInt(id_value[2]);        
        jQuery("#submenu_amount_"+menu_btn_id).attr("value", 0);
        jQuery("#input_area_" + menu_btn_id).html("");
        jQuery("#input_area_"+menu_btn_id).hide();
      }
      var param_id = jQuery(this).attr("name").substr(21);
      jQuery.get('/preview.php', {act : param, id : param_id}, function(data){
        jQuery("#prev-buttons").html(data);
      });
    });

    jQuery(".create_buttons_design").click(function(){
      var img = jQuery(this).attr("value");
      jQuery.get('/preview.php', {main_img : img}, function(data){
        jQuery("#prev-buttons").html(data);
      });
    });

    if(jQuery("#prev-buttons").length){
      jQuery("#prev-buttons").html("Loading ...");
      jQuery.get('/preview.php', {}, function(data){
        jQuery("#prev-buttons").html(data);
      });
    }

    jQuery(".analitic_view_code").click(function(){
      var txt_class = jQuery(this).attr("id");
      var code = jQuery("."+ txt_class);
      if(code.css("display") == "none"){
        code.show();
        jQuery(this).html("Hide code");
      } else {
        code.hide();
        jQuery(this).html("View code");
      }
    });

    jQuery(".adv").hover(function(){
      jQuery(this).next("div.adv_hint").show();
    }, function(){
      jQuery(this).next("div.adv_hint").hide();
    });

    jQuery(".simple_info").live("mouseover",function(){      
      jQuery(this).next("div.simple_info_hint").show();
    });

    jQuery(".simple_info").live("mouseout",function(){
      if(jQuery.browser.msie){
        jQuery(this).css("left", "-2px");
      }
      jQuery(this).next("div.simple_info_hint").hide();
    });

    jQuery(".simple_info_hint").live("mouseover",function(){
      jQuery(this).show();
    });

    jQuery(".simple_info_hint").live("mouseout",function(){
      jQuery(this).hide();
    });
    


    jQuery(".adv").click(function(){
      //var menu_btn_id = jQuery(this).parent("div").parent("div.one-button").children(".menu_button_id").attr("value");
      var menu_btn_id = jQuery(this).parent("td").parent("tr").parent("tbody").parent("table").parent("div.one-button").children(".menu_button_id").attr("value");
      var simple_info = jQuery("#url_info_"+menu_btn_id).attr("value");
      var menu_url = jQuery("#menu_url_"+menu_btn_id).attr("value");
      if(jQuery("#submenu_" + menu_btn_id + "_1").length && jQuery("#submenu_" + menu_btn_id + "_1").children(".add_submenu").length){
        return false;
      }
      if(!jQuery("#input_area_" + menu_btn_id).length || jQuery("#input_area_" + menu_btn_id).css("display") == "none"){
        var add = 1;
      } else {
        var add = 0;
      }
      jQuery("#submenu_amount_"+menu_btn_id).attr("value", "0");
      var num = parseInt(jQuery("#submenu_amount_"+menu_btn_id).attr("value")) + 1;
      jQuery("#submenu_amount_"+menu_btn_id).attr("value", num);
      var add_text = jQuery("#add_text").attr("value");
      var delete_text = jQuery("#delete_text").attr("value");
      var button_title_text = jQuery("#button_title_text").attr("value");
      var html = "";
      html += "<div id='submenu_"+menu_btn_id+"_"+num+"' class='submenu_item'>";
      html += "<input type='text' name='buttons_"+menu_btn_id+"_title["+num+"]' id='buttons_"+menu_btn_id+"_title_"+num+"' value='"+button_title_text+"' class='menu_title' maxlength='27'><br>";
      html += "<span class='simple_info'><img src='/images/url_link.jpg' width='12' border='0' alt='' title=''></span>&nbsp;";
      html += "<div class='simple_info_hint hint_div'><div style='padding: 4px;'>"+simple_info+"</div></div>";
      html += "<input type='text' name='buttons_"+menu_btn_id+"_text["+num+"]' class='menu_url' id='buttons_"+menu_btn_id+"_text_"+num+"' value='"+menu_url+"' />&nbsp;";
      html += "<span class='add_submenu' id='add_"+menu_btn_id+"_"+num+"'><img src='/images/add.jpg' border='0' width='22' alt='"+add_text+"' title='"+add_text+"'></span>";
      html += "<span class='del_submenu' id='del_"+menu_btn_id+"_"+num+"'><img src='/images/del.jpg' border='0' width='22' alt='"+delete_text+"' title='"+delete_text+"'></span>";
      html += "</div>";

      jQuery("#input_area_" + menu_btn_id).html(html);


      jQuery("#create_buttons_"+menu_btn_id).attr("checked", "checked");
      jQuery("#input_area_"+menu_btn_id).show();

      if(add == 1){
        jQuery.get('/preview.php', {act : "add", id : menu_btn_id, num : 1}, function(data){
          jQuery("#prev-buttons").html(data);
        });
      }
    });

    jQuery(".add_submenu").live("click", function(){
      var id_value = jQuery(this).attr("id").split("_");
      var menu_btn_id = id_value[1];
      var num = parseInt(id_value[2]) + 1;
      var prev = num-1;
      var simple_info = jQuery("#url_info_"+menu_btn_id).attr("value");
      var menu_url = jQuery("#menu_url_"+menu_btn_id).attr("value");
      var amount = parseInt(jQuery("#submenu_amount_"+menu_btn_id).attr("value"));
      jQuery("#submenu_amount_"+menu_btn_id).attr("value", amount+1);
      var add_text = jQuery("#add_text").attr("value");
      var delete_text = jQuery("#delete_text").attr("value");
      var button_title_text = jQuery("#button_title_text").attr("value");
      var html = "";
      html += "<div id='submenu_"+menu_btn_id+"_"+num+"' class='submenu_item'>";
      html += "<input type='text' name='buttons_"+menu_btn_id+"_title["+num+"]' id='buttons_"+menu_btn_id+"_title_"+num+"' value='"+button_title_text+"' class='menu_title' maxlength='27'>&nbsp;<br>";
      html += "<span class='simple_info'><img src='/images/url_link.jpg' width='12' border='0' alt='' title=''></span>&nbsp;";
      html += "<div class='simple_info_hint hint_div'><div style='padding: 4px;'>"+simple_info+"</div></div>";
      html += "<input type='text' name='buttons_"+menu_btn_id+"_text["+num+"]' class='menu_url' id='buttons_"+menu_btn_id+"_text_"+num+"' value='"+menu_url+"' />&nbsp;";
      html += "<span class='add_submenu' id='add_"+menu_btn_id+"_"+num+"'><img src='/images/add.jpg' border='0' width='22' alt='"+add_text+"' title='"+add_text+"'></span>";
      html += "<span class='del_submenu' id='del_"+menu_btn_id+"_"+num+"'><img src='/images/del.jpg' border='0' width='22' alt='"+delete_text+"' title='"+delete_text+"'></span>";
      html += "</div>";

      for(var i = num; i <= amount; i++){
        var new_i = i + 1;
        jQuery("#submenu_"+menu_btn_id+"_"+i).attr("id", "submenu_"+menu_btn_id+"_"+new_i);
        jQuery("#buttons_"+menu_btn_id+"_title_"+i).attr("name", "buttons_"+menu_btn_id+"_title["+new_i+"]");
        jQuery("#buttons_"+menu_btn_id+"_title_"+i).attr("id", "buttons_"+menu_btn_id+"_title_"+new_i);
        jQuery("#buttons_"+menu_btn_id+"_text_"+i).attr("name", "buttons_"+menu_btn_id+"_text["+new_i+"]");
        jQuery("#buttons_"+menu_btn_id+"_text_"+i).attr("id", "buttons_"+menu_btn_id+"_text_"+new_i);
        jQuery("#add_"+menu_btn_id+"_"+i).attr("id", "add_"+menu_btn_id+"_"+new_i);
        jQuery("#del_"+menu_btn_id+"_"+i).attr("id", "del_"+menu_btn_id+"_"+new_i);
      }

      jQuery("#submenu_"+menu_btn_id+"_"+prev).after(html);

      jQuery.get('/preview.php', {act : "add", id : menu_btn_id, num : num}, function(data){
        jQuery("#prev-buttons").html(data);
      });

      //jQuery("#create_buttons_"+menu_btn_id).attr("checked", "checked");
      //jQuery("#input_area_"+menu_btn_id).show();
    });

    jQuery(".del_submenu").live("click", function(){
      var id_value = jQuery(this).attr("id").split("_");
      var menu_btn_id = id_value[1];
      var num = parseInt(id_value[2]);
      var amount = parseInt(jQuery("#submenu_amount_"+menu_btn_id).attr("value"));
      jQuery("#submenu_"+menu_btn_id+"_"+num).remove();
      for(var i = num+1; i <= amount; i++){
        var new_i = i - 1;
        jQuery("#submenu_"+menu_btn_id+"_"+i).attr("id", "submenu_"+menu_btn_id+"_"+new_i);
        jQuery("#buttons_"+menu_btn_id+"_title_"+i).attr("name", "buttons_"+menu_btn_id+"_title["+new_i+"]");
        jQuery("#buttons_"+menu_btn_id+"_title_"+i).attr("id", "buttons_"+menu_btn_id+"_title_"+new_i);
        jQuery("#buttons_"+menu_btn_id+"_text_"+i).attr("name", "buttons_"+menu_btn_id+"_text["+new_i+"]");
        jQuery("#buttons_"+menu_btn_id+"_text_"+i).attr("id", "buttons_"+menu_btn_id+"_text_"+new_i);
        jQuery("#add_"+menu_btn_id+"_"+i).attr("id", "add_"+menu_btn_id+"_"+new_i);
        jQuery("#del_"+menu_btn_id+"_"+i).attr("id", "del_"+menu_btn_id+"_"+new_i);
      }

      if(!jQuery("#input_area_"+menu_btn_id).children("div").length){
        jQuery("#input_area_"+menu_btn_id).html("");
        jQuery("#create_buttons_"+menu_btn_id).attr("checked", false);
      }
      jQuery("#submenu_amount_"+menu_btn_id).attr("value", amount-1);
      jQuery.get('/preview.php', {act : "del", id : menu_btn_id, num : num}, function(data){
        jQuery("#prev-buttons").html(data);
      });
    });

    jQuery(".menu_title").live("focus", function(){
      var button_title_text = jQuery("#button_title_text").attr("value");
      if(jQuery(this).attr("value") == button_title_text){
        var id_value = jQuery(this).attr("id").split("_");
        var menu_btn_id = id_value[1];
        jQuery(this).attr("value", "");
        jQuery("#submenu_amount_"+menu_btn_id).attr("value", "0")
      }
    });

    jQuery(".menu_title").live("blur", function(){
       var id_value = jQuery(this).attr("id").split("_");
       var menu_btn_id = id_value[1];
       var num = parseInt(id_value[3]);
       var title = jQuery(this).attr("value");
       jQuery.get('/preview.php', {act : "set_title", id : menu_btn_id, num : num, title : title}, function(data){
        jQuery("#prev-buttons").html(data);
      });
    });

    jQuery(".stat_view").click(function(){
      var id_value = jQuery(this).attr("id").split("_");
      var obj = jQuery("#info_"+id_value[1]);
      if(obj.css("display") == "none"){
        obj.show();
      } else {
        obj.hide();
      }
    });

    jQuery(".step_title").click(function(){
      if(jQuery(this).next(".step_more").css("display") != "none"){
        jQuery(this).next(".step_more").hide();
      } else {
        jQuery(this).next(".step_more").show();
      }
    });

    jQuery("#prev-buttons").mouseover(function(){
      if(!jQuery.browser.msie){
        if(jQuery(".create_button").length){
          jQuery(".create_button").focus();
        }
      } else {
//        var obj = document.getElementById("create_buttons_1");
//        obj.setFocus();
      }
    });

    jQuery("#btn_reset").click(function(){
      if(confirm("RESET will clear all data entered for this button. Continue with resetting this button?")){
        window.location.href="/action-reset.html";
      }
    });
});
