<?php

$themename = "Shop";
$shortname = "cp";

add_action('admin_init', 'cp_theme_add_init');
add_action('admin_menu', 'cp_theme_add_admin');

function cp_theme_add_admin() 
{
 
	global $themename, $shortname, $options;
 
	if ( $_GET['page'] == basename(__FILE__) ) 
	{
 
		if($_POST['save']) {

update_option('cp_sidebarthumbs', $_POST['cp_sidebarthumbs']);

update_option('cp_sitename', $_POST['cp_sitename']);
update_option('cp_siteslogan', $_POST['cp_siteslogan']);

update_option('cp_metadesc', $_POST['cp_metadesc']);
update_option('cp_keywords', $_POST['cp_keywords']);


update_option('cp_number', $_POST['cp_number']);
update_option('cp_analytics', $_POST['cp_analytics']);
update_option('cp_maintenance', $_POST['cp_maintenance']);
update_option('cp_slider', $_POST['cp_slider']);
update_option('cp_news_slider', $_POST['cp_news_slider']);
update_option('cp_order', $_POST['cp_order']);
update_option('cp_welcome', $_POST['cp_welcome']);

update_option('cp_product_detail_1', $_POST['cp_product_detail_1']);
update_option('cp_product_detail_2', $_POST['cp_product_detail_2']);
update_option('cp_product_detail_3', $_POST['cp_product_detail_3']);
update_option('cp_product_detail_4', $_POST['cp_product_detail_4']);
update_option('cp_product_detail_5', $_POST['cp_product_detail_5']);
update_option('cp_product_detail_6', $_POST['cp_product_detail_6']);
update_option('cp_product_detail_7', $_POST['cp_product_detail_7']);
update_option('cp_product_detail_8', $_POST['cp_product_detail_8']);
update_option('cp_product_detail_9', $_POST['cp_product_detail_9']);
update_option('cp_product_detail_10', $_POST['cp_product_detail_10']);


update_option('cp_order_mail', $_POST['cp_order_mail']);
update_option('cp_contact_mail', $_POST['cp_contact_mail']);
update_option('cp_phone_1', $_POST['cp_phone_1']);
update_option('cp_phone_2', $_POST['cp_phone_2']);
update_option('cp_phone_3', $_POST['cp_phone_3']);
update_option('cp_facebook', $_POST['cp_facebook']);
update_option('cp_twitter', $_POST['cp_twitter']);
update_option('cp_google', $_POST['cp_google']);
update_option('cp_youtube', $_POST['cp_youtube']);
update_option('cp_address', $_POST['cp_address']);
update_option('cp_zip', $_POST['cp_zip']);
update_option('cp_city', $_POST['cp_city']);
update_option('cp_country', $_POST['cp_country']);

$location = $_SERVER['REQUEST_URI'] . "&saved=true";
header("Location: $location");
die;
 
		}
		else if( 'reset' == $_REQUEST['action'] ) 
		{
 
			foreach ($options as $value) 
			{
				delete_option( $value['id'] ); 
			}
 
			$location = $_SERVER['REQUEST_URI'] . "&reset=true";
			header("Location: $location");
			die;
 
		}
	}

	add_theme_page($themename." Options", "Site Options", 'edit_themes', basename(__FILE__), 'cp_theme_admin');
}

function cp_theme_add_init() 
{
	$template_dir = get_bloginfo('template_directory');
	wp_enqueue_style('admin-menu', $template_dir . '/admin/admin-menu.css', false, '1.0', 'all');
}

