document.write("<script type=\"text/javascript\" src=\"webmanage_custom/public_panel/other/jquery.form.js\"></script>");
document.write("<script type=\"text/javascript\" src=\"webmanage_custom/public_panel/other/jquery.cycle.all.min.js\"></script>");
document.write("<script type=\"text/javascript\" src=\"webmanage_custom/public_panel/other/jquery.lightbox-0.5.js\"></script>");
document.write("<script type=\"text/javascript\" src=\"webmanage_custom/public_panel/other/jquery.scrollTo-min.js\"></script>");


/**
 * Logout the customer via AJAX and reload current page
*/
function WBM_logoutCustomer() {
    $.post('webmanage_custom/public_panel/ajax/customer_login.php', {action: 'logout'}, function(json) {
        if(json.status == true) {
            location.reload();
        }
    }, 'json');
}

/**
 * Search website (triggered by "GO" button from header)
*/
function WBM_searchWebsite() {
    search_text = $('#frm_search_text').val();

    if(search_text == 'Search DataDot here' || search_text == '') {
        $('#frm_search_text').focus();
    }
    else {
        $('#search_form').submit();
    }
}


/**
 * Dummy function
*/
function datadot() {
    // nothing
}

/**
 * Remove product from cart via AJAX, and refresh the page.
 * It's done like this so the browser doesn't save links like ?frm_action=delete...
*/
function WBM_removeCartProduct(id) {
    $.post('webmanage_custom/public_panel/ajax/cart_misc.php', {action: 'remove', id: id}, function(json) {
        if(json.status == true) {
            location.reload();
        }
    }, 'json');
}

/** 
 * Popup opener
*/
function WBM_popup(url, width, height, scrollbars, status, resizable, titlebar, toolbar) {
    var popleft = ((document.body.clientWidth - width) / 2) + window.screenLeft;
    var poptop = ((( document.body.clientHeight - height) / 2)) + window.screenTop-40;
		
    str = "" + Math.random() + "";
    _unique_id = str.substr(2, str.length);
		
    window.open(url, _unique_id ,"status=" + status + ",resizable=" + resizable + ",titlebar=" + titlebar + ",toolbar=" + toolbar + ",scrollbars=" + scrollbars + ",width=" + width + ",height=" + height + ",left=" + popleft + ",top=" + poptop);
}
	
/**
 * 'What is CCV' popup
*/
function WBM_whatIsCCV() {
    WBM_popup('what_is_ccv.php', 560, 300, 'no', 'yes', 'no', 'no', 'no');
}

/**
 * 'Freight details' popup
*/
function WBM_freightDetails() {
    WBM_popup('freight_details.php', 560, 300, 'no', 'yes', 'no', 'no', 'no');
}

/**
 * 'Crime Stoppers ID' popup
*/
function WBM_csidDetails() {
    WBM_popup('csid_details.php', 560, 300, 'no', 'yes', 'no', 'no', 'no');
}

$(document).ready(function(){

    /**
     * Homepage big images
    */
    $('#homepage-image').cycle({ 
        fx: 'fade', 
        speed: 2000, 
        timeout: 10000,
        continuous: 0,
        pager: '#homepage-image-pag' 
    });

    /**
     * Try to login the customer via Ajax. Triggered when 'GO' button
     * from header is clicked
    */
    var form_login_options = { 
        dataType: 'json',
        url: 'webmanage_custom/public_panel/ajax/customer_login.php',
        data: {action: 'login'},
        success: function(json) { 
            // Reload current page if success
            if(json.status == true) {
                location.reload();
            }
            // or display error message
            else {
                $('#login-message-box').html(json.message).fadeIn();
                $('#frm_top_password').val('');
            }
        }
    }; 
 	
    $('#form_login').ajaxForm(form_login_options);



    /**
     * Empty field on focus and populate back if nothing typed (for class='focus')
    */
    $('input.focus, textarea.focus').focus(function(){
        if($(this).val() == $(this).attr('alt')) {
            $(this).val('');
        }
    });
    $('input.focus, textarea.focus').blur(function(){
        if($(this).val() == '') {
            $(this).val($(this).attr('alt'));
        }
    });

    /**
     * Initialize stuff from markup
    */
    initMapTabs();
    //initMapTabsDetails();
    initMap();
    initButtonHover();

    /**
     * Lightbox for resource images on Media and News
    */
    $('.page-images .img-lightbox a').lightBox({hideDownload:true});
    $('.details-media .img-lightbox a').lightBox();
    $('.media-container .res-main .image .img a').lightBox();

});


