<?php
add_action('widgets_init', 'serpentsoft_advertisements_widget_init');

function serpentsoft_advertisements_widget_init() {
    register_widget('serpentsoft_advertisements_widget');
}

class serpentsoft_advertisements_widget extends WP_Widget {

    function serpentsoft_advertisements_widget() {
        $widget_ops = array('classname' => 'w-adv', 'description' => '');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-advertisements');
        $this->WP_Widget('id-widget-advertisements', theme_name . ' - Advertisements', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        global $post;

        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

        $title = $instance['title'];
        $hide_title = $instance['hideTitle'];
        $open_newTab = $instance['openNewTab'];
        
        $adv_type = $instance['advType'];
        
        $adv1Code = $instance['adv1Code'];
        $adv1Image = $instance['adv1Image'];
        $adv1URL = $instance['adv1URL'];
        
        $adv2Code = $instance['adv2Code'];
        $adv2Image = $instance['adv2Image'];
        $adv2URL = $instance['adv2URL'];
        
        $adv3Code = $instance['adv3Code'];
        $adv3Image = $instance['adv3Image'];
        $adv3URL = $instance['adv3URL'];
        
        $adv4Code = $instance['adv4Code'];
        $adv4Image = $instance['adv4Image'];
        $adv4URL = $instance['adv4URL'];
        
        echo $before_widget;
        if (!$hide_title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif;
        
        $str_newTag = $open_newTab ? TRUE : FALSE;
        
        // Draw Widget in website
        // Check Advertisement Type
        if($adv_type == '300x600' || $adv_type == '300x250' || $adv_type == '300x100' || $adv_type == '250x250' || $adv_type == '160x600' || $adv_type == 'custom'):
            serpentsoft_wdg_adv_Draw($str_newTag, $adv1Code, $adv1Image, $adv1URL);
        else:
            $css_class_sponsors_adv = '';
            if($adv_type == '125x125') { $css_class_sponsors_adv = 'w-adv-125x125'; }
            elseif($adv_type == '120x600') { $css_class_sponsors_adv = 'w-adv-120x600'; }
            elseif($adv_type == '120x240') { $css_class_sponsors_adv = 'w-adv-120x240'; }
            elseif($adv_type == '120x90') { $css_class_sponsors_adv = 'w-adv-120x90'; }
            elseif($adv_type == '120x60') { $css_class_sponsors_adv = 'w-adv-120x60'; } ?>

            <ul class="widget-sponsor clearfix <?php echo $css_class_sponsors_adv; ?>">
                <?php if( (isset($adv1Code) && $adv1Code != '') || (isset($adv1Image) && $adv1Image != '') || (isset($adv1URL) && $adv1URL != '')): ?>
                    <li><?php serpentsoft_wdg_adv_Draw($str_newTag, $adv1Code, $adv1Image, $adv1URL); ?></li>
                
                <?php endif; if( (isset($adv2Code) && $adv2Code != '') || (isset($adv2Image) && $adv2Image != '') || (isset($adv2URL) && $adv2URL != '')): ?>
                    <li><?php serpentsoft_wdg_adv_Draw($str_newTag, $adv2Code, $adv2Image, $adv2URL); ?></li>
                    
                <?php endif; if( (isset($adv3Code) && $adv3Code != '') || (isset($adv3Image) && $adv3Image != '') || (isset($adv3URL) && $adv3URL != '')): ?>
                    <li><?php serpentsoft_wdg_adv_Draw($str_newTag, $adv3Code, $adv3Image, $adv3URL); ?></li>
                
                <?php endif; if( (isset($adv4Code) && $adv4Code != '') || (isset($adv4Image) && $adv4Image != '') || (isset($adv4URL) && $adv4URL != '')): ?>
                    <li><?php serpentsoft_wdg_adv_Draw($str_newTag, $adv4Code, $adv4Image, $adv4URL); ?></li>
                    
                <?php endif ?>
            </ul>
        <?php endif;
        
        echo $after_widget;
    }
    
    // Update Widget Fields
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['hideTitle'] = strip_tags($new_instance['hideTitle']);
        $instance['advType'] = strip_tags($new_instance['advType']);
        $instance['openNewTab'] = strip_tags($new_instance['openNewTab']);
        
        $instance['adv1Code'] = $new_instance['adv1Code'];
        $instance['adv1Image'] = $new_instance['adv1Image'];
        $instance['adv1URL'] = $new_instance['adv1URL'];
        
        $instance['adv2Code'] = $new_instance['adv2Code'];
        $instance['adv2Image'] = $new_instance['adv2Image'];
        $instance['adv2URL'] = $new_instance['adv2URL'];
        
        $instance['adv3Code'] = $new_instance['adv3Code'];
        $instance['adv3Image'] = $new_instance['adv3Image'];
        $instance['adv3URL'] = $new_instance['adv3URL'];
        
        $instance['adv4Code'] = $new_instance['adv4Code'];
        $instance['adv4Image'] = $new_instance['adv4Image'];
        $instance['adv4URL'] = $new_instance['adv4URL'];
        
        return $instance;
    }

    // Draw Widget Controls
    function form($instance) {
        $defaults = array('title' => 'Advertisement', 'hideTitle' => '', 'advType' => '', 'openNewTab' => '',
            'adv1Code' => '', 'adv1Image' => '', 'adv1URL' => '',
            'adv2Code' => '', 'adv2Image' => '', 'adv2URL' => '',
            'adv3Code' => '', 'adv3Image' => '', 'adv3URL' => '',
            'adv4Code' => '', 'adv4Image' => '', 'adv4URL' => '',
            );
        $instance = wp_parse_args((array) $instance, $defaults);
        
        $txt_adv1_code = esc_textarea( $instance['adv1Code'] );
        $txt_adv2_code = esc_textarea( $instance['adv2Code'] );
        $txt_adv3_code = esc_textarea( $instance['adv3Code'] );
        $txt_adv4_code = esc_textarea( $instance['adv4Code'] ); ?>

        <style>
            .adv-container{
                padding: 10px 2px;
                border: 1px solid #d5d5d5;
            }
            .adv-small-warn{
                display: block;
                font-weight: bold;
                color: red;
            }
            .adv-banner-text{
                display: block;
                width: 217px;
            }
            .adv-code-textArea{
                display: block;
                width: 217px;
                height: 75px;
            }
            .adv-sep{
                border: none;
                border-bottom: 1px solid #8A8A8A;
                border-top: 1px solid #CCCCCC;
            }
            .adv-sep.margin15{
                margin: 15px 0;
            }
            .adv-code-text-sep{
                border: none;
                border-bottom: 1px solid #CCCCCC;
            }
            .adv-code-text-sep:before{
                content: "OR ";
            }
        </style>
        
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'serpentsoft'); ?> :</label>
            <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('hideTitle'); ?>"><?php _e('Hide Box Title?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('hideTitle'); ?>" name="<?php echo $this->get_field_name('hideTitle'); ?>"
                   value="false" <?php if ($instance['hideTitle']) { echo 'checked="checked"'; } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('openNewTab'); ?>"><?php _e('Open in a new window?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('openNewTab'); ?>" name="<?php echo $this->get_field_name('openNewTab'); ?>"
                   value="false" <?php if ($instance['openNewTab']) { echo 'checked="checked"'; } ?> type="checkbox" />
            <small class="adv-small-warn">Open in new window will work only with uploaded image.</small>
        </p>
        
        <p>
            <label for="<?php echo $this->get_field_id('advType'); ?>"><?php _e('Advertisement Size', 'serpentsoft'); ?> :</label>

            <select id="<?php echo $this->get_field_id('advType'); ?>" name="<?php echo $this->get_field_name('advType'); ?>" class="widget-select-adv-type">
                <optgroup value="" disabled="disabled" label="First Section"></optgroup>
                <option value="300x600" <?php if($instance['advType'] == '300x600') { echo ' selected="selected"'; } ?>>300x600</option>
                <option value="300x250" <?php if($instance['advType'] == '300x250') { echo ' selected="selected"'; } ?>>300x250</option>
                <option value="300x100" <?php if($instance['advType'] == '300x100') { echo ' selected="selected"'; } ?>>300x100</option>
                <option value="250x250" <?php if($instance['advType'] == '250x250') { echo ' selected="selected"'; } ?>>250x250</option>
                <option value="160x600" <?php if($instance['advType'] == '160x600') { echo ' selected="selected"'; } ?>>160x600</option>
                
                <optgroup value="" disabled="disabled" label="Second Section"></optgroup>
                <option value="125x125" <?php if($instance['advType'] == '125x125') { echo ' selected="selected"'; } ?>>125x125</option>
                <option value="120x600" <?php if($instance['advType'] == '120x600') { echo ' selected="selected"'; } ?>>120x600</option>
                <option value="120x240" <?php if($instance['advType'] == '120x240') { echo ' selected="selected"'; } ?>>120x240</option>
                <option value="120x90" <?php if($instance['advType'] == '120x90') { echo ' selected="selected"'; } ?>>120x90</option>
                <option value="120x60" <?php if($instance['advType'] == '120x60') { echo ' selected="selected"'; } ?>>120x60</option>
                
                <option value="custom" <?php if($instance['advType'] == 'custom') { echo ' selected="selected"'; } ?>>Custom</option>
            </select>
        </p>
        
        <hr class="adv-sep margin15" />
        
        <div id="widget-adv-1-ad" class="adv-container">
            <p>
                
                <label for="<?php echo $this->get_field_id('adv1Code'); ?>"><?php _e('Advertisement (1) Code', 'serpentsoft'); ?> :</label>
                <textarea id="<?php echo $this->get_field_id('adv1Code'); ?>" name="<?php echo $this->get_field_name('adv1Code'); ?>" class="adv-code-textArea" size="3"><?php echo $txt_adv1_code; ?></textarea>
            </p>
            
            <hr class="adv-code-text-sep" />
            
            <p>
                <label for="<?php echo $this->get_field_id('adv1Image'); ?>"><?php _e('Advertisement (1) Image Path', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv1Image'); ?>" name="<?php echo $this->get_field_name('adv1Image'); ?>" class="adv-banner-text" size="3" value="<?php echo $instance['adv1Image']; ?>" type="text" />
            </p>
            
            <p>
                <label for="<?php echo $this->get_field_id('adv1URL'); ?>"><?php _e('Advertisement (1) URL', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv1URL'); ?>" name="<?php echo $this->get_field_name('adv1URL'); ?>" type="text" class="adv-banner-text" size="3" value="<?php echo $instance['adv1URL']; ?>" />
            </p>
        </div>
        
        <hr class="adv-sep margin15" />
        <small class="adv-small-warn"><?php _e('The following advertisements fields are for (125x125 - 120x600 - 120x240 - 120x90 - 120x60) ONLY.', 'serpentsoft'); ?></small>
        <div id="widget-adv-2-ad" class="adv-container">
            <p>
                <label for="<?php echo $this->get_field_id('adv2Code'); ?>"><?php _e('Advertisement (2) Code', 'serpentsoft'); ?> :</label>
                <textarea id="<?php echo $this->get_field_id('adv2Code'); ?>" name="<?php echo $this->get_field_name('adv2Code'); ?>" class="adv-code-textArea" size="3"><?php echo $txt_adv2_code; ?></textarea>
            </p>
            
            <hr class="adv-code-text-sep" />
            
            <p>
                <label for="<?php echo $this->get_field_id('adv2Image'); ?>"><?php _e('Advertisement (2) Image Path', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv2Image'); ?>" name="<?php echo $this->get_field_name('adv2Image'); ?>" class="adv-banner-text" size="3" value="<?php echo $instance['adv2Image']; ?>" type="text" />
            </p>
            
            <p>
                <label for="<?php echo $this->get_field_id('adv2URL'); ?>"><?php _e('Advertisement (2) URL', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv2URL'); ?>" name="<?php echo $this->get_field_name('adv2URL'); ?>" class="adv-banner-text" size="3" value="<?php echo $instance['adv2URL']; ?>" type="text" />
            </p>
        </div>
        
        <hr class="adv-sep margin15" />
        
        <div id="widget-adv-3-ad" class="adv-container">
            <p>
                <label for="<?php echo $this->get_field_id('adv3Code'); ?>"><?php _e('Advertisement (3) Code', 'serpentsoft'); ?> :</label>
                <textarea id="<?php echo $this->get_field_id('adv3Code'); ?>" name="<?php echo $this->get_field_name('adv3Code'); ?>" class="adv-code-textArea" size="3"><?php echo $txt_adv3_code; ?></textarea>
            </p>
            
            <hr class="adv-code-text-sep" />
            
            <p>
                <label for="<?php echo $this->get_field_id('adv3Image'); ?>"><?php _e('Advertisement (3) Image Path', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv3Image'); ?>" name="<?php echo $this->get_field_name('adv3Image'); ?>" class="adv-banner-text" size="3" value="<?php echo $instance['adv3Image']; ?>" type="text" />
            </p>
            
            <p>
                <label for="<?php echo $this->get_field_id('adv3URL'); ?>"><?php _e('Advertisement (3) URL', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv3URL'); ?>" name="<?php echo $this->get_field_name('adv3URL'); ?>" class="adv-banner-text" size="3" value="<?php echo $instance['adv3URL']; ?>" type="text" />
            </p>
        </div>
        
        <hr class="adv-sep margin15" />
        
        <div id="widget-adv-4-ad" class="adv-container">
            <p>
                <label for="<?php echo $this->get_field_id('adv4Code'); ?>"><?php _e('Advertisement (4) Code', 'serpentsoft'); ?> :</label>
                <textarea id="<?php echo $this->get_field_id('adv4Code'); ?>" name="<?php echo $this->get_field_name('adv4Code'); ?>" class="adv-code-textArea" size="3"><?php echo $txt_adv4_code; ?></textarea>
            </p>
            
            <hr class="adv-code-text-sep" />
            
            <p>
                <label for="<?php echo $this->get_field_id('adv4Image'); ?>"><?php _e('Advertisement (4) Image Path', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv4Image'); ?>" name="<?php echo $this->get_field_name('adv4Image'); ?>" class="adv-banner-text" size="3" value="<?php echo $instance['adv4Image']; ?>" type="text" />
            </p>
            
            <p>
                <label for="<?php echo $this->get_field_id('adv4URL'); ?>"><?php _e('Advertisement (4) URL', 'serpentsoft'); ?> :</label>
                <input id="<?php echo $this->get_field_id('adv4URL'); ?>" name="<?php echo $this->get_field_name('adv4URL'); ?>" class="adv-banner-text" size="3" value="<?php echo $instance['adv4URL']; ?>" type="text" />
            </p>
        </div>
        
    <?php }
}

// Draw Advertisement function
function serpentsoft_wdg_adv_Draw($new_window=FALSE, $advCode='', $advImage='', $advURL=''){
    if(isset($advCode) && $advCode != ''): ?>
        
        <div><?php echo $advCode; ?></div>
        
        <?php elseif(isset($advImage) && $advImage != ''): ?>
            <?php if(isset($advURL) && $advURL != ''): ?>
                <a href="<?php echo $advURL; ?>" <?php echo $new_window ? 'target="_blank"' : ''; ?>><img src="<?php echo $advImage; ?>"  alt="Adv_Widget" /></a>
            <?php else: ?>
                <img src="<?php echo $advImage; ?>" alt="Adv_Widget" />
        <?php endif;
    endif;
} ?>