function cp_theme_admin() 
{
?>


<!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>-->

<!--<script src="http://cdn.jquerytools.org/1.2.6/full/jquery.tools.min.js"></script>-->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://upload.szabadka.org/jquery-ui.js"></script>

<script type="text/javascript">

$(document).ready(function () {

$( "#tabs" ).tabs();

$('.details-container').on("click", "img" ,function(event){
    var thisid = $(this).attr('id');
removefield(thisid);
});

//$('#delete').click(function() {
//var id = $(this).attr('id');
//alert('clicked');
//removefield(id);
//});

$("#add-new-field").click(function(e) {
    e.preventDefault();
    addfield();
});

function removefield(field) {
     $.ajax({
         type: "POST",
         url: "<?php bloginfo('template_url'); ?>/addfield.php?remove=" + field,
         cache: false,
         success: function (html) {
		 $('.' + field).val('');
		 $('.' + field).fadeOut();
         }
     });
}

function addfield() {
     $.ajax({
         type: "POST",
         url: "<?php bloginfo('template_url'); ?>/addfield.php",
         cache: false,
         success: function (html) {
	$('.details-container').append('<span class="cp_product_detail_' + html + '"><label for="cp_product_detail_' + html + '">Product detail #' + html + '</label><input id="cp_product_detail_' + html + '" type="text" value="" name="cp_product_detail_' + html + '"> <span id="delete"><img src="<?php bloginfo('template_url'); ?>/images/del.png" id="cp_product_detail_' + html + '"/></span></span>');
         }
     });
}

});



</script>
<style>
#add-new-field:hover {
    cursor: pointer;
    background: #eee;
}
#add-new-field {
    width: 100px !important;
    clear: both;
    margin-left: 440px;
    border: 1px solid #ccc;
    background: #fff;
}
#delete:hover {
    cursor: pointer;
}
#delete {
    float: right;
    margin-right: 85px;
    margin-top: 6px;
}
#tabs ul {
    height: 30px;
    list-style: none outside none;
    margin-bottom: 0 !important;
    padding: 0;
    width: 810px;
    margin-top:15px;
}
#tabs ul li {
    float: left;
    list-style-image: none !important;
    margin: 0 !important;
    padding: 0;
    text-indent: 0;
}
#tabs ul a {
    background: url("<?php echo get_stylesheet_directory_uri(); ?>/images/blue.png") no-repeat scroll -420px 0 rgba(0, 0, 0, 0);
    color: #333333;
    display: block;
    font-size: 15px;
    height: 30px;
    line-height: 30px;
    margin: 0;
    padding: 0;
    position: relative;
    text-align: center;
    text-decoration: none !important;
    top: 1px;
    width: 134px;
    outline:none;
}
#tabs ul a:active {
    outline: medium none;
}
#tabs ul a:hover {
    background-position: -420px -31px;
    color: #333333;
}
#tabs ul a.current, ul.tabs a.current:hover, ul.tabs li.current a {
    background-position: -420px -62px;
    color: #000000 !important;
    cursor: default !important;
}
#tabs ul a.s {
    background-position: -553px 0;
    width: 81px;
}
#tabs ul.tabs a.s:hover {
    background-position: -553px -31px;
}
#tabs ul a.s.current {
    background-position: -553px -62px;
}
#tabs ul a.l {
    background-position: -248px 0;
    width: 174px;
}
#tabs ul a.l:hover {
    background-position: -248px -31px;
}
#tabs ul a.l.current {
    background-position: -248px -62px;
}
#tabs ul a.xl {
    background-position: 0 0;
    width: 248px;
}
#tabs ul a.xl:hover {
    background-position: 0 -31px;
}
#tabs ul a.xl.current {
    background-position: 0 -62px;
}
.panes .pane {
    display: none;
}
.panes div {
    border: 1px solid #FFFFFF;
    border-radius: 6px;
    display: none;
    font-size: 14px;
    padding: 15px 10px;
    width: 645px;
    overflow:hidden;
}
.panes {
    background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #C8C8C8;
    width: 802px;
}

