/*
  API:

  publishes event "popup_start" that can be bound via:

  $( 'xyz' ).popups.bind( 'popup_start', function( selectedArray, selectedIndex, selectedOpts ) { alert 'ok'; } );

*/

// var Flint = window.Flint || {};

( function($) {

	$.fn.flint = function() {
	};

	$.flint = function() {
	}

	$.fn.flint.popups = function() {
	}

	$.fn.flint = function() {
		$( this ).each( function () {
				
			} );
	}
} )(jQuery);

var Flint = window.Flint || function(){};

jQuery(document).ready(function($) {

        Flint.Popups = {

            _countdown : 0,

            height : false,

            getSelectors : function() {
                return Flint.popups;
            },

            getUrlParam : function( param, url ) {
                var rx = new RegExp( "(&|\\?)" + param + "=([^&]+)" );
                var matches = url.match( rx );
                return ( matches.length > 0 ) ? matches[ matches.length - 1 ] : null;
            },

            deleteUrlParam : function( param, url ) {
                var helper = document.createElement( 'a' );
                helper.href = url;
                var params = helper.search;
                var rx = new RegExp( "(&|\\?)" + param + "=([^&]+)" );
                params = params.replace( rx, "" );
                if ( params ) {
                    params += ( params ) ? "&" : "?";
                }
                helper.search = params;
                return helper.href;
            },

            addUrlParam : function( param, value, url ) {
                url = this.deleteUrlParam( param, url );
                var helper = document.createElement( 'a' );
                helper.href = url;
                // Some browser append ? to url  automatically
		var append = ( ( helper.search.length == 0 ) && !( url.substr(-1) == '?' || url.substr(url.length - 1) == '?' )) ? '?' : '';
                append += escape( param ) + '=' + escape( value );
                url += append;
                return url;
            },

            init : function() {

                var divide = function( n, d ) {
                    r = n % d;
                    q = ( n - r ) / d;
                    q = ( q >= 0 ) ? Math.floor( q ) : Math.ceil( q );
                    return q;
                }

                //
                //  Init onPopup
                //
                var selectors = this.getSelectors();
                if ( selectors && selectors.popup ) {
                    for ( i=0; i<selectors.popup.length; i++ ) {
                        var item = selectors.popup[ i ];
                        var el = $( item.selector ).addClass( 'iframe' );
                        item.type = 'iframe';
                        item.onStart = this.onPopup;
                        el.fancybox( item );
                    }
                }

                //
                //  Init onResize
                //
                if ( selectors && selectors.resize ) {
                    for ( i=0; i<selectors.resize.length; i++ ) {
                        var item = selectors.resize[ i ];
                        this.onResize( item.options );
                    }
                }

                //
                //  Init onClose
                //
                if ( selectors && selectors.close ) {
                    for ( i=0; i<selectors.close.length; i++ ) {
                        var item = selectors.close[ i ];
                        $( item.selector ).click( this.onClose );
                    }
                }

                //
                //  Init onTimer
                //
                if ( selectors && selectors.timer ) {
                    for ( i=0; i<selectors.timer.length; i++ ) {
                        var item = selectors.timer[ i ];
                        if ( item.selector ) {
                            if ( item.countdown ) {
                                this._countdown = divide( item.selector, 1000 );
                                this.onTimerDisplay();
                            }
                            setTimeout( "Flint.Popups.onTimer()", item.selector );
                        }
                    }
                }

                //
                //  Init onSubmit
                //
                if(window.HTMLElement){
                    /* DOM-compliant Browsers */
                    HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
                    HTMLFormElement.prototype.submit = this.onSubmit;
                } else {
                    /* IE does not expose it's HTMLElement object or its children
                    Let the document load so all forms are accounted for */
                    for(var i = 0; i < document.forms.length; i++ ){
                        document.forms[i]._submit = document.forms[i].submit;
                        document.forms[i].submit = this.onSubmit;
                    }

                }

            },

            onResize : function( opts ) {
                if ( opts.height == "auto" ) {
                    opts.height = $( document ).height();
                }
                if ( opts.width == "auto" ) {
                    opts.width = $( document ).width();
                }
                window.parent.jQuery.fancybox.reshow( opts );
            },

            onPopup : function( selectedArray, selectedIndex, selectedOpts ) {
                var el = selectedArray[ selectedIndex ];
                $( el ).trigger( 'popup_start', [ selectedArray, selectedIndex, selectedOpts ] );
                selectedOpts.href = Flint.Popups.addUrlParam( 'popup', 'True', selectedOpts.href );
            },

            onClose : function() {
                window.parent.jQuery.fancybox.close();
            },

            onTimer : function() {
                window.parent.jQuery.fancybox.close();
            },

            onTimerDisplay : function() {
                window.parent.jQuery( '#fancybox-title-main' ).text( this._countdown );
                this._countdown--;
                setTimeout( "Flint.Popups.onTimerDisplay()", 1000 );
            },

            onSubmit : function() {
                var exit = false;
                var selectors = Flint.Popups.getSelectors();
                if ( selectors && selectors.submit ) {
                    for (i=0; i<selectors.submit.length; i++ ) {
                        var item = selectors.submit[ i ];
                        selection = $( item.selector );
                        for ( j=0; j<selection.length; i++ ) {
                            if ( selection[i] == this ) {
                                this.target = item.target;
                                if ( this.target != "" && this.target != "_self" ) {
                                    this.action = Flint.Popups.deleteUrlParam( 'popup', this.action );
                                };
                                exit = true;
                                break;
                            }
                        }
                        if ( exit ) break;
                    }
                }
                this._submit();
            }
        };

        Flint.Popups.init();

    });

