/*********************************************************************
* Copyright 2009 Amblique :: www.amblique.com :: All Rights Reserved *
**********************************************************************/

$(function() {
    initialiseDefaultButtons();     // initialise default buttons
    initialiseTooltips();
    initialiseTriggers();

    // PopupWindow :: http://rip747.github.com/popupwindow/
    var profiles = {
        popupToolsPrint: { height: 500, width: 600, center: 1, scrollbars: 1, status: 0, resizable: 1 },
        popupToolsEmail: { height: 400, width: 500, center: 1, scrollbars: 1, status: 0, resizable: 1 }
    };
    $(".tool").popupwindow(profiles);
});

initialiseTriggers = function() {
    trace('initialiseTriggers');

    var TRIGGER_CLASS = '.hastrigger';
    var TRIGGER_TARGET_ATTR = 'triggers';
    var MOUSE_EVENT_TO_TRIGGER = 'click';

    $(TRIGGER_CLASS).each(function(i) {
        $(this).bind(MOUSE_EVENT_TO_TRIGGER, function(event) {
            var trigger_id = $(this).attr(TRIGGER_TARGET_ATTR);
            var returnCode = false;
            if (typeof trigger_id != 'undefined' && trigger_id !== null) {
                if ($(trigger_id).length > 0) {
                    var href = $(trigger_id).attr('href');
                    var bubble = $(trigger_id).triggerHandler(MOUSE_EVENT_TO_TRIGGER); trace(bubble);
                    if ((typeof bubble == 'undefined' || bubble) && (typeof href != 'undefined' && /javascript/i.test(href))) {
                        returnCode = eval(href);
                    }
                }
            }
            return false;
        });
    });
}

initialiseTooltips = function() {
    trace('initialiseTooltips');

    var TOOLTIP_CLASS = '.tipenabled';
    var TOOLTIP_TARGET_ATTR = 'tiptarget';

    $(TOOLTIP_CLASS).each(function(i) {
        trace(i);
        var target_id = $(this).attr(TOOLTIP_TARGET_ATTR);
        if (typeof target_id != 'undefined') {
            trace(this); trace(target_id);
            $(this).tooltip({
                tip: target_id,
                delay: 0,
                effect: 'toggle'
            });
        }
    });
}

initialiseDefaultButtons = function() {
    trace('initialiseDefaultButtons');

    var FORM_ID = '#' + $('form').attr('id');
    var DEFAULT_BUTTON_ATTRIBUTE = 'default';

    var KEY_EVENT_TO_TRACK = 'keypress';
    var MOUSE_EVENT_TO_TRIGGER = 'click';
    var SUBMIT_KEY_CODE = 13;

    $("input[" + DEFAULT_BUTTON_ATTRIBUTE + "]").each(function(i) {
        trace(this);
        $(this).unbind(KEY_EVENT_TO_TRACK).bind(KEY_EVENT_TO_TRACK, function(event) {
            trace(KEY_EVENT_TO_TRACK);
            try {
                if (event.keyCode == SUBMIT_KEY_CODE) { // enter key detected
                    var returnCode = false;
                    if ($(this).attr(DEFAULT_BUTTON_ATTRIBUTE)) { // get the target element (default button)
                        var default_button_id = '#' + $(this).attr(DEFAULT_BUTTON_ATTRIBUTE);
                        if ($(default_button_id).length > 0) {
                            var default_button_type = $(default_button_id)[0].tagName;
                            if (/a/i.test(default_button_type)) { // this is an anchor button (link button)
                                var href = $(default_button_id).attr('href');
                                if (/javascript/i.test(href)) {
                                    var bubble = $(default_button_id).triggerHandler(MOUSE_EVENT_TO_TRIGGER);
                                    if (bubble || typeof bubble == 'undefined') returnCode = eval(href);
                                }
                            } else if (/input/i.test(default_button_type)) { // this is an input button
                                // need to clear jQuery validation object if present
                                var form_id = '#' + $('form').attr('id');
                                if ($.data($(form_id))) {
                                    $(form_id).validate().resetForm();
                                    $.removeData($(form_id));
                                }
                                returnCode = $(default_button_id).trigger(MOUSE_EVENT_TO_TRIGGER);
                            } else { // can't determine the type, so try clicking
                                try { returnCode = $(default_button_id).trigger(MOUSE_EVENT_TO_TRIGGER); } catch (e) { }
                            }
                        }
                    }
                    return false;
                }
            } catch (exception) {
                trace(exception);
            }
        });
    });
}

trace = function(message) {
    var page = window.location; var hasConsole = false;
    try { if (typeof (console) != "undefined") hasConsole = true; } catch (e) { }
    if (hasConsole) { console.log(message); }
}

/**********************************
* Custom scripts
**********************************/
// Belated PNG Fix :: http://www.dillerdesign.com/experiment/DD_belatedPNG
try { DD_belatedPNG.fix('#png_image, .png_class'); } catch (e) { }

/***** Google toolbar yellow fields Fix *****/
try { if (window.attachEvent) window.attachEvent("onload", setListeners); } catch (e) { }

function setListeners() {
    inputList = document.getElementsByTagName("INPUT");
    for (i = 0; i < inputList.length; i++) {
        inputList[i].attachEvent("onpropertychange", restoreStyles);
        inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for (i = 0; i < selectList.length; i++) {
        selectList[i].attachEvent("onpropertychange", restoreStyles);
        selectList[i].style.backgroundColor = "";
    }
}

function restoreStyles() {
    if (event.srcElement.style.backgroundColor != "" && event.srcElement.style.backgroundColor != "#a0d0ff") {
        event.srcElement.style.backgroundColor = "#a0d0ff"; /* color of choice for AutoFill */
        document.all['googleblurb'].style.display = "block";
    }
}

/***** Clear Input Text *****/
function clearText(thefield, clearType) {
    if (thefield.defaultValue == thefield.value) {
        thefield.value = ""
    }
}

function openPreview(url, width, height, title)
{
	//Getting rad window manager
	var oManager = GetRadWindowManager();
	var oWnd;
	
	oWnd = window.radopen(null, "RadWindow");
	oWnd.ClientCallBackFunction = closeWin;
	
	if (!width) { width = 800; }
	if (!height) { height = 900; }

	oWnd.SetSize(width, height);
	oWnd.SetUrl(url);
	oWnd.SetModal(true);
	oWnd.SetTitle(title);
	//oWnd.Center();
	var x = (document.documentElement.clientWidth - oWnd.Width) / 2;
	var y = (document.documentElement.clientHeight - oWnd.Height) / 2;
	
	oWnd.Show();
	oWnd.MoveTo(x, y);
	oWnd.Center();
	
	return true;
}

function AddToCart(baseUrl, iProductID, iQty)	{
	GB_showCenter('', baseUrl +'additem.aspx?pkid='+ iProductID + '&qty=' + iQty, 400, 450);
}

function AddToCartCompleted()	{
	window.location.reload();
}

function closeGBWindowFromPopup(noRefresh)  {
    if (!noRefresh)  {
        top.location.href = top.location.href;
    }
    top.GB_hide();
}

function gotoURL(url)   {
    top.GB_hide();
    top.location.href = url;
}

function changeURLs()   {
    if (window.location.href.indexOf('popup=1') > -1)   {
        $(document).ready(function() {
            $("a").each(function() {
                if ($(this).attr("href") != '')   {
                    $(this).attr("href", "javascript:gotoURL('"+ $(this).attr("href") +"');");
                }
            });
        });
    }
}