#site-settings label {
    width: 180px;
    float: left;
    text-align: right;
    padding-right: 10px;
    line-height: 28px;
}
#site-settings input {
    float: left;
    width: 350px;
    margin-bottom:5px;
}
#site-settings select {
    float: left;
    width: 350px;
    margin-bottom:5px;
}
#site-settings h1 {
    font-size: 20px;
    margin-bottom: 15px;
    padding-left: 5px;
}
.settings-saved {
    background: url("<?php echo get_stylesheet_directory_uri(); ?>/images/true.jpg") no-repeat scroll 0 0 rgba(0, 0, 0, 0);
    height: 31px;
    line-height: 31px;
    padding-left: 35px;
}
#wpbody-content {
padding-bottom:5px !important;
}
#save-settings:hover {
    cursor: pointer;
    background: #f1f1f1;
}
#save-settings {
    margin-bottom:5px;
    background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #CCCCCC;
    margin-left: 560px;
    padding: 5px;
    width: 110px;
}
.maintenance-mode {
    float: left;
    margin-top: 15px;
    color: #cc0000;
    text-align: center;
    width: 100%;
    font-weight: bold;
}
</style>

<h1>Settings</h1>

<?php if($_GET['saved'] == "true") { ?>
<div class="settings-saved">Settings saved successfully</div>
<?php } ?>
<div id="tabs">
<ul>
	<li><a href="#tabs-1">General Settings</a></li>
	<li><a href="#tabs-2">SEO Settings</a></li>
	<li><a href="#tabs-3">Product Settings</a></li>
	<li><a href="#tabs-4">Contact Settings</a></li>
	<li><a href="#tabs-5">Social Settings</a></li>
	<li><a href="#tabs-6">Personal Settings</a></li>
</ul>

<form id="site-settings" action="" method="POST">
<div class="panes">
<div id="tabs-1">
	<h1>General settings</h1>
		<label for="cp_sitename">Site name</label>
		<input id="cp_sitename" type="text" value="<?php if(get_option('cp_sitename')) { echo get_option('cp_sitename'); } else { echo ""; } ?>" name="cp_sitename">
		<br />
		<label for="cp_siteslogan">Site slogan</label>
		<input id="cp_siteslogan" type="text" value="<?php if(get_option('cp_siteslogan')) { echo get_option('cp_siteslogan'); } else { echo ""; } ?>" name="cp_siteslogan">
		<br />
		<label for="cp_analytics">Analytics Code</label>
		<input id="cp_analytics" type="text" value="<?php if(get_option('cp_analytics')) { echo get_option('cp_analytics'); } else { echo ""; } ?>" name="cp_analytics">
		<br />
		<label for="cp_welcome">Welcome message</label>
		<select id="cp_welcome" name="cp_welcome">
			<option value="On"<?php if(get_option('cp_welcome') == "On") { echo " selected=\"selected\""; } ?>>On</option>
			<option value="Off"<?php if(get_option('cp_welcome') == "Off") { echo " selected=\"selected\""; } ?>>Off</option>
		</select>
		<br />

		<label for="cp_slider">Slider on main page</label>
		<select id="cp_slider" name="cp_slider">
			<option value="On"<?php if(get_option('cp_slider') == "On") { echo " selected=\"selected\""; } ?>>On</option>
			<option value="Off"<?php if(get_option('cp_slider') == "Off") { echo " selected=\"selected\""; } ?>>Off</option>
		</select>
		<br />
		<label for="cp_news_slider">News slider on main page</label>
		<select id="cp_news_slider" name="cp_news_slider">
			<option value="On"<?php if(get_option('cp_news_slider') == "On") { echo " selected=\"selected\""; } ?>>On</option>
			<option value="Off"<?php if(get_option('cp_news_slider') == "Off") { echo " selected=\"selected\""; } ?>>Off</option>
		</select>
		<label for="cp_order">Order on main page</label>
		<select id="cp_order" name="cp_order">
			<option value="news"<?php if(get_option('cp_order') == "news") { echo " selected=\"selected\""; } ?>>News on top</option>
			<option value="products"<?php if(get_option('cp_order') == "products") { echo " selected=\"selected\""; } ?>>Products on top</option>
		</select>

		<label for="cp_number">Products on index</label>
		<input type="text" id="cp_number" name="cp_number" value="<?php echo get_option('cp_number'); ?>">

		<label for="cp_sidebarthumbs">Sidebar Thumbs</label>
		<select id="cp_sidebarthumbs" name="cp_sidebarthumbs">
			<option value="on"<?php if(get_option('cp_sidebarthumbs') == "on") { echo " selected=\"selected\""; } ?>>On</option>
			<option value="off"<?php if(get_option('cp_sidebarthumbs') == "off") { echo " selected=\"selected\""; } ?>>Off</option>
		</select>

		<label for="cp_maintenance">Maintenance mode</label>
		<select id="cp_maintenance" name="cp_maintenance">
			<option value="on"<?php if(get_option('cp_maintenance') == "on") { echo " selected=\"selected\""; } ?>>On</option>
			<option value="off"<?php if(get_option('cp_maintenance') == "off") { echo " selected=\"selected\""; } ?>>Off</option>
		</select>

		<?php if(get_option('cp_maintenance') == "on") { ?>
			<span class="maintenance-mode">
			Warning, maintenance mode is active. Only logged in users can reach your site!
			</span>
		<?php } ?>