/*IE 6 hover plagin*/
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 u=k(){9 g=/^([^#.>`]*)(#|\\.|\\>|\\`)(.+)$/;k u(a,b){9 c=a.J(/\\s*\\,\\s*/);9 d=[];n(9 i=0;i<c.l;i++){d=d.v(o(c[i],b))};6 d};k o(a,b,c){a=a.z(" ","`");9 d=a.r(g);9 e,5,m,7,i,h;9 f=[];4(d==8){d=[a,a]};4(d[1]==""){d[1]="*"};4(c==8){c="`"};4(b==8){b=E};K(d[2]){w"#":7=d[3].r(g);4(7==8){7=[8,d[3]]};e=E.L(7[1]);4(e==8||(d[1]!="*"&&!x(e,d[1]))){6 f};4(7.l==2){f.A(e);6 f};6 o(7[3],e,7[2]);w".":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};7=d[3].r(g);4(7!=8){4(e.j==8||e.j.r("(\\\\s|^)"+7[1]+"(\\\\s|$)")==8){q};m=o(7[3],e,7[2]);f=f.v(m)}y 4(e.j!=8&&e.j.r("(\\\\s|^)"+d[3]+"(\\\\s|$)")!=8){f.A(e)}};6 f;w">":4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};m=o(d[3],e,">");f=f.v(m)};6 f;w"`":5=p(b,d[1]);n(i=0,h=5.l;i<h;i++){e=5[i];m=o(d[3],e,"`");f=f.v(m)};6 f;M:4(c!=">"){5=p(b,d[1])}y{5=b.B};n(i=0,h=5.l;i<h;i++){e=5[i];4(e.C!=1){q};4(!x(e,d[1])){q};f.A(e)};6 f}};k p(a,b){4(b=="*"&&a.F!=8){6 a.F};6 a.p(b)};k x(a,b){4(b=="*"){6 N};6 a.O.G().z("P:","")==b.G()};6 u}();k Q(a,b){9 c=u(a);n(9 i=0;i<c.l;i++){c[i].R=k(){4(t.j.H(b)==-1){t.j+=" "+b}};c[i].S=k(){4(t.j.H(b)!=-1){t.j=t.j.z(b,"")}}}}4(D.I&&!D.T){D.I("U",V)}',58,58,'||||if|listNodes|return|subselector|null|var||||||||limit||className|function|length|listSubNodes|for|doParse|getElementsByTagName|continue|match||this|parseSelector|concat|case|matchNodeNames|else|replace|push|childNodes|nodeType|window|document|all|toLowerCase|indexOf|attachEvent|split|switch|getElementById|default|true|nodeName|html|hoverForIE6|onmouseover|onmouseout|opera|onload|ieHover'.split('|'),0,{}))
/*parameters [selector, hover_class]*/
function ieHover() {
	hoverForIE6("#nav li, .box-list li, #header .search-box span, .login-form span, .box-holder .input-row span", "hover");
}


/**
 * @file: image-map.js
 * @date: 28/05/02010
*/
function initMap() {
	$('#map area').hover(
        function() {
            $('#map-hover #' + $(this).attr('alt')).addClass('hover');
        },
        function() {
            $('#map-hover #' + $(this).attr('alt')).removeClass('hover');
        }            
    );
}



/**
 * @file: tabs.js
 * @date: 28/05/02010
*/
function initMapTabs() {

    $('#map.tabset2 .tab').click(function() {
        $('#map-hover li').removeClass('activestate');
        $('#map-hover li#' + $(this).attr('alt')).addClass('activestate');

        $('#parent-holder .tab-content').css('display', 'none');
        $('#parent-holder #' + $(this).attr('href').replace('#', '') + '.tab-content').css('display', 'block');

        $('#region-container').css('visibility', 'visible');

		if($(this).attr('alt') == 'asia_pacific') {
			$('#selected-region').addClass('small');
		}
		else {
			$('#selected-region').removeClass('small');
		}

        $('#selected-region').html($(this).attr('rel').replace(/^(.)|\s(.)/g, function($1){ return $1.toUpperCase( ); }));

        getMapDetails($(this).attr('alt'), '', '');

        return false;
    });

}

/**
 * Get the states/country/partners lists via ajax
 * @param string continent
 * @param string country
 * @param string state
*/
function getMapDetails(continent, country, state) {
    
    //$('#map-details').hide();
    //$('#map-loading').show();

    $.post('webmanage_custom/public_panel/ajax/partners_map.php', {continent: continent, country: country, state: state}, function(json) {
        //$('#map-loading').hide();
        //$('#map-details').show();

        if(json.container == 'left') {
            $('#map-details').html(json.content);
        }
        
        if(json.container == 'right' || (json.container == 'left' && json.head_office != '')) {
            // Fill partners and display first
            $('#map-partners').html(json.head_office ? json.head_office : json.content);
            $('#map-partners div.tab-content:first').addClass('active');

            initPartnerNavigation();
        }

        $('.region-list a.tab').click(function() {
            $('.region-list a.tab').removeClass('active');
            $(this).addClass('active');
        });

    }, 'json');

}

/**
 * Navigation through multiple partners on same state/country
*/
function initPartnerNavigation() {
    tabs = $('#map-partners .tab-content').length;
    
    display_next = false;
    display_prev = false;


    // Display nav only if more than 1 tab
    if(tabs > 1) {
        // Is first; display only 'Next'
        if($('#map-partners div.active').index() == 0) {
            display_next = true;
        }
        // Is last; display only 'Previous'
        else if($('#map-partners div.active').index() == tabs -1) {
            display_prev = true;
        }
        // Is a middle tab
        else {
            display_next = true;
            display_prev = true;
        }
    }
    
    // Hide both buttons first
    $('#map-partners .tab-nav a.next').hide();
    $('#map-partners .tab-nav a.prev').hide();

    if(display_next == true) {
        $('#map-partners .tab-nav a.next').show();
    }

    if(display_prev == true) {
        $('#map-partners .tab-nav a.prev').show();
    }
}


function nextPartner() {
    $('#map-partners div.active').removeClass('active').next().addClass('active');
    initPartnerNavigation();
}
function previousPartner() {
    $('#map-partners div.active').removeClass('active').prev().addClass('active');
    initPartnerNavigation();
}


/**
 * Zip Locator search
 */
var zipSearch_start = 0;

function WBM_zipSearch(start) {

	if(start == -1) {
		start = zipSearch_start + 5;
	}

	zipSearch_start = start;

	if($('#frm_zip_code').val() == '' || $('#frm_zip_code').val() == $('#frm_zip_code').attr('alt')) {
		$('#frm_zip_code').focus();
	}
	else {

		var zip_code = $('#frm_zip_code').val();
		var zip_range = $('#frm_zip_range').val();

		$.get('webmanage_custom/public_panel/ajax/zip_locator_search.php', {action: 'search', zip_code: zip_code, zip_range: zip_range, start: zipSearch_start}, function(json) {
			if(json.status == true) {
				
				if(zipSearch_start == 0) {
					$('#zip-results').html('');
				}
				
				if($('#zip-results .tab-content').length > 0) {
					$(document).scrollTo($('#zip-results .tab-content:last'), 400);
				}

				$('#zip-results').append(json.content);

				//alert();
				//

				if(json.block_more) {
					$('#more-zip-results').hide();
				}
				else {
					$('#more-zip-results').show();
				}
			}
		}, 'json');
	}
}

/**
 * @file: submit-hover.js
 * @date: 28/05/02010
*/

function initButtonHover() {
    if (typeof document.body.style.maxHeight == 'undefined') ie6 = true;
    else ie6 = false;
    var inputs = document.getElementsByTagName("input");
    for(var i=0; i<inputs.length; i++) {
        if(inputs[i].type == "image") {
            if (ie6) {
                if (inputs[i].src.indexOf(".png") != -1) {
                    var src = inputs[i].src;
                    inputs[i].path = inputs[i].src;
                    inputs[i].src = "images/none.gif";
                    inputs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
                }
            }
            inputs[i].onmouseover = function() {
                if(this.path && ie6) this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.path.replace(this.path, this.path.substr(0, this.path.lastIndexOf(".")) + "-hover" + this.path.substr(this.path.lastIndexOf("."))) + "',sizingMethod='scale')";
                else this.src = this.src.replace(this.src, this.src.substr(0, this.src.lastIndexOf(".")) + "-hover" + this.src.substr(this.src.lastIndexOf(".")));
            }
            inputs[i].onmouseout = function() {
                if(this.path && ie6) this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.path + "',sizingMethod='scale')";
                this.src = this.src.replace("-hover", "");
            }
        }
    }
}

/** 
 * Return the html with states dropdown for selected country
 */
function WBM_getCountryStates(country) {
	if(country + '' == '') {
		$('#state-sel-container').html('').hide();
		$('#state-temp-sel-container').show();
	}
	else {
		$.post('webmanage_custom/public_panel/ajax/country_states.php', {country: country}, function(json) {
			if(json.status == true) {
				$('#state-temp-sel-container').hide();
				$('#state-sel-container').html(json.content).show();
			}
			else {
				$('#state-sel-container').html('').hide();
				$('#state-temp-sel-container').show();
			}
		}, 'json');
	}
}