</div>

<div id="tabs-2">
	<h1>SEO settings</h1>
		<label for="cp_metadesc">Meta description</label>
		<input id="cp_metadesc" type="text" value="<?php if(get_option('cp_metadesc')) { echo get_option('cp_metadesc'); } else { echo ""; } ?>" name="cp_metadesc">
		<br />
		<label for="cp_keywords">Meta keywords</label>
		<input id="cp_keywords" type="text" value="<?php if(get_option('cp_keywords')) { echo get_option('cp_keywords'); } else { echo ""; } ?>" name="cp_keywords">
		<br />
</div>

<div id="tabs-3">
	<h1>Product settings</h1>
		<label for="cp_product_detail_1">Product detail #1</label>
		<input id="cp_product_detail_1" type="text" name="cp_product_detail_1" value="<?php if(get_option('cp_product_detail_1')) { echo get_option('cp_product_detail_1'); } else { echo ""; } ?>" name="cp_product_detail_1">
		<label for="cp_product_detail_2">Product detail #2</label>
		<input id="cp_product_detail_2" type="text" name="cp_product_detail_2" value="<?php if(get_option('cp_product_detail_2')) { echo get_option('cp_product_detail_2'); } else { echo ""; } ?>" name="cp_product_detail_2">
		<label for="cp_product_detail_3">Product detail #3</label>
		<input id="cp_product_detail_3" type="text" name="cp_product_detail_3" value="<?php if(get_option('cp_product_detail_3')) { echo get_option('cp_product_detail_3'); } else { echo ""; } ?>" name="cp_product_detail_3">
		<label for="cp_product_detail_4">Product detail #4</label>
		<input id="cp_product_detail_4" type="text" name="cp_product_detail_4" value="<?php if(get_option('cp_product_detail_4')) { echo get_option('cp_product_detail_4'); } else { echo ""; } ?>" name="cp_product_detail_4">
		<label for="cp_product_detail_5">Product detail #5</label>
		<input id="cp_product_detail_5" type="text" name="cp_product_detail_5" value="<?php if(get_option('cp_product_detail_5')) { echo get_option('cp_product_detail_5'); } else { echo ""; } ?>" name="cp_product_detail_5">
		<label for="cp_product_detail_6">Product detail #6</label>
		<input id="cp_product_detail_6" type="text" name="cp_product_detail_6" value="<?php if(get_option('cp_product_detail_6')) { echo get_option('cp_product_detail_6'); } else { echo ""; } ?>" name="cp_product_detail_6">
		<label for="cp_product_detail_7">Product detail #7</label>
		<input id="cp_product_detail_7" type="text" name="cp_product_detail_7" value="<?php if(get_option('cp_product_detail_7')) { echo get_option('cp_product_detail_7'); } else { echo ""; } ?>" name="cp_product_detail_7">
		<label for="cp_product_detail_8">Product detail #8</label>
		<input id="cp_product_detail_8" type="text" name="cp_product_detail_8" value="<?php if(get_option('cp_product_detail_8')) { echo get_option('cp_product_detail_8'); } else { echo ""; } ?>" name="cp_product_detail_8">
		<label for="cp_product_detail_9">Product detail #9</label>
		<input id="cp_product_detail_9" type="text" name="cp_product_detail_9" value="<?php if(get_option('cp_product_detail_9')) { echo get_option('cp_product_detail_9'); } else { echo ""; } ?>" name="cp_product_detail_9">
		<label for="cp_product_detail_10">Product detail #10</label>
		<input id="cp_product_detail_10" type="text" name="cp_product_detail_10" value="<?php if(get_option('cp_product_detail_10')) { echo get_option('cp_product_detail_10'); } else { echo ""; } ?>" name="cp_product_detail_10">
</div>

<div id="tabs-4">
	<h1>Contact settings</h1>
	<label for="cp_contact_mail">Contact E-Mail</label>
	<input id="cp_contact_mail" type="text" value="<?php if(get_option('cp_contact_mail')) { echo get_option('cp_contact_mail'); } else { echo ""; } ?>" name="cp_contact_mail">
	<label for="cp_phone_1">Phone #1</label>
	<input id="cp_phone_1" type="text" value="<?php if(get_option('cp_phone_1')) { echo get_option('cp_phone_1'); } else { echo ""; } ?>" name="cp_phone_1">
	<label for="cp_phone_2">Phone #2</label>
	<input id="cp_phone_2" type="text" value="<?php if(get_option('cp_phone_2')) { echo get_option('cp_phone_2'); } else { echo ""; } ?>" name="cp_phone_2">
	<label for="cp_phone_3">Phone #3</label>
	<input id="cp_phone_3" type="text" value="<?php if(get_option('cp_phone_3')) { echo get_option('cp_phone_3'); } else { echo ""; } ?>" name="cp_phone_3">
</div>

<div id="tabs-5">
	<h1>Social settings</h1>
	<label for="cp_facebook">Facebook URL</label>
	<input id="cp_facebook" type="text" value="<?php if(get_option('cp_facebook')) { echo get_option('cp_facebook'); } else { echo ""; } ?>" name="cp_facebook">
	<label for="cp_twitter">Twitter URL</label>
	<input id="cp_twitter" type="text" value="<?php if(get_option('cp_twitter')) { echo get_option('cp_twitter'); } else { echo ""; } ?>" name="cp_twitter">
	<label for="cp_google">Google+ URL</label>
	<input id="cp_google" type="text" value="<?php if(get_option('cp_google')) { echo get_option('cp_google'); } else { echo ""; } ?>" name="cp_google">
	<label for="cp_youtube">Youtube URL</label>
	<input id="cp_youtube" type="text" value="<?php if(get_option('cp_youtube')) { echo get_option('cp_youtube'); } else { echo ""; } ?>" name="cp_youtube">
</div>

<div id="tabs-6">
	<h1>Personal settings</h1>
	<label for="cp_address">Address</label>
	<input id="cp_address" type="text" value="<?php if(get_option('cp_address')) { echo get_option('cp_address'); } else { echo ""; } ?>" name="cp_address">
	<label for="cp_zip">Zip code</label>
	<input id="cp_zip" type="text" value="<?php if(get_option('cp_zip')) { echo get_option('cp_zip'); } else { echo ""; } ?>" name="cp_zip">
	<label for="cp_city">City</label>
	<input id="cp_city" type="text" value="<?php if(get_option('cp_city')) { echo get_option('cp_city'); } else { echo ""; } ?>" name="cp_city">
	<label for="cp_country">Country</label>
	<input id="cp_country" type="text" value="<?php if(get_option('cp_country')) { echo get_option('cp_country'); } else { echo ""; } ?>" name="cp_country">
</div>
</div>
</div>
<input type="hidden" name="save" value="save">
<input type="submit" id="save-settings" value="Save Settings">
</form>

<?php
}

?>
